@@ -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,44 +41,44 @@ 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 | - * |
|
| 79 | - * @throws ConvertHelper_Exception |
|
| 80 | - * @see ConvertHelper_StorageSizeEnum::ERROR_UNKNOWN_UNIT_NAME |
|
| 81 | - */ |
|
| 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 | + * @throws ConvertHelper_Exception |
|
| 80 | + * @see ConvertHelper_StorageSizeEnum::ERROR_UNKNOWN_UNIT_NAME |
|
| 81 | + */ |
|
| 82 | 82 | public function __construct(string $sizeString) |
| 83 | 83 | { |
| 84 | 84 | $this->sizeString = $this->cleanString($sizeString); |
@@ -86,31 +86,31 @@ discard block |
||
| 86 | 86 | $this->convert(); |
| 87 | 87 | } |
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Gets the amount of bytes contained in the size notation. |
|
| 91 | - * @return int |
|
| 92 | - */ |
|
| 89 | + /** |
|
| 90 | + * Gets the amount of bytes contained in the size notation. |
|
| 91 | + * @return int |
|
| 92 | + */ |
|
| 93 | 93 | public function toBytes() : int |
| 94 | 94 | { |
| 95 | 95 | return $this->bytes; |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - /** |
|
| 99 | - * Converts the size notation to a human-readable string, e.g. "50 MB". |
|
| 100 | - * |
|
| 101 | - * @param int $precision |
|
| 102 | - * @return string |
|
| 103 | - * @see ConvertHelper::bytes2readable() |
|
| 104 | - */ |
|
| 98 | + /** |
|
| 99 | + * Converts the size notation to a human-readable string, e.g. "50 MB". |
|
| 100 | + * |
|
| 101 | + * @param int $precision |
|
| 102 | + * @return string |
|
| 103 | + * @see ConvertHelper::bytes2readable() |
|
| 104 | + */ |
|
| 105 | 105 | public function toString(int $precision=1) : string |
| 106 | 106 | { |
| 107 | 107 | return ConvertHelper::bytes2readable($this->bytes, $precision, $this->getBase()); |
| 108 | 108 | } |
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * Retrieves the detected number's base. |
|
| 112 | - * @return int The base number (1000 for Base 10, or 1024 for Base 2), or 0 if it is not valid. |
|
| 113 | - */ |
|
| 110 | + /** |
|
| 111 | + * Retrieves the detected number's base. |
|
| 112 | + * @return int The base number (1000 for Base 10, or 1024 for Base 2), or 0 if it is not valid. |
|
| 113 | + */ |
|
| 114 | 114 | public function getBase() : int |
| 115 | 115 | { |
| 116 | 116 | if($this->isValid()) { |
@@ -120,13 +120,13 @@ discard block |
||
| 120 | 120 | return 0; |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * Retrieves the detected storage size instance, if |
|
| 125 | - * the size string is valid. |
|
| 126 | - * |
|
| 127 | - * @return ConvertHelper_StorageSizeEnum_Size|NULL |
|
| 128 | - * @see ConvertHelper_StorageSizeEnum_Size |
|
| 129 | - */ |
|
| 123 | + /** |
|
| 124 | + * Retrieves the detected storage size instance, if |
|
| 125 | + * the size string is valid. |
|
| 126 | + * |
|
| 127 | + * @return ConvertHelper_StorageSizeEnum_Size|NULL |
|
| 128 | + * @see ConvertHelper_StorageSizeEnum_Size |
|
| 129 | + */ |
|
| 130 | 130 | public function getSizeDefinition() : ?ConvertHelper_StorageSizeEnum_Size |
| 131 | 131 | { |
| 132 | 132 | if($this->isValid()) { |
@@ -136,27 +136,27 @@ discard block |
||
| 136 | 136 | return null; |
| 137 | 137 | } |
| 138 | 138 | |
| 139 | - /** |
|
| 140 | - * Checks whether the size notation was valid and could be parsed |
|
| 141 | - * into a meaningful byte value. If this returns `false`, it is |
|
| 142 | - * possible to use the `getErrorXXX` methods to retrieve information |
|
| 143 | - * on what went wrong. |
|
| 144 | - * |
|
| 145 | - * @return bool |
|
| 146 | - * @see ConvertHelper_SizeNotation::getErrorMessage() |
|
| 147 | - * @see ConvertHelper_SizeNotation::getErrorCode() |
|
| 148 | - */ |
|
| 139 | + /** |
|
| 140 | + * Checks whether the size notation was valid and could be parsed |
|
| 141 | + * into a meaningful byte value. If this returns `false`, it is |
|
| 142 | + * possible to use the `getErrorXXX` methods to retrieve information |
|
| 143 | + * on what went wrong. |
|
| 144 | + * |
|
| 145 | + * @return bool |
|
| 146 | + * @see ConvertHelper_SizeNotation::getErrorMessage() |
|
| 147 | + * @see ConvertHelper_SizeNotation::getErrorCode() |
|
| 148 | + */ |
|
| 149 | 149 | public function isValid() : bool |
| 150 | 150 | { |
| 151 | 151 | return $this->valid; |
| 152 | 152 | } |
| 153 | 153 | |
| 154 | - /** |
|
| 155 | - * Retrieves the error message if the size notation validation failed. |
|
| 156 | - * |
|
| 157 | - * @return string |
|
| 158 | - * @see ConvertHelper_SizeNotation::getErrorCode() |
|
| 159 | - */ |
|
| 154 | + /** |
|
| 155 | + * Retrieves the error message if the size notation validation failed. |
|
| 156 | + * |
|
| 157 | + * @return string |
|
| 158 | + * @see ConvertHelper_SizeNotation::getErrorCode() |
|
| 159 | + */ |
|
| 160 | 160 | public function getErrorMessage() : string |
| 161 | 161 | { |
| 162 | 162 | return $this->errorMessage; |
@@ -201,12 +201,12 @@ discard block |
||
| 201 | 201 | ); |
| 202 | 202 | } |
| 203 | 203 | |
| 204 | - /** |
|
| 205 | - * Detects the units and the number in the size notation string. |
|
| 206 | - * Populates the `units` and `number` properties. |
|
| 207 | - * |
|
| 208 | - * @return bool Whether the string could be parsed successfully. |
|
| 209 | - */ |
|
| 204 | + /** |
|
| 205 | + * Detects the units and the number in the size notation string. |
|
| 206 | + * Populates the `units` and `number` properties. |
|
| 207 | + * |
|
| 208 | + * @return bool Whether the string could be parsed successfully. |
|
| 209 | + */ |
|
| 210 | 210 | protected function detectParts() : bool |
| 211 | 211 | { |
| 212 | 212 | $units = ConvertHelper_StorageSizeEnum::getSizeNames(); |
@@ -242,16 +242,16 @@ discard block |
||
| 242 | 242 | return true; |
| 243 | 243 | } |
| 244 | 244 | |
| 245 | - /** |
|
| 246 | - * If the validation fails, this is used to store the reason for retrieval later. |
|
| 247 | - * |
|
| 248 | - * @param string $message |
|
| 249 | - * @param int $code |
|
| 250 | - * |
|
| 251 | - * @see ConvertHelper_SizeNotation::isValid() |
|
| 252 | - * @see ConvertHelper_SizeNotation::getErrorMessage() |
|
| 253 | - * @see ConvertHelper_SizeNotation::getErrorCode() |
|
| 254 | - */ |
|
| 245 | + /** |
|
| 246 | + * If the validation fails, this is used to store the reason for retrieval later. |
|
| 247 | + * |
|
| 248 | + * @param string $message |
|
| 249 | + * @param int $code |
|
| 250 | + * |
|
| 251 | + * @see ConvertHelper_SizeNotation::isValid() |
|
| 252 | + * @see ConvertHelper_SizeNotation::getErrorMessage() |
|
| 253 | + * @see ConvertHelper_SizeNotation::getErrorCode() |
|
| 254 | + */ |
|
| 255 | 255 | protected function setError(string $message, int $code) : void |
| 256 | 256 | { |
| 257 | 257 | $this->valid = false; |
@@ -259,12 +259,12 @@ discard block |
||
| 259 | 259 | $this->errorCode = $code; |
| 260 | 260 | } |
| 261 | 261 | |
| 262 | - /** |
|
| 263 | - * Retrieves the error code, if the size notation validation failed. |
|
| 264 | - * |
|
| 265 | - * @return int |
|
| 266 | - * @see ConvertHelper_SizeNotation::getErrorMessage() |
|
| 267 | - */ |
|
| 262 | + /** |
|
| 263 | + * Retrieves the error code, if the size notation validation failed. |
|
| 264 | + * |
|
| 265 | + * @return int |
|
| 266 | + * @see ConvertHelper_SizeNotation::getErrorMessage() |
|
| 267 | + */ |
|
| 268 | 268 | public function getErrorCode() : int |
| 269 | 269 | { |
| 270 | 270 | return $this->errorCode; |
@@ -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 = array(); |
| 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() : void |
| 46 | 46 | { |
| 47 | 47 | if(!empty(self::$sizes)) { |
@@ -68,28 +68,28 @@ discard block |
||
| 68 | 68 | } |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - /** |
|
| 72 | - * Called whenever the application locale is changed, |
|
| 73 | - * to reset the size definitions so the labels get |
|
| 74 | - * translated to the new locale. |
|
| 75 | - */ |
|
| 71 | + /** |
|
| 72 | + * Called whenever the application locale is changed, |
|
| 73 | + * to reset the size definitions so the labels get |
|
| 74 | + * translated to the new locale. |
|
| 75 | + */ |
|
| 76 | 76 | public static function handle_localeChanged() : void |
| 77 | 77 | { |
| 78 | 78 | self::$sizes = array(); |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * Adds a storage size to the internal collection. |
|
| 83 | - * |
|
| 84 | - * @param string $name The lowercase size name, e.g. "kb", "mib" |
|
| 85 | - * @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. |
|
| 86 | - * @param int $exponent The multiplier of the base to get the byte value |
|
| 87 | - * @param string $suffix The localized short suffix, e.g. "KB", "MiB" |
|
| 88 | - * @param string $singular The localized singular label of the size, e.g. "Kilobyte". |
|
| 89 | - * @param string $plural The localized plural label of the size, e.g. "Kilobytes". |
|
| 90 | - * |
|
| 91 | - * @see https://en.m.wikipedia.org/wiki/Megabyte#Definitions |
|
| 92 | - */ |
|
| 81 | + /** |
|
| 82 | + * Adds a storage size to the internal collection. |
|
| 83 | + * |
|
| 84 | + * @param string $name The lowercase size name, e.g. "kb", "mib" |
|
| 85 | + * @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. |
|
| 86 | + * @param int $exponent The multiplier of the base to get the byte value |
|
| 87 | + * @param string $suffix The localized short suffix, e.g. "KB", "MiB" |
|
| 88 | + * @param string $singular The localized singular label of the size, e.g. "Kilobyte". |
|
| 89 | + * @param string $plural The localized plural label of the size, e.g. "Kilobytes". |
|
| 90 | + * |
|
| 91 | + * @see https://en.m.wikipedia.org/wiki/Megabyte#Definitions |
|
| 92 | + */ |
|
| 93 | 93 | protected static function addSize(string $name, int $base, int $exponent, string $suffix, string $singular, string $plural) : void |
| 94 | 94 | { |
| 95 | 95 | self::$sizes[$name] = new ConvertHelper_StorageSizeEnum_Size( |
@@ -102,11 +102,11 @@ discard block |
||
| 102 | 102 | ); |
| 103 | 103 | } |
| 104 | 104 | |
| 105 | - /** |
|
| 106 | - * Retrieves all known sizes. |
|
| 107 | - * |
|
| 108 | - * @return ConvertHelper_StorageSizeEnum_Size[] |
|
| 109 | - */ |
|
| 105 | + /** |
|
| 106 | + * Retrieves all known sizes. |
|
| 107 | + * |
|
| 108 | + * @return ConvertHelper_StorageSizeEnum_Size[] |
|
| 109 | + */ |
|
| 110 | 110 | public static function getSizes() : array |
| 111 | 111 | { |
| 112 | 112 | self::init(); |
@@ -114,15 +114,15 @@ discard block |
||
| 114 | 114 | return self::$sizes; |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * Retrieves a size definition instance by its name. |
|
| 119 | - * |
|
| 120 | - * @param string $name Case-insensitive. For example "kb", "MiB"... |
|
| 121 | - * @throws ConvertHelper_Exception |
|
| 122 | - * @return ConvertHelper_StorageSizeEnum_Size |
|
| 123 | - * |
|
| 124 | - * @see ConvertHelper_StorageSizeEnum::ERROR_UNKNOWN_UNIT_NAME |
|
| 125 | - */ |
|
| 117 | + /** |
|
| 118 | + * Retrieves a size definition instance by its name. |
|
| 119 | + * |
|
| 120 | + * @param string $name Case-insensitive. For example "kb", "MiB"... |
|
| 121 | + * @throws ConvertHelper_Exception |
|
| 122 | + * @return ConvertHelper_StorageSizeEnum_Size |
|
| 123 | + * |
|
| 124 | + * @see ConvertHelper_StorageSizeEnum::ERROR_UNKNOWN_UNIT_NAME |
|
| 125 | + */ |
|
| 126 | 126 | public static function getSizeByName(string $name) : ConvertHelper_StorageSizeEnum_Size |
| 127 | 127 | { |
| 128 | 128 | self::init(); |
@@ -144,10 +144,10 @@ discard block |
||
| 144 | 144 | ); |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | - /** |
|
| 148 | - * Retrieves a list of all size names, e.g. "mb", "kib" (lowercase). |
|
| 149 | - * @return array |
|
| 150 | - */ |
|
| 147 | + /** |
|
| 148 | + * Retrieves a list of all size names, e.g. "mb", "kib" (lowercase). |
|
| 149 | + * @return array |
|
| 150 | + */ |
|
| 151 | 151 | public static function getSizeNames() : array |
| 152 | 152 | { |
| 153 | 153 | self::init(); |
@@ -155,16 +155,16 @@ discard block |
||
| 155 | 155 | return array_keys(self::$sizes); |
| 156 | 156 | } |
| 157 | 157 | |
| 158 | - /** |
|
| 159 | - * Retrieves all available storage sizes for the specified |
|
| 160 | - * base value. |
|
| 161 | - * |
|
| 162 | - * @param int $base |
|
| 163 | - * @return ConvertHelper_StorageSizeEnum_Size[] |
|
| 164 | - * |
|
| 165 | - * @see ConvertHelper_StorageSizeEnum::BASE_10 |
|
| 166 | - * @see ConvertHelper_StorageSizeEnum::BASE_2 |
|
| 167 | - */ |
|
| 158 | + /** |
|
| 159 | + * Retrieves all available storage sizes for the specified |
|
| 160 | + * base value. |
|
| 161 | + * |
|
| 162 | + * @param int $base |
|
| 163 | + * @return ConvertHelper_StorageSizeEnum_Size[] |
|
| 164 | + * |
|
| 165 | + * @see ConvertHelper_StorageSizeEnum::BASE_10 |
|
| 166 | + * @see ConvertHelper_StorageSizeEnum::BASE_2 |
|
| 167 | + */ |
|
| 168 | 168 | public static function getSizesByBase(int $base) : array |
| 169 | 169 | { |
| 170 | 170 | self::init(); |