|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* File containing the {@see ConvertHelper_StorageSizeEnum} class. |
|
4
|
|
|
* |
|
5
|
|
|
* @package AppUtils |
|
6
|
|
|
* @subpackage ConvertHelper |
|
7
|
|
|
* @see ConvertHelper_StorageSizeEnum |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace AppUtils; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class ConvertHelper_StorageSizeEnum |
|
16
|
|
|
{ |
|
17
|
|
|
const ERROR_UNKNOWN_UNIT_NAME = 43901; |
|
18
|
|
|
|
|
19
|
|
|
const BASE_10 = 1000; |
|
20
|
|
|
|
|
21
|
|
|
const BASE_2 = 1024; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var ConvertHelper_StorageSizeEnum_Size |
|
25
|
|
|
*/ |
|
26
|
|
|
protected static $sizes; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Stores the supported unit notations, and |
|
30
|
|
|
* how they are supposed to be calculated. |
|
31
|
|
|
* |
|
32
|
|
|
* The `base` key: This defines how many bytes there are |
|
33
|
|
|
* in a kilobyte, to differentiate with the two |
|
34
|
|
|
* common way to calculate sizes: base 10 or base 2. |
|
35
|
|
|
* See the Wikipedia link for more details. |
|
36
|
|
|
* |
|
37
|
|
|
* The `exponent` key: This defines the multiplier to |
|
38
|
|
|
* multiply the value with to get the target size. |
|
39
|
|
|
* |
|
40
|
|
|
* @var array |
|
41
|
|
|
* @see https://en.m.wikipedia.org/wiki/Megabyte#Definitions |
|
42
|
|
|
* @see ConvertHelper_SizeNotation::parseSize() |
|
43
|
|
|
*/ |
|
44
|
|
|
protected static function init() |
|
45
|
|
|
{ |
|
46
|
|
|
if(isset(self::$sizes)) { |
|
47
|
|
|
return; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
self::$sizes = array(); |
|
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
self::addSize('kib', self::BASE_2, 1, t('KiB'), t('Kibibyte'), t('Kibibytes')); |
|
53
|
|
|
self::addSize('mib', self::BASE_2, 2, t('MiB'), t('Mebibyte'), t('Mebibytes')); |
|
54
|
|
|
self::addSize('gib', self::BASE_2, 3, t('GiB'), t('Gibibyte'), t('Gibibytes')); |
|
55
|
|
|
self::addSize('tib', self::BASE_2, 4, t('TiB'), t('Tebibyte'), t('Tebibytes')); |
|
56
|
|
|
self::addSize('pib', self::BASE_2, 5, t('PiB'), t('Pebibyte'), t('Pebibytes')); |
|
57
|
|
|
|
|
58
|
|
|
self::addSize('kb', self::BASE_10, 1, t('KB'), t('Kilobyte'), t('Kilobytes')); |
|
59
|
|
|
self::addSize('mb', self::BASE_10, 2, t('MB'), t('Megabyte'), t('Megabytes')); |
|
60
|
|
|
self::addSize('gb', self::BASE_10, 3, t('GB'), t('Gigabyte'), t('Gigabytes')); |
|
61
|
|
|
self::addSize('tb', self::BASE_10, 4, t('TB'), t('Terabyte'), t('Terabytes')); |
|
62
|
|
|
self::addSize('pb', self::BASE_10, 5, t('PB'), t('Petabyte'), t('Petabytes')); |
|
63
|
|
|
|
|
64
|
|
|
self::addSize('b', 1, 1, t('B'), t('Byte'), t('Bytes')); |
|
65
|
|
|
|
|
66
|
|
|
if(class_exists('AppLocalize\Localization')) |
|
67
|
|
|
{ |
|
68
|
|
|
\AppLocalize\Localization::onLocaleChanged(array(self::class, 'handle_localeChanged')); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Called whenever the application locale is changed, |
|
74
|
|
|
* to reset the size definitions so the labels get |
|
75
|
|
|
* translated to the new locale. |
|
76
|
|
|
* |
|
77
|
|
|
* @param \AppLocalize\Localization_Event_LocaleChanged $event |
|
78
|
|
|
*/ |
|
79
|
|
|
public static function handle_localeChanged(\AppLocalize\Localization_Event_LocaleChanged $event) |
|
|
|
|
|
|
80
|
|
|
{ |
|
81
|
|
|
self::$sizes = null; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected static function addSize($name, int $base, int $exponent, string $suffix, string $singular, string $plural) |
|
85
|
|
|
{ |
|
86
|
|
|
self::$sizes[$name] = new ConvertHelper_StorageSizeEnum_Size( |
|
87
|
|
|
$name, |
|
88
|
|
|
$base, |
|
89
|
|
|
$exponent, |
|
90
|
|
|
$suffix, |
|
91
|
|
|
$singular, |
|
92
|
|
|
$plural |
|
93
|
|
|
); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Retrieves all known sizes. |
|
98
|
|
|
* @return \AppUtils\ConvertHelper_StorageSizeEnum_Size |
|
99
|
|
|
*/ |
|
100
|
|
|
public static function getSizes() |
|
101
|
|
|
{ |
|
102
|
|
|
self::init(); |
|
103
|
|
|
|
|
104
|
|
|
return self::$sizes; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public static function getSizeByName(string $name) : ConvertHelper_StorageSizeEnum_Size |
|
108
|
|
|
{ |
|
109
|
|
|
self::init(); |
|
110
|
|
|
|
|
111
|
|
|
if(isset(self::$sizes[$name])) { |
|
112
|
|
|
return self::$sizes[$name]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
throw new ConvertHelper_Exception( |
|
116
|
|
|
'Unknown storage size name '.$name.'.', |
|
117
|
|
|
sprintf( |
|
118
|
|
|
'The storage size name [%s] does not exist. Avaialable names are: [%s].', |
|
119
|
|
|
$name, |
|
120
|
|
|
implode(', ', self::getSizeNames()) |
|
121
|
|
|
), |
|
122
|
|
|
self::ERROR_UNKNOWN_UNIT_NAME |
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Retrieves a list of all size names, e.g. "mb", "kib" (lowercase). |
|
128
|
|
|
* @return array |
|
129
|
|
|
*/ |
|
130
|
|
|
public static function getSizeNames() : array |
|
131
|
|
|
{ |
|
132
|
|
|
self::init(); |
|
133
|
|
|
|
|
134
|
|
|
return array_keys(self::$sizes); |
|
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public static function getSizesByBase(int $base) |
|
138
|
|
|
{ |
|
139
|
|
|
self::init(); |
|
140
|
|
|
|
|
141
|
|
|
$result = array(); |
|
142
|
|
|
|
|
143
|
|
|
foreach(self::$sizes as $size) |
|
144
|
|
|
{ |
|
145
|
|
|
if($size->getBase() === $base) { |
|
146
|
|
|
$result[] = $size; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return $result; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..