Resources
2 weeks ago
LICENSE
2 weeks ago
Php83.php
2 weeks ago
README.md
2 weeks ago
bootstrap.php
2 weeks ago
bootstrap72.php
2 weeks ago
bootstrap81.php
2 weeks ago
composer.json
2 weeks ago
Php83.php
206 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | |
| 12 | namespace Symfony\Polyfill\Php83; |
| 13 | |
| 14 | /** |
| 15 | * @author Ion Bazan <ion.bazan@gmail.com> |
| 16 | * @author Pierre Ambroise <pierre27.ambroise@gmail.com> |
| 17 | * |
| 18 | * @internal |
| 19 | */ |
| 20 | final class Php83 |
| 21 | { |
| 22 | private const JSON_MAX_DEPTH = 0x7FFFFFFF; // see https://www.php.net/manual/en/function.json-decode.php |
| 23 | |
| 24 | public static function json_validate(string $json, int $depth = 512, int $flags = 0): bool |
| 25 | { |
| 26 | if (0 !== $flags && \defined('JSON_INVALID_UTF8_IGNORE') && \JSON_INVALID_UTF8_IGNORE !== $flags) { |
| 27 | throw new \ValueError('json_validate(): Argument #3 ($flags) must be a valid flag (allowed flags: JSON_INVALID_UTF8_IGNORE)'); |
| 28 | } |
| 29 | |
| 30 | if ($depth <= 0) { |
| 31 | throw new \ValueError('json_validate(): Argument #2 ($depth) must be greater than 0'); |
| 32 | } |
| 33 | |
| 34 | if ($depth > self::JSON_MAX_DEPTH) { |
| 35 | throw new \ValueError(\sprintf('json_validate(): Argument #2 ($depth) must be less than %d', self::JSON_MAX_DEPTH)); |
| 36 | } |
| 37 | |
| 38 | json_decode($json, true, $depth, $flags); |
| 39 | |
| 40 | return \JSON_ERROR_NONE === json_last_error(); |
| 41 | } |
| 42 | |
| 43 | /** @return string|false */ |
| 44 | public static function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $pad_type = \STR_PAD_RIGHT, ?string $encoding = null) |
| 45 | { |
| 46 | if (null === $encoding) { |
| 47 | $encoding = mb_internal_encoding(); |
| 48 | } |
| 49 | |
| 50 | $errorToTrigger = null; |
| 51 | try { |
| 52 | if (!@mb_check_encoding('', $encoding)) { |
| 53 | $errorToTrigger = \sprintf('mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "%s" given', $encoding); |
| 54 | } |
| 55 | } catch (\ValueError $e) { |
| 56 | $errorToTrigger = \sprintf('mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "%s" given', $encoding); |
| 57 | } |
| 58 | |
| 59 | if (null === $errorToTrigger && mb_strlen($pad_string, $encoding) <= 0) { |
| 60 | $errorToTrigger = 'mb_str_pad(): Argument #3 ($pad_string) must be a non-empty string'; |
| 61 | } |
| 62 | |
| 63 | if (null === $errorToTrigger && !\in_array($pad_type, [\STR_PAD_RIGHT, \STR_PAD_LEFT, \STR_PAD_BOTH], true)) { |
| 64 | $errorToTrigger = 'mb_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH'; |
| 65 | } |
| 66 | |
| 67 | if (null !== $errorToTrigger) { |
| 68 | if (80000 > \PHP_VERSION_ID) { |
| 69 | trigger_error($errorToTrigger, \E_USER_WARNING); |
| 70 | |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | throw new \ValueError($errorToTrigger); |
| 75 | } |
| 76 | |
| 77 | $paddingRequired = $length - mb_strlen($string, $encoding); |
| 78 | |
| 79 | if ($paddingRequired < 1) { |
| 80 | return $string; |
| 81 | } |
| 82 | |
| 83 | switch ($pad_type) { |
| 84 | case \STR_PAD_LEFT: |
| 85 | return mb_substr(str_repeat($pad_string, $paddingRequired), 0, $paddingRequired, $encoding).$string; |
| 86 | case \STR_PAD_RIGHT: |
| 87 | return $string.mb_substr(str_repeat($pad_string, $paddingRequired), 0, $paddingRequired, $encoding); |
| 88 | default: |
| 89 | $leftPaddingLength = floor($paddingRequired / 2); |
| 90 | $rightPaddingLength = $paddingRequired - $leftPaddingLength; |
| 91 | |
| 92 | return mb_substr(str_repeat($pad_string, $leftPaddingLength), 0, $leftPaddingLength, $encoding).$string.mb_substr(str_repeat($pad_string, $rightPaddingLength), 0, $rightPaddingLength, $encoding); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | public static function str_increment(string $string): string |
| 97 | { |
| 98 | if ('' === $string) { |
| 99 | throw new \ValueError('str_increment(): Argument #1 ($string) cannot be empty'); |
| 100 | } |
| 101 | |
| 102 | if (!preg_match('/^[a-zA-Z0-9]+$/', $string)) { |
| 103 | throw new \ValueError('str_increment(): Argument #1 ($string) must be composed only of alphanumeric ASCII characters'); |
| 104 | } |
| 105 | |
| 106 | for ($i = \strlen($string) - 1; $i >= 0; --$i) { |
| 107 | $char = $string[$i]; |
| 108 | |
| 109 | if ('z' === $char) { |
| 110 | $string[$i] = 'a'; |
| 111 | continue; |
| 112 | } |
| 113 | if ('Z' === $char) { |
| 114 | $string[$i] = 'A'; |
| 115 | continue; |
| 116 | } |
| 117 | if ('9' === $char) { |
| 118 | $string[$i] = '0'; |
| 119 | continue; |
| 120 | } |
| 121 | |
| 122 | $string[$i] = \chr(\ord($char) + 1); |
| 123 | |
| 124 | return $string; |
| 125 | } |
| 126 | |
| 127 | switch ($string[0]) { |
| 128 | case 'a': return 'a'.$string; |
| 129 | case 'A': return 'A'.$string; |
| 130 | } |
| 131 | |
| 132 | return '1'.$string; |
| 133 | } |
| 134 | |
| 135 | public static function str_decrement(string $string): string |
| 136 | { |
| 137 | if ('' === $string) { |
| 138 | throw new \ValueError('str_decrement(): Argument #1 ($string) cannot be empty'); |
| 139 | } |
| 140 | |
| 141 | if (!preg_match('/^[a-zA-Z0-9]+$/', $string)) { |
| 142 | throw new \ValueError('str_decrement(): Argument #1 ($string) must be composed only of alphanumeric ASCII characters'); |
| 143 | } |
| 144 | |
| 145 | if (preg_match('/\A(?:0[aA0]?|[aA])\z/', $string)) { |
| 146 | throw new \ValueError(\sprintf('str_decrement(): Argument #1 ($string) "%s" is out of decrement range', $string)); |
| 147 | } |
| 148 | |
| 149 | if (!\in_array(substr($string, -1), ['A', 'a', '0'], true)) { |
| 150 | return implode('', \array_slice(str_split($string), 0, -1)).\chr(\ord(substr($string, -1)) - 1); |
| 151 | } |
| 152 | |
| 153 | $carry = ''; |
| 154 | $decremented = ''; |
| 155 | |
| 156 | for ($i = \strlen($string) - 1; $i >= 0; --$i) { |
| 157 | $char = $string[$i]; |
| 158 | |
| 159 | switch ($char) { |
| 160 | case 'A': |
| 161 | if ('' !== $carry) { |
| 162 | $decremented = $carry.$decremented; |
| 163 | $carry = ''; |
| 164 | } |
| 165 | $carry = 'Z'; |
| 166 | |
| 167 | break; |
| 168 | case 'a': |
| 169 | if ('' !== $carry) { |
| 170 | $decremented = $carry.$decremented; |
| 171 | $carry = ''; |
| 172 | } |
| 173 | $carry = 'z'; |
| 174 | |
| 175 | break; |
| 176 | case '0': |
| 177 | if ('' !== $carry) { |
| 178 | $decremented = $carry.$decremented; |
| 179 | $carry = ''; |
| 180 | } |
| 181 | $carry = '9'; |
| 182 | |
| 183 | break; |
| 184 | case '1': |
| 185 | if ('' !== $carry) { |
| 186 | $decremented = $carry.$decremented; |
| 187 | $carry = ''; |
| 188 | } |
| 189 | |
| 190 | break; |
| 191 | default: |
| 192 | if ('' !== $carry) { |
| 193 | $decremented = $carry.$decremented; |
| 194 | $carry = ''; |
| 195 | } |
| 196 | |
| 197 | if (!\in_array($char, ['A', 'a', '0'], true)) { |
| 198 | $decremented = \chr(\ord($char) - 1).$decremented; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | return $decremented; |
| 204 | } |
| 205 | } |
| 206 |