sebastianfeldmann /
phpbu
| 1 | <?php |
||
| 2 | namespace phpbu\App\Util; |
||
| 3 | |||
| 4 | use RuntimeException; |
||
| 5 | |||
| 6 | /** |
||
| 7 | * String utility class |
||
| 8 | * |
||
| 9 | * @package phpbu |
||
| 10 | * @subpackage Util |
||
| 11 | * @author Sebastian Feldmann <[email protected]> |
||
| 12 | * @copyright Sebastian Feldmann <[email protected]> |
||
| 13 | * @license https://opensource.org/licenses/MIT The MIT License (MIT) |
||
| 14 | * @link https://phpbu.de/ |
||
| 15 | * @since Class available since Release 1.0.0 |
||
| 16 | */ |
||
| 17 | class Str |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Converts a given value to a bool |
||
| 21 | * |
||
| 22 | * @param string $value |
||
| 23 | * @param bool $default |
||
| 24 | * @return bool |
||
| 25 | */ |
||
| 26 | 121 | public static function toBoolean($value, $default) : bool |
|
| 27 | { |
||
| 28 | 121 | if (is_bool($value)) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 29 | 1 | return $value; |
|
| 30 | } |
||
| 31 | 121 | if (strtolower($value) == 'false') { |
|
| 32 | 17 | return false; |
|
| 33 | 121 | } elseif (strtolower($value) == 'true') { |
|
| 34 | 33 | return true; |
|
| 35 | } |
||
| 36 | 114 | return $default; |
|
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Return given size in bytes |
||
| 41 | * Allowed units: |
||
| 42 | * B => byte |
||
| 43 | * K => kilo byte |
||
| 44 | * M => mega byte |
||
| 45 | * G => giga byte |
||
| 46 | * T => terra byte |
||
| 47 | * P => peta byte |
||
| 48 | * |
||
| 49 | * e.g. |
||
| 50 | * 1K => 1024 |
||
| 51 | * 2K => 2048 |
||
| 52 | * ... |
||
| 53 | * |
||
| 54 | * @param string $value |
||
| 55 | * @throws \RuntimeException |
||
| 56 | * @return int |
||
| 57 | */ |
||
| 58 | 11 | public static function toBytes(string $value) : int |
|
| 59 | { |
||
| 60 | 11 | if (!preg_match('#^[0-9]*[BKMGT]$#i', $value)) { |
|
| 61 | 1 | throw new RuntimeException('Invalid size value'); |
|
| 62 | } |
||
| 63 | 10 | $units = ['B' => 0, 'K' => 1, 'M' => 2, 'G' => 3, 'T' => 4, 'P' => 5]; |
|
| 64 | 10 | $unit = strtoupper(substr($value, -1)); |
|
| 65 | 10 | $number = intval(substr($value, 0, -1)); |
|
| 66 | |||
| 67 | 10 | return $number * pow(1024, $units[$unit]); |
|
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Return time in seconds for a given value |
||
| 72 | * Allowed units: |
||
| 73 | * S => second |
||
| 74 | * I => minute |
||
| 75 | * D => day |
||
| 76 | * W => week |
||
| 77 | * M => month |
||
| 78 | * Y => year |
||
| 79 | * |
||
| 80 | * e.g. |
||
| 81 | * 2I => 120 |
||
| 82 | * 10D => 864000 |
||
| 83 | * ... |
||
| 84 | * |
||
| 85 | * @param string $offset |
||
| 86 | * @throws \RuntimeException |
||
| 87 | * @return int |
||
| 88 | */ |
||
| 89 | 9 | public static function toTime(string $offset) : int |
|
| 90 | { |
||
| 91 | 9 | if (!preg_match('#^[1-9]+[0-9]*[SIHDWMY]$#i', $offset)) { |
|
| 92 | 3 | throw new RuntimeException(sprintf('Invalid value for offset: %s', $offset)); |
|
| 93 | } |
||
| 94 | 6 | $units = ['S' => 1, 'I' => 60, 'H' => 3600, 'D' => 86400, 'W' => 604800, 'M' => 2678400, 'Y' => 31536000]; |
|
| 95 | 6 | $unit = strtoupper(substr($offset, -1)); |
|
| 96 | 6 | $number = intval(substr($offset, 0, -1)); |
|
| 97 | |||
| 98 | 6 | return $number * $units[$unit]; |
|
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Pads all given strings to given length. |
||
| 103 | * |
||
| 104 | * @param array $strings |
||
| 105 | * @param int $length |
||
| 106 | * @param string $pad |
||
| 107 | * @param int $mode |
||
| 108 | * @return array |
||
| 109 | */ |
||
| 110 | 1 | public static function padAll(array $strings, int $length, string $pad = ' ', int $mode = STR_PAD_LEFT) : array |
|
| 111 | { |
||
| 112 | 1 | $result = []; |
|
| 113 | 1 | foreach ($strings as $key => $s) { |
|
| 114 | 1 | $result[$key] = str_pad($s, $length, $pad, $mode); |
|
| 115 | } |
||
| 116 | 1 | return $result; |
|
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Explodes string to array but empty string results in empty array not array with empty string in it |
||
| 121 | * |
||
| 122 | * @param string $separated |
||
| 123 | * @param string $separator |
||
| 124 | * @param bool $trim |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | 70 | public static function toList(string $separated, string $separator = ',', bool $trim = true) : array |
|
| 128 | { |
||
| 129 | 70 | $list = empty($separated) ? [] : explode($separator, $separated); |
|
| 130 | 70 | if ($trim) { |
|
| 131 | 69 | $list = array_map('trim', $list); |
|
| 132 | } |
||
| 133 | 70 | return $list; |
|
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Appends a plural "s" or "'s". |
||
| 138 | * |
||
| 139 | * @param string $subject |
||
| 140 | * @param int $amount |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | 6 | public static function appendPluralS(string $subject, int $amount) : string |
|
| 144 | { |
||
| 145 | 6 | return $subject . ($amount == 1 ? '' : (substr($subject, -1) == 's' ? '\'s' : 's')); |
|
| 146 | } |
||
| 147 | } |
||
| 148 |