Mistralys /
application-utils
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * File containing the {@see ConvertHelper_StorageSizeEnum} class. |
||
| 4 | * |
||
| 5 | * @package Application Utils |
||
| 6 | * @subpackage ConvertHelper |
||
| 7 | * @see ConvertHelper_StorageSizeEnum |
||
| 8 | */ |
||
| 9 | |||
| 10 | declare(strict_types=1); |
||
| 11 | |||
| 12 | namespace AppUtils; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Static class used to handle possible storage sizes, |
||
| 16 | * like Kilobytes, Megabytes and the like. Offers an easy-to-use |
||
| 17 | * interface to access information on these sizes, |
||
| 18 | * and to translate their labels to the application locale. |
||
| 19 | * |
||
| 20 | * It supports both Base 10 and Base 2 sizes. |
||
| 21 | * |
||
| 22 | * @package Application Utils |
||
| 23 | * @subpackage ConvertHelper |
||
| 24 | * @author Sebastian Mordziol <[email protected]> |
||
| 25 | */ |
||
| 26 | class ConvertHelper_StorageSizeEnum |
||
| 27 | { |
||
| 28 | public const ERROR_UNKNOWN_UNIT_NAME = 43901; |
||
| 29 | |||
| 30 | public const BASE_10 = 1000; |
||
| 31 | public const BASE_2 = 1024; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var array<string,ConvertHelper_StorageSizeEnum_Size> |
||
| 35 | */ |
||
| 36 | protected static $sizes = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Initializes the supported unit notations, and |
||
| 40 | * how they are supposed to be calculated. |
||
| 41 | * |
||
| 42 | * @see ConvertHelper_SizeNotation::parseSize() |
||
| 43 | */ |
||
| 44 | protected static function init() : void |
||
| 45 | { |
||
| 46 | if(!empty(self::$sizes)) { |
||
| 47 | return; |
||
| 48 | } |
||
| 49 | |||
| 50 | self::addSize('kib', self::BASE_2, 1, t('KiB'), t('Kibibyte'), t('Kibibytes')); |
||
| 51 | self::addSize('mib', self::BASE_2, 2, t('MiB'), t('Mebibyte'), t('Mebibytes')); |
||
| 52 | self::addSize('gib', self::BASE_2, 3, t('GiB'), t('Gibibyte'), t('Gibibytes')); |
||
| 53 | self::addSize('tib', self::BASE_2, 4, t('TiB'), t('Tebibyte'), t('Tebibytes')); |
||
| 54 | self::addSize('pib', self::BASE_2, 5, t('PiB'), t('Pebibyte'), t('Pebibytes')); |
||
| 55 | |||
| 56 | self::addSize('kb', self::BASE_10, 1, t('KB'), t('Kilobyte'), t('Kilobytes')); |
||
| 57 | self::addSize('mb', self::BASE_10, 2, t('MB'), t('Megabyte'), t('Megabytes')); |
||
| 58 | self::addSize('gb', self::BASE_10, 3, t('GB'), t('Gigabyte'), t('Gigabytes')); |
||
| 59 | self::addSize('tb', self::BASE_10, 4, t('TB'), t('Terabyte'), t('Terabytes')); |
||
| 60 | self::addSize('pb', self::BASE_10, 5, t('PB'), t('Petabyte'), t('Petabytes')); |
||
| 61 | |||
| 62 | self::addSize('b', 1, 1, t('B'), t('Byte'), t('Bytes')); |
||
| 63 | |||
| 64 | if(class_exists('AppLocalize\Localization')) |
||
| 65 | { |
||
| 66 | \AppLocalize\Localization::onLocaleChanged(array(self::class, 'handle_localeChanged')); |
||
|
0 ignored issues
–
show
|
|||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Called whenever the application locale is changed, |
||
| 72 | * to reset the size definitions so the labels get |
||
| 73 | * translated to the new locale. |
||
| 74 | */ |
||
| 75 | public static function handle_localeChanged() : void |
||
| 76 | { |
||
| 77 | self::$sizes = array(); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Adds a storage size to the internal collection. |
||
| 82 | * |
||
| 83 | * @param string $name The lowercase size name, e.g. "kb", "mib" |
||
| 84 | * @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. |
||
| 85 | * @param int $exponent The multiplier of the base to get the byte value |
||
| 86 | * @param string $suffix The localized short suffix, e.g. "KB", "MiB" |
||
| 87 | * @param string $singular The localized singular label of the size, e.g. "Kilobyte". |
||
| 88 | * @param string $plural The localized plural label of the size, e.g. "Kilobytes". |
||
| 89 | * |
||
| 90 | * @see https://en.m.wikipedia.org/wiki/Megabyte#Definitions |
||
| 91 | */ |
||
| 92 | protected static function addSize(string $name, int $base, int $exponent, string $suffix, string $singular, string $plural) : void |
||
| 93 | { |
||
| 94 | self::$sizes[$name] = new ConvertHelper_StorageSizeEnum_Size( |
||
| 95 | $name, |
||
| 96 | $base, |
||
| 97 | $exponent, |
||
| 98 | $suffix, |
||
| 99 | $singular, |
||
| 100 | $plural |
||
| 101 | ); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Retrieves all known sizes. |
||
| 106 | * |
||
| 107 | * @return ConvertHelper_StorageSizeEnum_Size[] |
||
| 108 | */ |
||
| 109 | public static function getSizes() : array |
||
| 110 | { |
||
| 111 | self::init(); |
||
| 112 | |||
| 113 | return array_values(self::$sizes); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Retrieves a size definition instance by its name. |
||
| 118 | * |
||
| 119 | * @param string $name Case-insensitive. For example "kb", "MiB"... |
||
| 120 | * @throws ConvertHelper_Exception |
||
| 121 | * @return ConvertHelper_StorageSizeEnum_Size |
||
| 122 | * |
||
| 123 | * @see ConvertHelper_StorageSizeEnum::ERROR_UNKNOWN_UNIT_NAME |
||
| 124 | */ |
||
| 125 | public static function getSizeByName(string $name) : ConvertHelper_StorageSizeEnum_Size |
||
| 126 | { |
||
| 127 | self::init(); |
||
| 128 | |||
| 129 | $name = strtolower($name); |
||
| 130 | |||
| 131 | if(isset(self::$sizes[$name])) { |
||
| 132 | return self::$sizes[$name]; |
||
| 133 | } |
||
| 134 | |||
| 135 | throw new ConvertHelper_Exception( |
||
| 136 | 'Unknown storage size name '.$name.'.', |
||
| 137 | sprintf( |
||
| 138 | 'The storage size name [%s] does not exist. Available names are: [%s].', |
||
| 139 | $name, |
||
| 140 | implode(', ', self::getSizeNames()) |
||
| 141 | ), |
||
| 142 | self::ERROR_UNKNOWN_UNIT_NAME |
||
| 143 | ); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Retrieves a list of all size names, e.g. "mb", "kib" (lowercase). |
||
| 148 | * @return string[] |
||
| 149 | */ |
||
| 150 | public static function getSizeNames() : array |
||
| 151 | { |
||
| 152 | self::init(); |
||
| 153 | |||
| 154 | return array_keys(self::$sizes); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Retrieves all available storage sizes for the specified |
||
| 159 | * base value. |
||
| 160 | * |
||
| 161 | * @param int $base |
||
| 162 | * @return ConvertHelper_StorageSizeEnum_Size[] |
||
| 163 | * |
||
| 164 | * @see ConvertHelper_StorageSizeEnum::BASE_10 |
||
| 165 | * @see ConvertHelper_StorageSizeEnum::BASE_2 |
||
| 166 | */ |
||
| 167 | public static function getSizesByBase(int $base) : array |
||
| 168 | { |
||
| 169 | self::init(); |
||
| 170 | |||
| 171 | $result = array(); |
||
| 172 | |||
| 173 | foreach(self::$sizes as $size) |
||
| 174 | { |
||
| 175 | if($size->getBase() === $base) { |
||
| 176 | $result[] = $size; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | return $result; |
||
| 181 | } |
||
| 182 | } |
||
| 183 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths