@@ -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,10 +106,10 @@ discard block |
||
| 106 | 106 | ); |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * Retrieves all known sizes. |
|
| 111 | - * @return \AppUtils\ConvertHelper_StorageSizeEnum_Size |
|
| 112 | - */ |
|
| 109 | + /** |
|
| 110 | + * Retrieves all known sizes. |
|
| 111 | + * @return \AppUtils\ConvertHelper_StorageSizeEnum_Size |
|
| 112 | + */ |
|
| 113 | 113 | public static function getSizes() |
| 114 | 114 | { |
| 115 | 115 | self::init(); |
@@ -117,15 +117,15 @@ discard block |
||
| 117 | 117 | return self::$sizes; |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * Retrieves a size definition instance by its name. |
|
| 122 | - * |
|
| 123 | - * @param string $name Case insensitive. For example "kb", "MiB"... |
|
| 124 | - * @throws ConvertHelper_Exception |
|
| 125 | - * @return ConvertHelper_StorageSizeEnum_Size |
|
| 126 | - * |
|
| 127 | - * @see ConvertHelper_StorageSizeEnum::ERROR_UNKNOWN_UNIT_NAME |
|
| 128 | - */ |
|
| 120 | + /** |
|
| 121 | + * Retrieves a size definition instance by its name. |
|
| 122 | + * |
|
| 123 | + * @param string $name Case insensitive. For example "kb", "MiB"... |
|
| 124 | + * @throws ConvertHelper_Exception |
|
| 125 | + * @return ConvertHelper_StorageSizeEnum_Size |
|
| 126 | + * |
|
| 127 | + * @see ConvertHelper_StorageSizeEnum::ERROR_UNKNOWN_UNIT_NAME |
|
| 128 | + */ |
|
| 129 | 129 | public static function getSizeByName(string $name) : ConvertHelper_StorageSizeEnum_Size |
| 130 | 130 | { |
| 131 | 131 | self::init(); |
@@ -147,10 +147,10 @@ discard block |
||
| 147 | 147 | ); |
| 148 | 148 | } |
| 149 | 149 | |
| 150 | - /** |
|
| 151 | - * Retrieves a list of all size names, e.g. "mb", "kib" (lowercase). |
|
| 152 | - * @return array |
|
| 153 | - */ |
|
| 150 | + /** |
|
| 151 | + * Retrieves a list of all size names, e.g. "mb", "kib" (lowercase). |
|
| 152 | + * @return array |
|
| 153 | + */ |
|
| 154 | 154 | public static function getSizeNames() : array |
| 155 | 155 | { |
| 156 | 156 | self::init(); |
@@ -158,16 +158,16 @@ discard block |
||
| 158 | 158 | return array_keys(self::$sizes); |
| 159 | 159 | } |
| 160 | 160 | |
| 161 | - /** |
|
| 162 | - * Retrieves all available storage sizes for the specified |
|
| 163 | - * base value. |
|
| 164 | - * |
|
| 165 | - * @param int $base |
|
| 166 | - * @return \AppUtils\ConvertHelper_StorageSizeEnum_Size[] |
|
| 167 | - * |
|
| 168 | - * @see ConvertHelper_StorageSizeEnum::BASE_10 |
|
| 169 | - * @see ConvertHelper_StorageSizeEnum::BASE_2 |
|
| 170 | - */ |
|
| 161 | + /** |
|
| 162 | + * Retrieves all available storage sizes for the specified |
|
| 163 | + * base value. |
|
| 164 | + * |
|
| 165 | + * @param int $base |
|
| 166 | + * @return \AppUtils\ConvertHelper_StorageSizeEnum_Size[] |
|
| 167 | + * |
|
| 168 | + * @see ConvertHelper_StorageSizeEnum::BASE_10 |
|
| 169 | + * @see ConvertHelper_StorageSizeEnum::BASE_2 |
|
| 170 | + */ |
|
| 171 | 171 | public static function getSizesByBase(int $base) |
| 172 | 172 | { |
| 173 | 173 | self::init(); |
@@ -44,7 +44,7 @@ discard block |
||
| 44 | 44 | */ |
| 45 | 45 | protected static function init() |
| 46 | 46 | { |
| 47 | - if(isset(self::$sizes)) { |
|
| 47 | + if (isset(self::$sizes)) { |
|
| 48 | 48 | return; |
| 49 | 49 | } |
| 50 | 50 | |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | |
| 65 | 65 | self::addSize('b', 1, 1, t('B'), t('Byte'), t('Bytes')); |
| 66 | 66 | |
| 67 | - if(class_exists('AppLocalize\Localization')) |
|
| 67 | + if (class_exists('AppLocalize\Localization')) |
|
| 68 | 68 | { |
| 69 | 69 | \AppLocalize\Localization::onLocaleChanged(array(self::class, 'handle_localeChanged')); |
| 70 | 70 | } |
@@ -132,7 +132,7 @@ discard block |
||
| 132 | 132 | |
| 133 | 133 | $name = strtolower($name); |
| 134 | 134 | |
| 135 | - if(isset(self::$sizes[$name])) { |
|
| 135 | + if (isset(self::$sizes[$name])) { |
|
| 136 | 136 | return self::$sizes[$name]; |
| 137 | 137 | } |
| 138 | 138 | |
@@ -174,9 +174,9 @@ discard block |
||
| 174 | 174 | |
| 175 | 175 | $result = array(); |
| 176 | 176 | |
| 177 | - foreach(self::$sizes as $size) |
|
| 177 | + foreach (self::$sizes as $size) |
|
| 178 | 178 | { |
| 179 | - if($size->getBase() === $base) { |
|
| 179 | + if ($size->getBase() === $base) { |
|
| 180 | 180 | $result[] = $size; |
| 181 | 181 | } |
| 182 | 182 | } |
@@ -31,46 +31,46 @@ discard block |
||
| 31 | 31 | |
| 32 | 32 | const VALIDATION_ERROR_NEGATIVE_VALUE = 43803; |
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * @var ConvertHelper_StorageSizeEnum_Size |
|
| 36 | - */ |
|
| 34 | + /** |
|
| 35 | + * @var ConvertHelper_StorageSizeEnum_Size |
|
| 36 | + */ |
|
| 37 | 37 | protected $size; |
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @var integer |
|
| 41 | - */ |
|
| 39 | + /** |
|
| 40 | + * @var integer |
|
| 41 | + */ |
|
| 42 | 42 | protected $bytes = 0; |
| 43 | 43 | |
| 44 | - /** |
|
| 45 | - * @var bool |
|
| 46 | - */ |
|
| 44 | + /** |
|
| 45 | + * @var bool |
|
| 46 | + */ |
|
| 47 | 47 | protected $valid = true; |
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * @var string |
|
| 51 | - */ |
|
| 49 | + /** |
|
| 50 | + * @var string |
|
| 51 | + */ |
|
| 52 | 52 | protected $units = null; |
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * @var string |
|
| 56 | - */ |
|
| 54 | + /** |
|
| 55 | + * @var string |
|
| 56 | + */ |
|
| 57 | 57 | protected $number = ''; |
| 58 | 58 | |
| 59 | - /** |
|
| 60 | - * @var string |
|
| 61 | - */ |
|
| 59 | + /** |
|
| 60 | + * @var string |
|
| 61 | + */ |
|
| 62 | 62 | protected $errorMessage = ''; |
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * @var int |
|
| 66 | - */ |
|
| 64 | + /** |
|
| 65 | + * @var int |
|
| 66 | + */ |
|
| 67 | 67 | protected $errorCode = 0; |
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Create a new instance for the specified size string. |
|
| 71 | - * |
|
| 72 | - * @param string $size 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". |
|
| 73 | - */ |
|
| 69 | + /** |
|
| 70 | + * Create a new instance for the specified size string. |
|
| 71 | + * |
|
| 72 | + * @param string $size 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". |
|
| 73 | + */ |
|
| 74 | 74 | public function __construct(string $size) |
| 75 | 75 | { |
| 76 | 76 | $this->size = $this->cleanString($size); |
@@ -78,31 +78,31 @@ discard block |
||
| 78 | 78 | $this->convert(); |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * Gets the amount of bytes contained in the size notation. |
|
| 83 | - * @return int |
|
| 84 | - */ |
|
| 81 | + /** |
|
| 82 | + * Gets the amount of bytes contained in the size notation. |
|
| 83 | + * @return int |
|
| 84 | + */ |
|
| 85 | 85 | public function toBytes() : int |
| 86 | 86 | { |
| 87 | 87 | return $this->bytes; |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * Converts the size notation to a human readable string, e.g. "50 MB". |
|
| 92 | - * |
|
| 93 | - * @param int $precision |
|
| 94 | - * @return string |
|
| 95 | - * @see ConvertHelper::bytes2readable() |
|
| 96 | - */ |
|
| 90 | + /** |
|
| 91 | + * Converts the size notation to a human readable string, e.g. "50 MB". |
|
| 92 | + * |
|
| 93 | + * @param int $precision |
|
| 94 | + * @return string |
|
| 95 | + * @see ConvertHelper::bytes2readable() |
|
| 96 | + */ |
|
| 97 | 97 | public function toString(int $precision=1) : string |
| 98 | 98 | { |
| 99 | 99 | return ConvertHelper::bytes2readable($this->bytes, $precision, $this->getBase()); |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Retrieves the detected number's base. |
|
| 104 | - * @return int The base number (1000 for Base 10, or 1024 for Base 2), or 0 if it is not valid. |
|
| 105 | - */ |
|
| 102 | + /** |
|
| 103 | + * Retrieves the detected number's base. |
|
| 104 | + * @return int The base number (1000 for Base 10, or 1024 for Base 2), or 0 if it is not valid. |
|
| 105 | + */ |
|
| 106 | 106 | public function getBase() : int |
| 107 | 107 | { |
| 108 | 108 | if(!$this->isValid()) { |
@@ -112,27 +112,27 @@ discard block |
||
| 112 | 112 | return $this->size->getBase(); |
| 113 | 113 | } |
| 114 | 114 | |
| 115 | - /** |
|
| 116 | - * Checks whether the size notation was valid and could be parsed |
|
| 117 | - * into a meaningful byte value. If this returns `false`, it is |
|
| 118 | - * possible to use the `getErrorXXX` methods to retrieve information |
|
| 119 | - * on what went wrong. |
|
| 120 | - * |
|
| 121 | - * @return bool |
|
| 122 | - * @see ConvertHelper_SizeNotation::getErrorMessage() |
|
| 123 | - * @see ConvertHelper_SizeNotation::getErrorCode() |
|
| 124 | - */ |
|
| 115 | + /** |
|
| 116 | + * Checks whether the size notation was valid and could be parsed |
|
| 117 | + * into a meaningful byte value. If this returns `false`, it is |
|
| 118 | + * possible to use the `getErrorXXX` methods to retrieve information |
|
| 119 | + * on what went wrong. |
|
| 120 | + * |
|
| 121 | + * @return bool |
|
| 122 | + * @see ConvertHelper_SizeNotation::getErrorMessage() |
|
| 123 | + * @see ConvertHelper_SizeNotation::getErrorCode() |
|
| 124 | + */ |
|
| 125 | 125 | public function isValid() : bool |
| 126 | 126 | { |
| 127 | 127 | return $this->valid; |
| 128 | 128 | } |
| 129 | 129 | |
| 130 | - /** |
|
| 131 | - * Retrieves the error message if the size notation validation failed. |
|
| 132 | - * |
|
| 133 | - * @return string |
|
| 134 | - * @see ConvertHelper_SizeNotation::getErrorCode() |
|
| 135 | - */ |
|
| 130 | + /** |
|
| 131 | + * Retrieves the error message if the size notation validation failed. |
|
| 132 | + * |
|
| 133 | + * @return string |
|
| 134 | + * @see ConvertHelper_SizeNotation::getErrorCode() |
|
| 135 | + */ |
|
| 136 | 136 | public function getErrorMessage() : string |
| 137 | 137 | { |
| 138 | 138 | return $this->errorMessage; |
@@ -179,12 +179,12 @@ discard block |
||
| 179 | 179 | ); |
| 180 | 180 | } |
| 181 | 181 | |
| 182 | - /** |
|
| 183 | - * Detects the units and the number in the size notation string. |
|
| 184 | - * Populates the `units` and `number` properties. |
|
| 185 | - * |
|
| 186 | - * @return bool Whether the string could be parsed successfully. |
|
| 187 | - */ |
|
| 182 | + /** |
|
| 183 | + * Detects the units and the number in the size notation string. |
|
| 184 | + * Populates the `units` and `number` properties. |
|
| 185 | + * |
|
| 186 | + * @return bool Whether the string could be parsed successfully. |
|
| 187 | + */ |
|
| 188 | 188 | protected function detectParts() : bool |
| 189 | 189 | { |
| 190 | 190 | $units = ConvertHelper_StorageSizeEnum::getSizeNames(); |
@@ -220,16 +220,16 @@ discard block |
||
| 220 | 220 | return true; |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | - /** |
|
| 224 | - * If the validation fails, this is used to store the reason for retrieval later. |
|
| 225 | - * |
|
| 226 | - * @param string $message |
|
| 227 | - * @param int $code |
|
| 228 | - * |
|
| 229 | - * @see ConvertHelper_SizeNotation::isValid() |
|
| 230 | - * @see ConvertHelper_SizeNotation::getErrorMessage() |
|
| 231 | - * @see ConvertHelper_SizeNotation::getErrorCode() |
|
| 232 | - */ |
|
| 223 | + /** |
|
| 224 | + * If the validation fails, this is used to store the reason for retrieval later. |
|
| 225 | + * |
|
| 226 | + * @param string $message |
|
| 227 | + * @param int $code |
|
| 228 | + * |
|
| 229 | + * @see ConvertHelper_SizeNotation::isValid() |
|
| 230 | + * @see ConvertHelper_SizeNotation::getErrorMessage() |
|
| 231 | + * @see ConvertHelper_SizeNotation::getErrorCode() |
|
| 232 | + */ |
|
| 233 | 233 | protected function setError(string $message, int $code) : void |
| 234 | 234 | { |
| 235 | 235 | $this->valid = false; |
@@ -237,12 +237,12 @@ discard block |
||
| 237 | 237 | $this->errorCode = $code; |
| 238 | 238 | } |
| 239 | 239 | |
| 240 | - /** |
|
| 241 | - * Retrieves the error code, if the size notation validation failed. |
|
| 242 | - * |
|
| 243 | - * @return int |
|
| 244 | - * @see ConvertHelper_SizeNotation::getErrorMessage() |
|
| 245 | - */ |
|
| 240 | + /** |
|
| 241 | + * Retrieves the error code, if the size notation validation failed. |
|
| 242 | + * |
|
| 243 | + * @return int |
|
| 244 | + * @see ConvertHelper_SizeNotation::getErrorMessage() |
|
| 245 | + */ |
|
| 246 | 246 | public function getErrorCode() : int |
| 247 | 247 | { |
| 248 | 248 | return $this->errorCode; |