| 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\Php84; |
| 13 |
|
| 14 |
/** |
| 15 |
* @author Ayesh Karunaratne <[email protected]> |
| 16 |
* @author Pierre Ambroise <[email protected]> |
| 17 |
* |
| 18 |
* @internal |
| 19 |
*/ |
| 20 |
final class Php84 |
| 21 |
{ |
| 22 |
/** @return string|false */ |
| 23 |
public static function mb_ucfirst(string $string, ?string $encoding = null) |
| 24 |
{ |
| 25 |
if (null === $encoding) { |
| 26 |
$encoding = mb_internal_encoding(); |
| 27 |
} |
| 28 |
|
| 29 |
try { |
| 30 |
$validEncoding = @mb_check_encoding('', $encoding); |
| 31 |
} catch (\ValueError $e) { |
| 32 |
throw new \ValueError(\sprintf('mb_ucfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given', $encoding)); |
| 33 |
} |
| 34 |
|
| 35 |
if (!$validEncoding) { |
| 36 |
if (80000 > \PHP_VERSION_ID) { |
| 37 |
trigger_error(\sprintf('mb_ucfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given', $encoding), \E_USER_WARNING); |
| 38 |
|
| 39 |
return false; |
| 40 |
} |
| 41 |
|
| 42 |
throw new \ValueError(\sprintf('mb_ucfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given', $encoding)); |
| 43 |
} |
| 44 |
|
| 45 |
$firstChar = mb_substr($string, 0, 1, $encoding); |
| 46 |
$firstChar = mb_convert_case($firstChar, \MB_CASE_TITLE, $encoding); |
| 47 |
|
| 48 |
return $firstChar.mb_substr($string, 1, null, $encoding); |
| 49 |
} |
| 50 |
|
| 51 |
/** @return string|false */ |
| 52 |
public static function mb_lcfirst(string $string, ?string $encoding = null) |
| 53 |
{ |
| 54 |
if (null === $encoding) { |
| 55 |
$encoding = mb_internal_encoding(); |
| 56 |
} |
| 57 |
|
| 58 |
try { |
| 59 |
$validEncoding = @mb_check_encoding('', $encoding); |
| 60 |
} catch (\ValueError $e) { |
| 61 |
throw new \ValueError(\sprintf('mb_lcfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given', $encoding)); |
| 62 |
} |
| 63 |
|
| 64 |
if (!$validEncoding) { |
| 65 |
if (80000 > \PHP_VERSION_ID) { |
| 66 |
trigger_error(\sprintf('mb_lcfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given', $encoding), \E_USER_WARNING); |
| 67 |
|
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
throw new \ValueError(\sprintf('mb_lcfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given', $encoding)); |
| 72 |
} |
| 73 |
|
| 74 |
$firstChar = mb_substr($string, 0, 1, $encoding); |
| 75 |
$firstChar = mb_convert_case($firstChar, \MB_CASE_LOWER, $encoding); |
| 76 |
|
| 77 |
return $firstChar.mb_substr($string, 1, null, $encoding); |
| 78 |
} |
| 79 |
|
| 80 |
public static function array_find(array $array, callable $callback) |
| 81 |
{ |
| 82 |
foreach ($array as $key => $value) { |
| 83 |
if ($callback($value, $key)) { |
| 84 |
return $value; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return null; |
| 89 |
} |
| 90 |
|
| 91 |
public static function array_find_key(array $array, callable $callback) |
| 92 |
{ |
| 93 |
foreach ($array as $key => $value) { |
| 94 |
if ($callback($value, $key)) { |
| 95 |
return $key; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return null; |
| 100 |
} |
| 101 |
|
| 102 |
public static function array_any(array $array, callable $callback): bool |
| 103 |
{ |
| 104 |
foreach ($array as $key => $value) { |
| 105 |
if ($callback($value, $key)) { |
| 106 |
return true; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
public static function array_all(array $array, callable $callback): bool |
| 114 |
{ |
| 115 |
foreach ($array as $key => $value) { |
| 116 |
if (!$callback($value, $key)) { |
| 117 |
return false; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
return true; |
| 122 |
} |
| 123 |
|
| 124 |
public static function fpow(float $num, float $exponent): float |
| 125 |
{ |
| 126 |
return $num ** $exponent; |
| 127 |
} |
| 128 |
|
| 129 |
/** @return string|false */ |
| 130 |
public static function mb_trim(string $string, ?string $characters = null, ?string $encoding = null) |
| 131 |
{ |
| 132 |
return self::mb_internal_trim('{^[%s]+|[%1$s]+$}Du', $string, $characters, $encoding, __FUNCTION__); |
| 133 |
} |
| 134 |
|
| 135 |
/** @return string|false */ |
| 136 |
public static function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null) |
| 137 |
{ |
| 138 |
return self::mb_internal_trim('{^[%s]+}Du', $string, $characters, $encoding, __FUNCTION__); |
| 139 |
} |
| 140 |
|
| 141 |
/** @return string|false */ |
| 142 |
public static function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null) |
| 143 |
{ |
| 144 |
return self::mb_internal_trim('{[%s]+$}D', $string, $characters, $encoding, __FUNCTION__); |
| 145 |
} |
| 146 |
|
| 147 |
/** @return string|false */ |
| 148 |
private static function mb_internal_trim(string $regex, string $string, ?string $characters, ?string $encoding, string $function) |
| 149 |
{ |
| 150 |
if (null === $encoding) { |
| 151 |
$encoding = mb_internal_encoding(); |
| 152 |
} |
| 153 |
|
| 154 |
try { |
| 155 |
$validEncoding = @mb_check_encoding('', $encoding); |
| 156 |
} catch (\ValueError $e) { |
| 157 |
throw new \ValueError(\sprintf('%s(): Argument #3 ($encoding) must be a valid encoding, "%s" given', $function, $encoding)); |
| 158 |
} |
| 159 |
|
| 160 |
if (!$validEncoding) { |
| 161 |
if (80000 > \PHP_VERSION_ID) { |
| 162 |
trigger_error(\sprintf('%s(): Argument #3 ($encoding) must be a valid encoding, "%s" given', $function, $encoding), \E_USER_WARNING); |
| 163 |
|
| 164 |
return false; |
| 165 |
} |
| 166 |
|
| 167 |
throw new \ValueError(\sprintf('%s(): Argument #3 ($encoding) must be a valid encoding, "%s" given', $function, $encoding)); |
| 168 |
} |
| 169 |
|
| 170 |
if ('' === $characters) { |
| 171 |
return null === $encoding ? $string : mb_convert_encoding($string, $encoding); |
| 172 |
} |
| 173 |
|
| 174 |
if ('UTF-8' === $encoding || \in_array(strtolower($encoding), ['utf-8', 'utf8'], true)) { |
| 175 |
$encoding = 'UTF-8'; |
| 176 |
} |
| 177 |
|
| 178 |
$string = mb_convert_encoding($string, 'UTF-8', $encoding); |
| 179 |
|
| 180 |
if (null !== $characters) { |
| 181 |
$characters = mb_convert_encoding($characters, 'UTF-8', $encoding); |
| 182 |
} |
| 183 |
|
| 184 |
if (null === $characters) { |
| 185 |
$characters = "\\0 \f\n\r\t\v\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}"; |
| 186 |
} else { |
| 187 |
$characters = preg_quote($characters); |
| 188 |
} |
| 189 |
|
| 190 |
$string = preg_replace(\sprintf($regex, $characters), '', $string); |
| 191 |
|
| 192 |
if ('UTF-8' === $encoding) { |
| 193 |
return $string; |
| 194 |
} |
| 195 |
|
| 196 |
return mb_convert_encoding($string, $encoding, 'UTF-8'); |
| 197 |
} |
| 198 |
|
| 199 |
public static function grapheme_str_split(string $string, int $length) |
| 200 |
{ |
| 201 |
if (0 > $length || 1073741823 < $length) { |
| 202 |
throw new \ValueError('grapheme_str_split(): Argument #2 ($length) must be greater than 0 and less than or equal to 1073741823.'); |
| 203 |
} |
| 204 |
|
| 205 |
if ('' === $string) { |
| 206 |
return []; |
| 207 |
} |
| 208 |
|
| 209 |
$regex = ((float) \PCRE_VERSION < 10 ? (float) \PCRE_VERSION >= 8.32 : (float) \PCRE_VERSION >= 10.39) |
| 210 |
? '\X' |
| 211 |
: '(?:\r\n|(?:[ -~\x{200C}\x{200D}]|[ᆨ-ᇹ]+|[ᄀ-ᅟ]*(?:[가개갸걔거게겨계고과괘괴교구궈궤귀규그긔기까깨꺄꺠꺼께껴꼐꼬꽈꽤꾀꾜꾸꿔꿰뀌뀨끄끠끼나내냐냬너네녀녜노놔놰뇌뇨누눠눼뉘뉴느늬니다대댜댸더데뎌뎨도돠돼되됴두둬뒈뒤듀드듸디따때땨떄떠떼뗘뗴또똬뙈뙤뚀뚜뚸뛔뛰뜌뜨띄띠라래랴럐러레려례로롸뢔뢰료루뤄뤠뤼류르릐리마매먀먜머메며몌모뫄뫠뫼묘무뭐뭬뮈뮤므믜미바배뱌뱨버베벼볘보봐봬뵈뵤부붜붸뷔뷰브븨비빠빼뺘뺴뻐뻬뼈뼤뽀뽜뽸뾔뾰뿌뿨쀄쀠쀼쁘쁴삐사새샤섀서세셔셰소솨쇄쇠쇼수숴쉐쉬슈스싀시싸쌔쌰썌써쎄쎠쎼쏘쏴쐐쐬쑈쑤쒀쒜쒸쓔쓰씌씨아애야얘어에여예오와왜외요우워웨위유으의이자재쟈쟤저제져졔조좌좨죄죠주줘줴쥐쥬즈즤지짜째쨔쨰쩌쩨쪄쪠쪼쫘쫴쬐쬬쭈쭤쮀쮜쮸쯔쯰찌차채챠챼처체쳐쳬초촤쵀최쵸추춰췌취츄츠츼치카캐캬컈커케켜켸코콰쾌쾨쿄쿠쿼퀘퀴큐크킈키타태탸턔터테텨톄토톼퇘퇴툐투퉈퉤튀튜트틔티파패퍄퍠퍼페펴폐포퐈퐤푀표푸풔풰퓌퓨프픠피하해햐햬허헤혀혜호화홰회효후훠훼휘휴흐희히]?[ᅠ-ᆢ]+|[가-힣])[ᆨ-ᇹ]*|[ᄀ-ᅟ]+|[^\p{Cc}\p{Cf}\p{Zl}\p{Zp}])[\p{Mn}\p{Me}\x{09BE}\x{09D7}\x{0B3E}\x{0B57}\x{0BBE}\x{0BD7}\x{0CC2}\x{0CD5}\x{0CD6}\x{0D3E}\x{0D57}\x{0DCF}\x{0DDF}\x{200C}\x{200D}\x{1D165}\x{1D16E}-\x{1D172}]*|[\p{Cc}\p{Cf}\p{Zl}\p{Zp}])'; |
| 212 |
|
| 213 |
if (!preg_match_all('/'.$regex.'/u', $string, $matches)) { |
| 214 |
return false; |
| 215 |
} |
| 216 |
|
| 217 |
if (1 === $length) { |
| 218 |
return $matches[0]; |
| 219 |
} |
| 220 |
|
| 221 |
$chunks = array_chunk($matches[0], $length); |
| 222 |
foreach ($chunks as &$chunk) { |
| 223 |
$chunk = implode('', $chunk); |
| 224 |
} |
| 225 |
|
| 226 |
return $chunks; |
| 227 |
} |
| 228 |
|
| 229 |
public static function bcceil(string $num): string |
| 230 |
{ |
| 231 |
if (!is_numeric($num)) { |
| 232 |
throw new \ValueError('bcceil(): Argument #1 ($num) is not well-formed'); |
| 233 |
} |
| 234 |
|
| 235 |
return self::bcround($num, 0, \RoundingMode::PositiveInfinity); |
| 236 |
} |
| 237 |
|
| 238 |
public static function bcdivmod(string $num1, string $num2, ?int $scale = null): ?array |
| 239 |
{ |
| 240 |
if (null === $quot = @bcdiv($num1, $num2, 0)) { |
| 241 |
throw new \DivisionByZeroError('Division by zero'); |
| 242 |
} |
| 243 |
$scale = $scale ?? (\PHP_VERSION_ID >= 70300 ? bcscale() : (\ini_get('bcmath.scale') ?: 0)); |
| 244 |
|
| 245 |
return [$quot, bcmod($num1, $num2, $scale)]; |
| 246 |
} |
| 247 |
|
| 248 |
public static function bcfloor(string $num): string |
| 249 |
{ |
| 250 |
if (!is_numeric($num)) { |
| 251 |
throw new \ValueError('bcfloor(): Argument #1 ($num) is not well-formed'); |
| 252 |
} |
| 253 |
|
| 254 |
return self::bcround($num, 0, \RoundingMode::NegativeInfinity); |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* @param \RoundingMode|\RoundingMode::* $mode |
| 259 |
*/ |
| 260 |
public static function bcround(string $num, int $precision = 0, $mode = \RoundingMode::HalfAwayFromZero): string |
| 261 |
{ |
| 262 |
if (!is_numeric($num)) { |
| 263 |
throw new \ValueError('bcround(): Argument #1 ($num) is not well-formed'); |
| 264 |
} |
| 265 |
|
| 266 |
$sign = 1; |
| 267 |
if ('' !== $num && ('-' === $num[0] || '+' === $num[0])) { |
| 268 |
if ('-' === $num[0]) { |
| 269 |
$sign = -1; |
| 270 |
} |
| 271 |
|
| 272 |
$num = substr($num, 1); |
| 273 |
} |
| 274 |
|
| 275 |
if (false !== strpos($num, '.')) { |
| 276 |
[$intPart, $fracPart] = array_pad(explode('.', $num, 2), 2, ''); |
| 277 |
} else { |
| 278 |
$intPart = $num; |
| 279 |
$fracPart = ''; |
| 280 |
} |
| 281 |
|
| 282 |
if ('' === $intPart) { |
| 283 |
$intPart = '0'; |
| 284 |
} |
| 285 |
|
| 286 |
$intPart = self::trimLeadingZeros($intPart); |
| 287 |
$fracPart = (string) $fracPart; |
| 288 |
|
| 289 |
if ($precision >= 0) { |
| 290 |
$fracLength = \strlen($fracPart); |
| 291 |
|
| 292 |
if ($precision <= $fracLength) { |
| 293 |
$scaledInt = $intPart.(string) substr($fracPart, 0, $precision); |
| 294 |
$scaledFrac = (string) substr($fracPart, $precision); |
| 295 |
} else { |
| 296 |
$scaledInt = $intPart.$fracPart.str_repeat('0', $precision - $fracLength); |
| 297 |
$scaledFrac = ''; |
| 298 |
} |
| 299 |
} else { |
| 300 |
$shift = -$precision; |
| 301 |
$intLength = \strlen($intPart); |
| 302 |
|
| 303 |
if ($shift <= $intLength) { |
| 304 |
$splitPos = $intLength - $shift; |
| 305 |
$scaledInt = substr($intPart, 0, $splitPos); |
| 306 |
$scaledInt = '' === $scaledInt ? '0' : $scaledInt; |
| 307 |
$scaledFrac = substr($intPart, $splitPos).$fracPart; |
| 308 |
} else { |
| 309 |
$scaledInt = '0'; |
| 310 |
$scaledFrac = str_repeat('0', $shift - $intLength).$intPart.$fracPart; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
$roundedInt = self::roundIntegerPart($scaledInt, $scaledFrac, $sign, $mode); |
| 315 |
$isZero = '' === trim($roundedInt, '0'); |
| 316 |
$absResult = self::formatRoundedDigits($roundedInt, $precision); |
| 317 |
|
| 318 |
if (-1 === $sign && !$isZero) { |
| 319 |
$absResult = '-'.$absResult; |
| 320 |
} |
| 321 |
|
| 322 |
return $absResult; |
| 323 |
} |
| 324 |
|
| 325 |
private static function roundIntegerPart(string $intPart, string $fracPart, int $sign, $mode): string |
| 326 |
{ |
| 327 |
$intPart = self::trimLeadingZeros($intPart); |
| 328 |
|
| 329 |
if ('' === $fracPart || '' === trim($fracPart, '0')) { |
| 330 |
return $intPart; |
| 331 |
} |
| 332 |
|
| 333 |
$firstDigit = $fracPart[0]; |
| 334 |
$tail = (string) substr($fracPart, 1); |
| 335 |
$tailNonZero = '' !== trim($tail, '0'); |
| 336 |
$isGreaterThanHalf = $firstDigit > '5' || ('5' === $firstDigit && $tailNonZero); |
| 337 |
$isExactlyHalf = '5' === $firstDigit && !$tailNonZero; |
| 338 |
$shouldIncrease = false; |
| 339 |
|
| 340 |
switch ($mode) { |
| 341 |
case \RoundingMode::TowardsZero: |
| 342 |
break; |
| 343 |
|
| 344 |
case \RoundingMode::AwayFromZero: |
| 345 |
$shouldIncrease = true; |
| 346 |
break; |
| 347 |
|
| 348 |
case \RoundingMode::PositiveInfinity: |
| 349 |
$shouldIncrease = $sign > 0; |
| 350 |
break; |
| 351 |
|
| 352 |
case \RoundingMode::NegativeInfinity: |
| 353 |
$shouldIncrease = $sign < 0; |
| 354 |
break; |
| 355 |
|
| 356 |
case \RoundingMode::HalfAwayFromZero: |
| 357 |
$shouldIncrease = $isGreaterThanHalf || $isExactlyHalf; |
| 358 |
break; |
| 359 |
|
| 360 |
case \RoundingMode::HalfTowardsZero: |
| 361 |
$shouldIncrease = $isGreaterThanHalf; |
| 362 |
break; |
| 363 |
|
| 364 |
case \RoundingMode::HalfEven: |
| 365 |
if ($isGreaterThanHalf) { |
| 366 |
$shouldIncrease = true; |
| 367 |
} elseif ($isExactlyHalf && 1 === self::lastDigit($intPart) % 2) { |
| 368 |
$shouldIncrease = true; |
| 369 |
} |
| 370 |
break; |
| 371 |
|
| 372 |
case \RoundingMode::HalfOdd: |
| 373 |
if ($isGreaterThanHalf) { |
| 374 |
$shouldIncrease = true; |
| 375 |
} elseif ($isExactlyHalf && 0 === self::lastDigit($intPart) % 2) { |
| 376 |
$shouldIncrease = true; |
| 377 |
} |
| 378 |
break; |
| 379 |
} |
| 380 |
|
| 381 |
if ($shouldIncrease) { |
| 382 |
$intPart = self::incrementDigits($intPart); |
| 383 |
} |
| 384 |
|
| 385 |
return self::trimLeadingZeros($intPart); |
| 386 |
} |
| 387 |
|
| 388 |
private static function formatRoundedDigits(string $roundedInt, int $precision): string |
| 389 |
{ |
| 390 |
if ($precision > 0) { |
| 391 |
if (\strlen($roundedInt) <= $precision) { |
| 392 |
$roundedInt = str_pad($roundedInt, $precision + 1, '0', \STR_PAD_LEFT); |
| 393 |
} |
| 394 |
|
| 395 |
$intDigits = substr($roundedInt, 0, -$precision); |
| 396 |
$fracDigits = substr($roundedInt, -$precision); |
| 397 |
|
| 398 |
$intDigits = self::trimLeadingZeros('' === $intDigits ? '0' : $intDigits); |
| 399 |
$fracDigits = str_pad($fracDigits, $precision, '0', \STR_PAD_LEFT); |
| 400 |
|
| 401 |
return $intDigits.'.'.$fracDigits; |
| 402 |
} |
| 403 |
|
| 404 |
if (0 === $precision) { |
| 405 |
return self::trimLeadingZeros($roundedInt); |
| 406 |
} |
| 407 |
|
| 408 |
$shift = -$precision; |
| 409 |
$digits = $roundedInt.str_repeat('0', $shift); |
| 410 |
|
| 411 |
return self::trimLeadingZeros($digits); |
| 412 |
} |
| 413 |
|
| 414 |
private static function incrementDigits(string $digits): string |
| 415 |
{ |
| 416 |
$digits = '' === $digits ? '0' : $digits; |
| 417 |
$index = \strlen($digits) - 1; |
| 418 |
$result = $digits; |
| 419 |
$carry = 1; |
| 420 |
|
| 421 |
while ($index >= 0 && $carry) { |
| 422 |
$value = \ord($result[$index]) - 48 + $carry; |
| 423 |
$carry = $value >= 10 ? 1 : 0; |
| 424 |
$result[$index] = \chr(48 + ($value % 10)); |
| 425 |
--$index; |
| 426 |
} |
| 427 |
|
| 428 |
return $carry ? '1'.$result : $result; |
| 429 |
} |
| 430 |
|
| 431 |
private static function trimLeadingZeros(string $digits): string |
| 432 |
{ |
| 433 |
$digits = ltrim($digits, '0'); |
| 434 |
|
| 435 |
return '' === $digits ? '0' : $digits; |
| 436 |
} |
| 437 |
|
| 438 |
private static function lastDigit(string $digits): int |
| 439 |
{ |
| 440 |
$length = \strlen($digits); |
| 441 |
|
| 442 |
return $length ? \ord($digits[$length - 1]) - 48 : 0; |
| 443 |
} |
| 444 |
} |