You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
3.3 KiB

3 years ago
  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\Php80;
  11. /**
  12. * @author Ion Bazan <ion.bazan@gmail.com>
  13. * @author Nico Oelgart <nicoswd@gmail.com>
  14. * @author Nicolas Grekas <p@tchwork.com>
  15. *
  16. * @internal
  17. */
  18. final class Php80
  19. {
  20. public static function fdiv(float $dividend, float $divisor): float
  21. {
  22. return @($dividend / $divisor);
  23. }
  24. public static function get_debug_type($value): string
  25. {
  26. switch (true) {
  27. case null === $value: return 'null';
  28. case \is_bool($value): return 'bool';
  29. case \is_string($value): return 'string';
  30. case \is_array($value): return 'array';
  31. case \is_int($value): return 'int';
  32. case \is_float($value): return 'float';
  33. case \is_object($value): break;
  34. case $value instanceof \__PHP_Incomplete_Class: return '__PHP_Incomplete_Class';
  35. default:
  36. if (null === $type = @get_resource_type($value)) {
  37. return 'unknown';
  38. }
  39. if ('Unknown' === $type) {
  40. $type = 'closed';
  41. }
  42. return "resource ($type)";
  43. }
  44. $class = \get_class($value);
  45. if (false === strpos($class, '@')) {
  46. return $class;
  47. }
  48. return (get_parent_class($class) ?: key(class_implements($class)) ?: 'class').'@anonymous';
  49. }
  50. public static function get_resource_id($res): int
  51. {
  52. if (!\is_resource($res) && null === @get_resource_type($res)) {
  53. throw new \TypeError(sprintf('Argument 1 passed to get_resource_id() must be of the type resource, %s given', get_debug_type($res)));
  54. }
  55. return (int) $res;
  56. }
  57. public static function preg_last_error_msg(): string
  58. {
  59. switch (preg_last_error()) {
  60. case \PREG_INTERNAL_ERROR:
  61. return 'Internal error';
  62. case \PREG_BAD_UTF8_ERROR:
  63. return 'Malformed UTF-8 characters, possibly incorrectly encoded';
  64. case \PREG_BAD_UTF8_OFFSET_ERROR:
  65. return 'The offset did not correspond to the beginning of a valid UTF-8 code point';
  66. case \PREG_BACKTRACK_LIMIT_ERROR:
  67. return 'Backtrack limit exhausted';
  68. case \PREG_RECURSION_LIMIT_ERROR:
  69. return 'Recursion limit exhausted';
  70. case \PREG_JIT_STACKLIMIT_ERROR:
  71. return 'JIT stack limit exhausted';
  72. case \PREG_NO_ERROR:
  73. return 'No error';
  74. default:
  75. return 'Unknown error';
  76. }
  77. }
  78. public static function str_contains(string $haystack, string $needle): bool
  79. {
  80. return '' === $needle || false !== strpos($haystack, $needle);
  81. }
  82. public static function str_starts_with(string $haystack, string $needle): bool
  83. {
  84. return 0 === strncmp($haystack, $needle, \strlen($needle));
  85. }
  86. public static function str_ends_with(string $haystack, string $needle): bool
  87. {
  88. return '' === $needle || ('' !== $haystack && 0 === substr_compare($haystack, $needle, -\strlen($needle)));
  89. }
  90. }