| 1 |
<?php |
| 2 |
|
| 3 |
namespace React\Http\Io; |
| 4 |
|
| 5 |
/** |
| 6 |
* @internal |
| 7 |
*/ |
| 8 |
final class IniUtil |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Convert a ini like size to a numeric size in bytes. |
| 12 |
* |
| 13 |
* @param string $size |
| 14 |
* @return int |
| 15 |
*/ |
| 16 |
public static function iniSizeToBytes($size) |
| 17 |
{ |
| 18 |
if (\is_numeric($size)) { |
| 19 |
return (int)$size; |
| 20 |
} |
| 21 |
|
| 22 |
$suffix = \strtoupper(\substr($size, -1)); |
| 23 |
$strippedSize = \substr($size, 0, -1); |
| 24 |
|
| 25 |
if (!\is_numeric($strippedSize)) { |
| 26 |
throw new \InvalidArgumentException("$size is not a valid ini size"); |
| 27 |
} |
| 28 |
|
| 29 |
if ($strippedSize <= 0) { |
| 30 |
throw new \InvalidArgumentException("Expect $size to be higher isn't zero or lower"); |
| 31 |
} |
| 32 |
|
| 33 |
if ($suffix === 'K') { |
| 34 |
return $strippedSize * 1024; |
| 35 |
} |
| 36 |
if ($suffix === 'M') { |
| 37 |
return $strippedSize * 1024 * 1024; |
| 38 |
} |
| 39 |
if ($suffix === 'G') { |
| 40 |
return $strippedSize * 1024 * 1024 * 1024; |
| 41 |
} |
| 42 |
if ($suffix === 'T') { |
| 43 |
return $strippedSize * 1024 * 1024 * 1024 * 1024; |
| 44 |
} |
| 45 |
|
| 46 |
return (int)$size; |
| 47 |
} |
| 48 |
} |