ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
DataSize.php
Go to the documentation of this file.
1 <?php
2 
3 namespace ILIAS\Data;
4 
15 final class DataSize
16 {
17  const Byte = 1;
18 
19  //binary
20  const KiB = 1024;
21  const MiB = 1048576; //pow(1024, 2)
22  const GiB = 1073741824;
23  const TiB = 1099511627776;
24  const PiB = 1125899906842624;
25  const EiB = 1152921504606846976;
26  const ZiB = 1180591620717411303424;
27  const YiB = 1208925819614629174706176;
28 
29  //decimal
30  const KB = 1000; //kilobyte
31  const MB = 1000000; //megabyte
32  const GB = 1000000000; //gigabyte
33  const TB = 1000000000000; //terabyte
34  const PB = 1000000000000000; //petabyte
35  const EB = 1000000000000000000; //exabyte
36  const ZB = 1000000000000000000000; //zettabyte
37  const YB = 1000000000000000000000000; //yottabyte
41  private static $suffixMap = [
42  self::Byte => 'B',
43 
44  self::KB => 'KB',
45  self::KiB => 'KiB',
46 
47  self::MB => 'MB',
48  self::MiB => 'MiB',
49 
50  self::GB => 'GB',
51  self::GiB => 'GiB',
52 
53  self::TB => 'TB',
54  self::TiB => 'TiB',
55 
56  self::PB => 'PB',
57  self::PiB => 'PiB',
58 
59  self::EB => 'EB',
60  self::EiB => 'EiB',
61 
62  self::ZB => 'ZB',
63  self::ZiB => 'ZiB',
64 
65  self::YB => 'YB',
66  self::YiB => 'YiB'
67  ];
68 
69  public static $abbreviations = [
70  'B' => self::Byte,
71 
72  'KB' => self::KB,
73  'K' => self::KiB,
74  'k' => self::KiB,
75  'KiB' => self::KiB,
76 
77  'MB' => self::MB,
78  'M' => self::MiB,
79  'm' => self::MiB,
80  'MiB' => self::MiB,
81 
82  'GB' => self::GB,
83  'G' => self::GiB,
84  'g' => self::GiB,
85  'GiB' => self::GiB,
86 
87  'TB' => self::TB,
88  'TiB' => self::TiB,
89 
90  'PB' => self::PB,
91  'PiB' => self::PiB,
92 
93  'EB' => self::EB,
94  'EiB' => self::EiB,
95 
96  'ZB' => self::ZB,
97  'ZiB' => self::ZiB,
98 
99  'YB' => self::YB,
100  'YiB' => self::YiB
101  ];
102 
106  private $size;
110  private $unit;
114  private $suffix;
115 
126  public function __construct(int $size, int $unit)
127  {
128  if (!isset(self::$suffixMap[$unit])) {
129  throw new \InvalidArgumentException('The given data size unit is not valid, please check the provided class constants of the DataSize class.');
130  }
131 
132  $this->size = (float) $size / $unit; //the div operation can return int and float
133  $this->unit = $unit;
134  $this->suffix = self::$suffixMap[$unit];
135  }
136 
143  public function getSize()
144  {
145  return $this->size;
146  }
147 
148 
155  public function getUnit()
156  {
157  return $this->unit;
158  }
159 
163  public function inBytes() : int
164  {
165  return $this->size * $this->unit;
166  }
167 
178  public function __toString()
179  {
180  return "{$this->size} {$this->suffix}";
181  }
182 }
Class DataSize.
Definition: DataSize.php:15
inBytes()
Get the size in bytes.
Definition: DataSize.php:163
static $abbreviations
Definition: DataSize.php:69
__toString()
Returns the data size with the corresponding suffix.
Definition: DataSize.php:178
getSize()
The calculated data size.
Definition: DataSize.php:143
getUnit()
The unit which equals the class constant used to calculate the data size.
Definition: DataSize.php:155
__construct(int $size, int $unit)
DataSize constructor.
Definition: DataSize.php:126