ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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  ];
71  private $size;
75  private $unit;
79  private $suffix;
80 
91  public function __construct($size, $unit)
92  {
93  if (!is_int($size)) {
94  throw new \InvalidArgumentException("Size must be of the type int.");
95  }
96 
97  if (!is_int($unit)) {
98  throw new \InvalidArgumentException("Unit must be of the type int.");
99  }
100 
101  if (!isset(self::$suffixMap[$unit])) {
102  throw new \InvalidArgumentException('The given data size unit is not valid, please check the provided class constants of the DataSize class.');
103  }
104 
105  $this->size = (float) $size / $unit; //the div operation can return int and float
106  $this->unit = $unit;
107  $this->suffix = self::$suffixMap[$unit];
108  }
109 
116  public function getSize()
117  {
118  return $this->size;
119  }
120 
121 
128  public function getUnit()
129  {
130  return $this->unit;
131  }
132 
143  public function __toString()
144  {
145  return "{$this->size} {$this->suffix}";
146  }
147 }
__construct($size, $unit)
DataSize constructor.
Definition: DataSize.php:91
Class DataSize.
Definition: DataSize.php:15
memory_get_usage(true)/1024/1024) MB
__toString()
Returns the data size with the corresponding suffix.
Definition: DataSize.php:143
getSize()
The calculated data size.
Definition: DataSize.php:116
getUnit()
The unit which equals the class constant used to calculate the data size.
Definition: DataSize.php:128
Set page orientation and size
Definition: 04printing.php:77