jetpack
/
vendor
/
wp-php-toolkit
/
data-liberation
/
vendor-patched
/
symfony
/
polyfill-php80
/
Php80.php
jetpack
/
vendor
/
wp-php-toolkit
/
data-liberation
/
vendor-patched
/
symfony
/
polyfill-php80
Last commit date
Resources
3 weeks ago
LICENSE
3 weeks ago
Php80.php
3 weeks ago
PhpToken.php
3 weeks ago
README.md
3 weeks ago
bootstrap.php
3 weeks ago
Php80.php
138 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\Php80; |
| 13 | |
| 14 | use __PHP_Incomplete_Class; |
| 15 | use TypeError; |
| 16 | |
| 17 | use function get_class; |
| 18 | use function is_array; |
| 19 | use function is_bool; |
| 20 | use function is_float; |
| 21 | use function is_int; |
| 22 | use function is_object; |
| 23 | use function is_resource; |
| 24 | use function is_string; |
| 25 | use function strlen; |
| 26 | |
| 27 | use const PREG_BACKTRACK_LIMIT_ERROR; |
| 28 | use const PREG_BAD_UTF8_ERROR; |
| 29 | use const PREG_BAD_UTF8_OFFSET_ERROR; |
| 30 | use const PREG_INTERNAL_ERROR; |
| 31 | use const PREG_JIT_STACKLIMIT_ERROR; |
| 32 | use const PREG_NO_ERROR; |
| 33 | use const PREG_RECURSION_LIMIT_ERROR; |
| 34 | |
| 35 | /** |
| 36 | * @author Ion Bazan <ion.bazan@gmail.com> |
| 37 | * @author Nico Oelgart <nicoswd@gmail.com> |
| 38 | * @author Nicolas Grekas <p@tchwork.com> |
| 39 | * |
| 40 | * @internal |
| 41 | */ |
| 42 | final class Php80 { |
| 43 | public static function fdiv( float $dividend, float $divisor ): float { |
| 44 | return @( $dividend / $divisor ); |
| 45 | } |
| 46 | |
| 47 | public static function get_debug_type( $value ): string { |
| 48 | switch ( true ) { |
| 49 | case null === $value: |
| 50 | return 'null'; |
| 51 | case is_bool( $value ): |
| 52 | return 'bool'; |
| 53 | case is_string( $value ): |
| 54 | return 'string'; |
| 55 | case is_array( $value ): |
| 56 | return 'array'; |
| 57 | case is_int( $value ): |
| 58 | return 'int'; |
| 59 | case is_float( $value ): |
| 60 | return 'float'; |
| 61 | case is_object( $value ): |
| 62 | break; |
| 63 | case $value instanceof __PHP_Incomplete_Class: |
| 64 | return '__PHP_Incomplete_Class'; |
| 65 | default: |
| 66 | if ( null === $type = @get_resource_type( $value ) ) { |
| 67 | return 'unknown'; |
| 68 | } |
| 69 | |
| 70 | if ( 'Unknown' === $type ) { |
| 71 | $type = 'closed'; |
| 72 | } |
| 73 | |
| 74 | return "resource ($type)"; |
| 75 | } |
| 76 | |
| 77 | $class = get_class( $value ); |
| 78 | |
| 79 | if ( false === strpos( $class, '@' ) ) { |
| 80 | return $class; |
| 81 | } |
| 82 | |
| 83 | return ( get_parent_class( $class ) ?: key( class_implements( $class ) ) ?: 'class' ) . '@anonymous'; |
| 84 | } |
| 85 | |
| 86 | public static function get_resource_id( $res ): int { |
| 87 | if ( ! is_resource( $res ) && null === @get_resource_type( $res ) ) { |
| 88 | throw new TypeError( sprintf( 'Argument 1 passed to get_resource_id() must be of the type resource, %s given', |
| 89 | get_debug_type( $res ) ) ); |
| 90 | } |
| 91 | |
| 92 | return (int) $res; |
| 93 | } |
| 94 | |
| 95 | public static function preg_last_error_msg(): string { |
| 96 | switch ( preg_last_error() ) { |
| 97 | case PREG_INTERNAL_ERROR: |
| 98 | return 'Internal error'; |
| 99 | case PREG_BAD_UTF8_ERROR: |
| 100 | return 'Malformed UTF-8 characters, possibly incorrectly encoded'; |
| 101 | case PREG_BAD_UTF8_OFFSET_ERROR: |
| 102 | return 'The offset did not correspond to the beginning of a valid UTF-8 code point'; |
| 103 | case PREG_BACKTRACK_LIMIT_ERROR: |
| 104 | return 'Backtrack limit exhausted'; |
| 105 | case PREG_RECURSION_LIMIT_ERROR: |
| 106 | return 'Recursion limit exhausted'; |
| 107 | case PREG_JIT_STACKLIMIT_ERROR: |
| 108 | return 'JIT stack limit exhausted'; |
| 109 | case PREG_NO_ERROR: |
| 110 | return 'No error'; |
| 111 | default: |
| 112 | return 'Unknown error'; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | public static function str_contains( string $haystack, string $needle ): bool { |
| 117 | return '' === $needle || false !== strpos( $haystack, $needle ); |
| 118 | } |
| 119 | |
| 120 | public static function str_starts_with( string $haystack, string $needle ): bool { |
| 121 | return 0 === strncmp( $haystack, $needle, strlen( $needle ) ); |
| 122 | } |
| 123 | |
| 124 | public static function str_ends_with( string $haystack, string $needle ): bool { |
| 125 | if ( '' === $needle || $needle === $haystack ) { |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | if ( '' === $haystack ) { |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | $needleLength = strlen( $needle ); |
| 134 | |
| 135 | return $needleLength <= strlen( $haystack ) && 0 === substr_compare( $haystack, $needle, - $needleLength ); |
| 136 | } |
| 137 | } |
| 138 |