ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
DataSize.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Data;
22
30final class DataSize implements \Stringable
31{
32 private const SIZE_FACTOR = 1000;
33 private const PRECISION = 2;
34
35 public const Byte = 1;
36
37 // binary
38 public const KiB = 1024;
39 public const MiB = 1_048_576; // pow(1024, 2)
40 public const GiB = 1_073_741_824;
41 public const TiB = 1_099_511_627_776;
42
43 // decimal
44 public const KB = 1000; // kilobyte
45 public const MB = 1_000_000; // megabyte
46 public const GB = 1_000_000_000; // gigabyte
47 public const TB = 1_000_000_000_000; // terabyte
48
52 public static array $abbreviations = [
53 'B' => self::Byte,
54
55 'KB' => self::KB,
56 'K' => self::KiB,
57 'k' => self::KiB,
58 'KiB' => self::KiB,
59
60 'MB' => self::MB,
61 'M' => self::MiB,
62 'm' => self::MiB,
63 'MiB' => self::MiB,
64
65 'GB' => self::GB,
66 'G' => self::GiB,
67 'g' => self::GiB,
68 'GiB' => self::GiB,
69
70 'TB' => self::TB,
71 'TiB' => self::TiB,
72 ];
73
74 private float $size;
75 private int $unit;
76 private string $suffix;
77
86 public function __construct(int $size, int $unit)
87 {
88 $this->suffix = $this->mapUnitToSuffix($unit);
89 $this->size = (float) $size / (float) $unit; // the div operation can return int and float
90 $this->unit = $unit;
91 }
92
93 private function mapUnitToSuffix(int $unit): string
94 {
95 return match ($unit) {
96 self::Byte => 'B',
97 self::KiB => 'KiB',
98 self::MiB => 'MiB',
99 self::GiB => 'GiB',
100 self::TiB => 'TiB',
101 self::KB => 'KB',
102 self::MB => 'MB',
103 self::GB => 'GB',
104 self::TB => 'TB',
105 default => throw new \InvalidArgumentException('The given data size unit is not valid, please check the provided class constants of the DataSize class.')
106 };
107 }
108
112 public function getSize(): float
113 {
114 return $this->size;
115 }
116
120 public function getUnit(): int
121 {
122 return $this->unit;
123 }
124
128 public function inBytes(): float
129 {
130 return $this->size * $this->unit;
131 }
132
140 public function __toString(): string
141 {
142 $size = $this->inBytes();
143 $unit = match (true) {
144 $size > self::SIZE_FACTOR * self::SIZE_FACTOR * self::SIZE_FACTOR * self::SIZE_FACTOR => DataSize::TB,
145 $size > self::SIZE_FACTOR * self::SIZE_FACTOR * self::SIZE_FACTOR => DataSize::GB,
146 $size > self::SIZE_FACTOR * self::SIZE_FACTOR => DataSize::MB,
147 $size > self::SIZE_FACTOR => DataSize::KB,
148 default => DataSize::Byte,
149 };
150
151 $size = round($size / (float) $unit, self::PRECISION);
152
153 return "$size " . $this->mapUnitToSuffix($unit);
154 }
155}
This class provides the data size with additional information to remove the work to calculate the siz...
Definition: DataSize.php:31
mapUnitToSuffix(int $unit)
Definition: DataSize.php:93
getSize()
The calculated data size.
Definition: DataSize.php:112
getUnit()
The unit which equals the class constant used to calculate the data size.
Definition: DataSize.php:120
__construct(int $size, int $unit)
DataSize constructor.
Definition: DataSize.php:86
__toString()
Returns the data size in a human readable manner.
Definition: DataSize.php:140
static array $abbreviations
Definition: DataSize.php:52
inBytes()
Get the size in bytes.
Definition: DataSize.php:128