Php83.php 6.7 KB

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