ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
DataSize.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Data;
22 
30 final class DataSize
31 {
32  private const SIZE_FACTOR = 1000;
33  private const PRECISION = 2;
34  public const Byte = 1;
35  // binary
36  public const KiB = 1024;
37  public const MiB = 1_048_576;
38  // pow(1024, 2)
39  public const GiB = 1_073_741_824;
40  public const TiB = 1_099_511_627_776;
41  // decimal
42  public const KB = 1000;
43  // kilobyte
44  public const MB = 1_000_000;
45  // megabyte
46  public const GB = 1_000_000_000;
47  // gigabyte
48  public const TB = 1_000_000_000_000;
49  // terabyte
53  public static array $abbreviations = [
54  'B' => self::Byte,
55 
56  'KB' => self::KB,
57  'K' => self::KiB,
58  'k' => self::KiB,
59  'KiB' => self::KiB,
60 
61  'MB' => self::MB,
62  'M' => self::MiB,
63  'm' => self::MiB,
64  'MiB' => self::MiB,
65 
66  'GB' => self::GB,
67  'G' => self::GiB,
68  'g' => self::GiB,
69  'GiB' => self::GiB,
70 
71  'TB' => self::TB,
72  'TiB' => self::TiB,
73  ];
74  private float $size;
75  private int $unit;
76  private string $suffix;
85  public function __construct(int $size, int $unit)
86  {
87  $this->suffix = $this->mapUnitToSuffix($unit);
88  $this->size = (float) $size / (float) $unit; // the div operation can return int and float
89  $this->unit = $unit;
90  }
91  private function mapUnitToSuffix(int $unit): string
92  {
93  switch ($unit) {
94  case self::Byte:
95  return 'B';
96  case self::KiB:
97  return 'KiB';
98  case self::MiB:
99  return 'MiB';
100  case self::GiB:
101  return 'GiB';
102  case self::TiB:
103  return 'TiB';
104  case self::KB:
105  return 'KB';
106  case self::MB:
107  return 'MB';
108  case self::GB:
109  return 'GB';
110  case self::TB:
111  return 'TB';
112  default:
113  throw new \InvalidArgumentException('The given data size unit is not valid, please check the provided class constants of the DataSize class.');
114  }
115  }
119  public function getSize(): float
120  {
121  return $this->size;
122  }
126  public function getUnit(): int
127  {
128  return $this->unit;
129  }
133  public function inBytes(): float
134  {
135  return $this->size * $this->unit;
136  }
144  public function __toString(): string
145  {
146  $size = $this->inBytes();
147  // can be switched to match in ILIAS 9
148  switch (true) {
149  case $size > self::SIZE_FACTOR * self::SIZE_FACTOR * self::SIZE_FACTOR * self::SIZE_FACTOR:
150  $unit = DataSize::TB;
151  break;
152  case $size > self::SIZE_FACTOR * self::SIZE_FACTOR * self::SIZE_FACTOR:
153  $unit = DataSize::GB;
154  break;
155  case $size > self::SIZE_FACTOR * self::SIZE_FACTOR:
156  $unit = DataSize::MB;
157  break;
158  case $size > self::SIZE_FACTOR:
159  $unit = DataSize::KB;
160  break;
161  default:
162  $unit = DataSize::Byte;
163  break;
164  }
165 
166  $size = round($size / (float) $unit, self::PRECISION);
167 
168  return "$size " . $this->mapUnitToSuffix($unit);
169  }
170 }
mapUnitToSuffix(int $unit)
Definition: DataSize.php:91
This class provides the data size with additional information to remove the work to calculate the siz...
Definition: DataSize.php:30
static array float $size
Definition: DataSize.php:56
inBytes()
Get the size in bytes.
Definition: DataSize.php:133
static array $abbreviations
Definition: DataSize.php:53
__toString()
Returns the data size in a human readable manner.
Definition: DataSize.php:144
getSize()
The calculated data size.
Definition: DataSize.php:119
getUnit()
The unit which equals the class constant used to calculate the data size.
Definition: DataSize.php:126
__construct(int $size, int $unit)
DataSize constructor.
Definition: DataSize.php:85