@@ -31,9 +31,9 @@ discard block |
||
31 | 31 | |
32 | 32 | const VALIDATION_ERROR_NEGATIVE_VALUE = 43803; |
33 | 33 | |
34 | - /** |
|
35 | - * @var string |
|
36 | - */ |
|
34 | + /** |
|
35 | + * @var string |
|
36 | + */ |
|
37 | 37 | protected $sizeString; |
38 | 38 | |
39 | 39 | /** |
@@ -41,41 +41,41 @@ discard block |
||
41 | 41 | */ |
42 | 42 | protected $sizeDefinition; |
43 | 43 | |
44 | - /** |
|
45 | - * @var integer |
|
46 | - */ |
|
44 | + /** |
|
45 | + * @var integer |
|
46 | + */ |
|
47 | 47 | protected $bytes = 0; |
48 | 48 | |
49 | - /** |
|
50 | - * @var bool |
|
51 | - */ |
|
49 | + /** |
|
50 | + * @var bool |
|
51 | + */ |
|
52 | 52 | protected $valid = true; |
53 | 53 | |
54 | - /** |
|
55 | - * @var string |
|
56 | - */ |
|
54 | + /** |
|
55 | + * @var string |
|
56 | + */ |
|
57 | 57 | protected $units = null; |
58 | 58 | |
59 | - /** |
|
60 | - * @var string |
|
61 | - */ |
|
59 | + /** |
|
60 | + * @var string |
|
61 | + */ |
|
62 | 62 | protected $number = ''; |
63 | 63 | |
64 | - /** |
|
65 | - * @var string |
|
66 | - */ |
|
64 | + /** |
|
65 | + * @var string |
|
66 | + */ |
|
67 | 67 | protected $errorMessage = ''; |
68 | 68 | |
69 | - /** |
|
70 | - * @var int |
|
71 | - */ |
|
69 | + /** |
|
70 | + * @var int |
|
71 | + */ |
|
72 | 72 | protected $errorCode = 0; |
73 | 73 | |
74 | - /** |
|
75 | - * Create a new instance for the specified size string. |
|
76 | - * |
|
77 | - * @param string $sizeString A size notation in the format "50 MB", or a number of bytes without units. Spaces are ignored, so "50MB" = "50 MB" = " 50 MB ". Floating point values are accepted, both with dot and comma notation ("50.5" = "50,5"). To use Base 2 values, ose appropriate units, e.g. "50 MiB", "1.5 GiB". The units are case insensitive, so "50 MB" = "50 mb". |
|
78 | - */ |
|
74 | + /** |
|
75 | + * Create a new instance for the specified size string. |
|
76 | + * |
|
77 | + * @param string $sizeString A size notation in the format "50 MB", or a number of bytes without units. Spaces are ignored, so "50MB" = "50 MB" = " 50 MB ". Floating point values are accepted, both with dot and comma notation ("50.5" = "50,5"). To use Base 2 values, ose appropriate units, e.g. "50 MiB", "1.5 GiB". The units are case insensitive, so "50 MB" = "50 mb". |
|
78 | + */ |
|
79 | 79 | public function __construct(string $sizeString) |
80 | 80 | { |
81 | 81 | $this->sizeString = $this->cleanString($sizeString); |
@@ -83,31 +83,31 @@ discard block |
||
83 | 83 | $this->convert(); |
84 | 84 | } |
85 | 85 | |
86 | - /** |
|
87 | - * Gets the amount of bytes contained in the size notation. |
|
88 | - * @return int |
|
89 | - */ |
|
86 | + /** |
|
87 | + * Gets the amount of bytes contained in the size notation. |
|
88 | + * @return int |
|
89 | + */ |
|
90 | 90 | public function toBytes() : int |
91 | 91 | { |
92 | 92 | return $this->bytes; |
93 | 93 | } |
94 | 94 | |
95 | - /** |
|
96 | - * Converts the size notation to a human readable string, e.g. "50 MB". |
|
97 | - * |
|
98 | - * @param int $precision |
|
99 | - * @return string |
|
100 | - * @see ConvertHelper::bytes2readable() |
|
101 | - */ |
|
95 | + /** |
|
96 | + * Converts the size notation to a human readable string, e.g. "50 MB". |
|
97 | + * |
|
98 | + * @param int $precision |
|
99 | + * @return string |
|
100 | + * @see ConvertHelper::bytes2readable() |
|
101 | + */ |
|
102 | 102 | public function toString(int $precision=1) : string |
103 | 103 | { |
104 | 104 | return ConvertHelper::bytes2readable($this->bytes, $precision, $this->getBase()); |
105 | 105 | } |
106 | 106 | |
107 | - /** |
|
108 | - * Retrieves the detected number's base. |
|
109 | - * @return int The base number (1000 for Base 10, or 1024 for Base 2), or 0 if it is not valid. |
|
110 | - */ |
|
107 | + /** |
|
108 | + * Retrieves the detected number's base. |
|
109 | + * @return int The base number (1000 for Base 10, or 1024 for Base 2), or 0 if it is not valid. |
|
110 | + */ |
|
111 | 111 | public function getBase() : int |
112 | 112 | { |
113 | 113 | if($this->isValid()) { |
@@ -117,13 +117,13 @@ discard block |
||
117 | 117 | return 0; |
118 | 118 | } |
119 | 119 | |
120 | - /** |
|
121 | - * Retrieves the detected storage size instance, if |
|
122 | - * the size string is valid. |
|
123 | - * |
|
124 | - * @return ConvertHelper_StorageSizeEnum_Size|NULL |
|
125 | - * @see ConvertHelper_StorageSizeEnum_Size |
|
126 | - */ |
|
120 | + /** |
|
121 | + * Retrieves the detected storage size instance, if |
|
122 | + * the size string is valid. |
|
123 | + * |
|
124 | + * @return ConvertHelper_StorageSizeEnum_Size|NULL |
|
125 | + * @see ConvertHelper_StorageSizeEnum_Size |
|
126 | + */ |
|
127 | 127 | public function getSizeDefinition() : ?ConvertHelper_StorageSizeEnum_Size |
128 | 128 | { |
129 | 129 | if($this->isValid()) { |
@@ -133,27 +133,27 @@ discard block |
||
133 | 133 | return null; |
134 | 134 | } |
135 | 135 | |
136 | - /** |
|
137 | - * Checks whether the size notation was valid and could be parsed |
|
138 | - * into a meaningful byte value. If this returns `false`, it is |
|
139 | - * possible to use the `getErrorXXX` methods to retrieve information |
|
140 | - * on what went wrong. |
|
141 | - * |
|
142 | - * @return bool |
|
143 | - * @see ConvertHelper_SizeNotation::getErrorMessage() |
|
144 | - * @see ConvertHelper_SizeNotation::getErrorCode() |
|
145 | - */ |
|
136 | + /** |
|
137 | + * Checks whether the size notation was valid and could be parsed |
|
138 | + * into a meaningful byte value. If this returns `false`, it is |
|
139 | + * possible to use the `getErrorXXX` methods to retrieve information |
|
140 | + * on what went wrong. |
|
141 | + * |
|
142 | + * @return bool |
|
143 | + * @see ConvertHelper_SizeNotation::getErrorMessage() |
|
144 | + * @see ConvertHelper_SizeNotation::getErrorCode() |
|
145 | + */ |
|
146 | 146 | public function isValid() : bool |
147 | 147 | { |
148 | 148 | return $this->valid; |
149 | 149 | } |
150 | 150 | |
151 | - /** |
|
152 | - * Retrieves the error message if the size notation validation failed. |
|
153 | - * |
|
154 | - * @return string |
|
155 | - * @see ConvertHelper_SizeNotation::getErrorCode() |
|
156 | - */ |
|
151 | + /** |
|
152 | + * Retrieves the error message if the size notation validation failed. |
|
153 | + * |
|
154 | + * @return string |
|
155 | + * @see ConvertHelper_SizeNotation::getErrorCode() |
|
156 | + */ |
|
157 | 157 | public function getErrorMessage() : string |
158 | 158 | { |
159 | 159 | return $this->errorMessage; |
@@ -200,12 +200,12 @@ discard block |
||
200 | 200 | ); |
201 | 201 | } |
202 | 202 | |
203 | - /** |
|
204 | - * Detects the units and the number in the size notation string. |
|
205 | - * Populates the `units` and `number` properties. |
|
206 | - * |
|
207 | - * @return bool Whether the string could be parsed successfully. |
|
208 | - */ |
|
203 | + /** |
|
204 | + * Detects the units and the number in the size notation string. |
|
205 | + * Populates the `units` and `number` properties. |
|
206 | + * |
|
207 | + * @return bool Whether the string could be parsed successfully. |
|
208 | + */ |
|
209 | 209 | protected function detectParts() : bool |
210 | 210 | { |
211 | 211 | $units = ConvertHelper_StorageSizeEnum::getSizeNames(); |
@@ -241,16 +241,16 @@ discard block |
||
241 | 241 | return true; |
242 | 242 | } |
243 | 243 | |
244 | - /** |
|
245 | - * If the validation fails, this is used to store the reason for retrieval later. |
|
246 | - * |
|
247 | - * @param string $message |
|
248 | - * @param int $code |
|
249 | - * |
|
250 | - * @see ConvertHelper_SizeNotation::isValid() |
|
251 | - * @see ConvertHelper_SizeNotation::getErrorMessage() |
|
252 | - * @see ConvertHelper_SizeNotation::getErrorCode() |
|
253 | - */ |
|
244 | + /** |
|
245 | + * If the validation fails, this is used to store the reason for retrieval later. |
|
246 | + * |
|
247 | + * @param string $message |
|
248 | + * @param int $code |
|
249 | + * |
|
250 | + * @see ConvertHelper_SizeNotation::isValid() |
|
251 | + * @see ConvertHelper_SizeNotation::getErrorMessage() |
|
252 | + * @see ConvertHelper_SizeNotation::getErrorCode() |
|
253 | + */ |
|
254 | 254 | protected function setError(string $message, int $code) : void |
255 | 255 | { |
256 | 256 | $this->valid = false; |
@@ -258,12 +258,12 @@ discard block |
||
258 | 258 | $this->errorCode = $code; |
259 | 259 | } |
260 | 260 | |
261 | - /** |
|
262 | - * Retrieves the error code, if the size notation validation failed. |
|
263 | - * |
|
264 | - * @return int |
|
265 | - * @see ConvertHelper_SizeNotation::getErrorMessage() |
|
266 | - */ |
|
261 | + /** |
|
262 | + * Retrieves the error code, if the size notation validation failed. |
|
263 | + * |
|
264 | + * @return int |
|
265 | + * @see ConvertHelper_SizeNotation::getErrorMessage() |
|
266 | + */ |
|
267 | 267 | public function getErrorCode() : int |
268 | 268 | { |
269 | 269 | return $this->errorCode; |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | * @return string |
100 | 100 | * @see ConvertHelper::bytes2readable() |
101 | 101 | */ |
102 | - public function toString(int $precision=1) : string |
|
102 | + public function toString(int $precision = 1) : string |
|
103 | 103 | { |
104 | 104 | return ConvertHelper::bytes2readable($this->bytes, $precision, $this->getBase()); |
105 | 105 | } |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | */ |
111 | 111 | public function getBase() : int |
112 | 112 | { |
113 | - if($this->isValid()) { |
|
113 | + if ($this->isValid()) { |
|
114 | 114 | return $this->sizeDefinition->getBase(); |
115 | 115 | } |
116 | 116 | |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | */ |
127 | 127 | public function getSizeDefinition() : ?ConvertHelper_StorageSizeEnum_Size |
128 | 128 | { |
129 | - if($this->isValid()) { |
|
129 | + if ($this->isValid()) { |
|
130 | 130 | return $this->sizeDefinition; |
131 | 131 | } |
132 | 132 | |
@@ -175,18 +175,18 @@ discard block |
||
175 | 175 | |
176 | 176 | protected function parseSize() : void |
177 | 177 | { |
178 | - if(!$this->detectParts()) { |
|
178 | + if (!$this->detectParts()) { |
|
179 | 179 | return; |
180 | 180 | } |
181 | 181 | |
182 | 182 | // we detected units in the string: all good. |
183 | - if($this->units !== null) |
|
183 | + if ($this->units !== null) |
|
184 | 184 | { |
185 | 185 | return; |
186 | 186 | } |
187 | 187 | |
188 | 188 | // just a numeric value: we assume this means we're dealing with bytes. |
189 | - if(is_numeric($this->number)) |
|
189 | + if (is_numeric($this->number)) |
|
190 | 190 | { |
191 | 191 | $this->units = 'b'; |
192 | 192 | return; |
@@ -212,12 +212,12 @@ discard block |
||
212 | 212 | |
213 | 213 | $number = $this->sizeString; |
214 | 214 | |
215 | - foreach($units as $unit) |
|
215 | + foreach ($units as $unit) |
|
216 | 216 | { |
217 | - if(stristr($number, $unit)) |
|
217 | + if (stristr($number, $unit)) |
|
218 | 218 | { |
219 | 219 | // there are more than 1 unit defined in the string |
220 | - if($this->units !== null) |
|
220 | + if ($this->units !== null) |
|
221 | 221 | { |
222 | 222 | $this->setError( |
223 | 223 | t( |
@@ -273,14 +273,14 @@ discard block |
||
273 | 273 | { |
274 | 274 | $this->parseSize(); |
275 | 275 | |
276 | - if(!$this->valid) { |
|
276 | + if (!$this->valid) { |
|
277 | 277 | return; |
278 | 278 | } |
279 | 279 | |
280 | 280 | $int = intval($this->number); |
281 | 281 | |
282 | 282 | // negative values |
283 | - if($int < 0) |
|
283 | + if ($int < 0) |
|
284 | 284 | { |
285 | 285 | $this->setError( |
286 | 286 | t('Negative values cannot be used as size.'), |
@@ -31,17 +31,17 @@ discard block |
||
31 | 31 | |
32 | 32 | const BASE_2 = 1024; |
33 | 33 | |
34 | - /** |
|
35 | - * @var ConvertHelper_StorageSizeEnum_Size[] |
|
36 | - */ |
|
34 | + /** |
|
35 | + * @var ConvertHelper_StorageSizeEnum_Size[] |
|
36 | + */ |
|
37 | 37 | protected static $sizes; |
38 | 38 | |
39 | - /** |
|
40 | - * Initializes the supported unit notations, and |
|
41 | - * how they are supposed to be calculated. |
|
42 | - * |
|
43 | - * @see ConvertHelper_SizeNotation::parseSize() |
|
44 | - */ |
|
39 | + /** |
|
40 | + * Initializes the supported unit notations, and |
|
41 | + * how they are supposed to be calculated. |
|
42 | + * |
|
43 | + * @see ConvertHelper_SizeNotation::parseSize() |
|
44 | + */ |
|
45 | 45 | protected static function init() |
46 | 46 | { |
47 | 47 | if(isset(self::$sizes)) { |
@@ -70,30 +70,30 @@ discard block |
||
70 | 70 | } |
71 | 71 | } |
72 | 72 | |
73 | - /** |
|
74 | - * Called whenever the application locale is changed, |
|
75 | - * to reset the size definitions so the labels get |
|
76 | - * translated to the new locale. |
|
77 | - * |
|
78 | - * @param \AppLocalize\Localization_Event_LocaleChanged $event |
|
79 | - */ |
|
73 | + /** |
|
74 | + * Called whenever the application locale is changed, |
|
75 | + * to reset the size definitions so the labels get |
|
76 | + * translated to the new locale. |
|
77 | + * |
|
78 | + * @param \AppLocalize\Localization_Event_LocaleChanged $event |
|
79 | + */ |
|
80 | 80 | public static function handle_localeChanged(\AppLocalize\Localization_Event_LocaleChanged $event) |
81 | 81 | { |
82 | 82 | self::$sizes = null; |
83 | 83 | } |
84 | 84 | |
85 | - /** |
|
86 | - * Adds a storage size to the internal collection. |
|
87 | - * |
|
88 | - * @param string $name The lowercase size name, e.g. "kb", "mib" |
|
89 | - * @param int $base This defines how many bytes there are in a kilobyte, to differentiate with the two common way to calculate sizes: base 10 or base 2. See the Wikipedia link for more details. |
|
90 | - * @param int $exponent The multiplier of the base to get the byte value |
|
91 | - * @param string $suffix The localized short suffix, e.g. "KB", "MiB" |
|
92 | - * @param string $singular The localized singular label of the size, e.g. "Kilobyte". |
|
93 | - * @param string $plural The localized plural label of the size, e.g. "Kilobytes". |
|
94 | - * |
|
95 | - * @see https://en.m.wikipedia.org/wiki/Megabyte#Definitions |
|
96 | - */ |
|
85 | + /** |
|
86 | + * Adds a storage size to the internal collection. |
|
87 | + * |
|
88 | + * @param string $name The lowercase size name, e.g. "kb", "mib" |
|
89 | + * @param int $base This defines how many bytes there are in a kilobyte, to differentiate with the two common way to calculate sizes: base 10 or base 2. See the Wikipedia link for more details. |
|
90 | + * @param int $exponent The multiplier of the base to get the byte value |
|
91 | + * @param string $suffix The localized short suffix, e.g. "KB", "MiB" |
|
92 | + * @param string $singular The localized singular label of the size, e.g. "Kilobyte". |
|
93 | + * @param string $plural The localized plural label of the size, e.g. "Kilobytes". |
|
94 | + * |
|
95 | + * @see https://en.m.wikipedia.org/wiki/Megabyte#Definitions |
|
96 | + */ |
|
97 | 97 | protected static function addSize(string $name, int $base, int $exponent, string $suffix, string $singular, string $plural) |
98 | 98 | { |
99 | 99 | self::$sizes[$name] = new ConvertHelper_StorageSizeEnum_Size( |
@@ -106,11 +106,11 @@ discard block |
||
106 | 106 | ); |
107 | 107 | } |
108 | 108 | |
109 | - /** |
|
110 | - * Retrieves all known sizes. |
|
111 | - * |
|
112 | - * @return \AppUtils\ConvertHelper_StorageSizeEnum_Size[] |
|
113 | - */ |
|
109 | + /** |
|
110 | + * Retrieves all known sizes. |
|
111 | + * |
|
112 | + * @return \AppUtils\ConvertHelper_StorageSizeEnum_Size[] |
|
113 | + */ |
|
114 | 114 | public static function getSizes() |
115 | 115 | { |
116 | 116 | self::init(); |
@@ -118,15 +118,15 @@ discard block |
||
118 | 118 | return self::$sizes; |
119 | 119 | } |
120 | 120 | |
121 | - /** |
|
122 | - * Retrieves a size definition instance by its name. |
|
123 | - * |
|
124 | - * @param string $name Case insensitive. For example "kb", "MiB"... |
|
125 | - * @throws ConvertHelper_Exception |
|
126 | - * @return ConvertHelper_StorageSizeEnum_Size |
|
127 | - * |
|
128 | - * @see ConvertHelper_StorageSizeEnum::ERROR_UNKNOWN_UNIT_NAME |
|
129 | - */ |
|
121 | + /** |
|
122 | + * Retrieves a size definition instance by its name. |
|
123 | + * |
|
124 | + * @param string $name Case insensitive. For example "kb", "MiB"... |
|
125 | + * @throws ConvertHelper_Exception |
|
126 | + * @return ConvertHelper_StorageSizeEnum_Size |
|
127 | + * |
|
128 | + * @see ConvertHelper_StorageSizeEnum::ERROR_UNKNOWN_UNIT_NAME |
|
129 | + */ |
|
130 | 130 | public static function getSizeByName(string $name) : ConvertHelper_StorageSizeEnum_Size |
131 | 131 | { |
132 | 132 | self::init(); |
@@ -148,10 +148,10 @@ discard block |
||
148 | 148 | ); |
149 | 149 | } |
150 | 150 | |
151 | - /** |
|
152 | - * Retrieves a list of all size names, e.g. "mb", "kib" (lowercase). |
|
153 | - * @return array |
|
154 | - */ |
|
151 | + /** |
|
152 | + * Retrieves a list of all size names, e.g. "mb", "kib" (lowercase). |
|
153 | + * @return array |
|
154 | + */ |
|
155 | 155 | public static function getSizeNames() : array |
156 | 156 | { |
157 | 157 | self::init(); |
@@ -159,16 +159,16 @@ discard block |
||
159 | 159 | return array_keys(self::$sizes); |
160 | 160 | } |
161 | 161 | |
162 | - /** |
|
163 | - * Retrieves all available storage sizes for the specified |
|
164 | - * base value. |
|
165 | - * |
|
166 | - * @param int $base |
|
167 | - * @return \AppUtils\ConvertHelper_StorageSizeEnum_Size[] |
|
168 | - * |
|
169 | - * @see ConvertHelper_StorageSizeEnum::BASE_10 |
|
170 | - * @see ConvertHelper_StorageSizeEnum::BASE_2 |
|
171 | - */ |
|
162 | + /** |
|
163 | + * Retrieves all available storage sizes for the specified |
|
164 | + * base value. |
|
165 | + * |
|
166 | + * @param int $base |
|
167 | + * @return \AppUtils\ConvertHelper_StorageSizeEnum_Size[] |
|
168 | + * |
|
169 | + * @see ConvertHelper_StorageSizeEnum::BASE_10 |
|
170 | + * @see ConvertHelper_StorageSizeEnum::BASE_2 |
|
171 | + */ |
|
172 | 172 | public static function getSizesByBase(int $base) |
173 | 173 | { |
174 | 174 | self::init(); |