| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 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 <[email protected]> |
| 16 |
* @author Pierre Ambroise <[email protected]> |
| 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 (!\in_array($pad_type, [\STR_PAD_RIGHT, \STR_PAD_LEFT, \STR_PAD_BOTH], true)) { |
| 47 |
throw new \ValueError('mb_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH'); |
| 48 |
} |
| 49 |
|
| 50 |
if (null === $encoding) { |
| 51 |
$encoding = mb_internal_encoding(); |
| 52 |
} |
| 53 |
|
| 54 |
$errorToTrigger = null; |
| 55 |
try { |
| 56 |
if (!@mb_check_encoding('', $encoding)) { |
| 57 |
$errorToTrigger = \sprintf('mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "%s" given', $encoding); |
| 58 |
} |
| 59 |
} catch (\ValueError $e) { |
| 60 |
$errorToTrigger = \sprintf('mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "%s" given', $encoding); |
| 61 |
} |
| 62 |
|
| 63 |
if (mb_strlen($pad_string, $encoding) <= 0) { |
| 64 |
$errorToTrigger = 'mb_str_pad(): Argument #3 ($pad_string) must be a non-empty string'; |
| 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 |
if (is_numeric($string)) { |
| 107 |
$offset = stripos($string, 'e'); |
| 108 |
if (false !== $offset) { |
| 109 |
$char = $string[$offset]; |
| 110 |
++$char; |
| 111 |
$string[$offset] = $char; |
| 112 |
++$string; |
| 113 |
|
| 114 |
switch ($string[$offset]) { |
| 115 |
case 'f': |
| 116 |
$string[$offset] = 'e'; |
| 117 |
break; |
| 118 |
case 'F': |
| 119 |
$string[$offset] = 'E'; |
| 120 |
break; |
| 121 |
case 'g': |
| 122 |
$string[$offset] = 'f'; |
| 123 |
break; |
| 124 |
case 'G': |
| 125 |
$string[$offset] = 'F'; |
| 126 |
break; |
| 127 |
} |
| 128 |
|
| 129 |
return $string; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
return ++$string; |
| 134 |
} |
| 135 |
|
| 136 |
public static function str_decrement(string $string): string |
| 137 |
{ |
| 138 |
if ('' === $string) { |
| 139 |
throw new \ValueError('str_decrement(): Argument #1 ($string) cannot be empty'); |
| 140 |
} |
| 141 |
|
| 142 |
if (!preg_match('/^[a-zA-Z0-9]+$/', $string)) { |
| 143 |
throw new \ValueError('str_decrement(): Argument #1 ($string) must be composed only of alphanumeric ASCII characters'); |
| 144 |
} |
| 145 |
|
| 146 |
if (preg_match('/\A(?:0[aA0]?|[aA])\z/', $string)) { |
| 147 |
throw new \ValueError(\sprintf('str_decrement(): Argument #1 ($string) "%s" is out of decrement range', $string)); |
| 148 |
} |
| 149 |
|
| 150 |
if (!\in_array(substr($string, -1), ['A', 'a', '0'], true)) { |
| 151 |
return implode('', \array_slice(str_split($string), 0, -1)).\chr(\ord(substr($string, -1)) - 1); |
| 152 |
} |
| 153 |
|
| 154 |
$carry = ''; |
| 155 |
$decremented = ''; |
| 156 |
|
| 157 |
for ($i = \strlen($string) - 1; $i >= 0; --$i) { |
| 158 |
$char = $string[$i]; |
| 159 |
|
| 160 |
switch ($char) { |
| 161 |
case 'A': |
| 162 |
if ('' !== $carry) { |
| 163 |
$decremented = $carry.$decremented; |
| 164 |
$carry = ''; |
| 165 |
} |
| 166 |
$carry = 'Z'; |
| 167 |
|
| 168 |
break; |
| 169 |
case 'a': |
| 170 |
if ('' !== $carry) { |
| 171 |
$decremented = $carry.$decremented; |
| 172 |
$carry = ''; |
| 173 |
} |
| 174 |
$carry = 'z'; |
| 175 |
|
| 176 |
break; |
| 177 |
case '0': |
| 178 |
if ('' !== $carry) { |
| 179 |
$decremented = $carry.$decremented; |
| 180 |
$carry = ''; |
| 181 |
} |
| 182 |
$carry = '9'; |
| 183 |
|
| 184 |
break; |
| 185 |
case '1': |
| 186 |
if ('' !== $carry) { |
| 187 |
$decremented = $carry.$decremented; |
| 188 |
$carry = ''; |
| 189 |
} |
| 190 |
|
| 191 |
break; |
| 192 |
default: |
| 193 |
if ('' !== $carry) { |
| 194 |
$decremented = $carry.$decremented; |
| 195 |
$carry = ''; |
| 196 |
} |
| 197 |
|
| 198 |
if (!\in_array($char, ['A', 'a', '0'], true)) { |
| 199 |
$decremented = \chr(\ord($char) - 1).$decremented; |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
return $decremented; |
| 205 |
} |
| 206 |
} |