| @@ -43,7 +43,57 @@ | ||
| 43 | 43 | * @param mixed $value The value to check. |
| 44 | 44 | * @return bool True if `$value` is countable, otherwise false. |
| 45 | 45 | */ |
| 46 | 46 | function is_countable( $value ) { |
| 47 | - return is_array( $value ) || $value instanceof \Countable; | |
| 47 | + return is_array( $value ) || $value instanceof \Countable; | |
| 48 | + } | |
| 49 | +} | |
| 50 | + | |
| 51 | +/** | |
| 52 | + * Polyfill for `array_is_list()` function added in PHP 7.3. | |
| 53 | + * | |
| 54 | + * @param array $array The array to check. | |
| 55 | + * | |
| 56 | + * @return bool True if `$array` is a list, otherwise false. | |
| 57 | + */ | |
| 58 | +if ( ! function_exists( 'array_is_list' ) ) { | |
| 59 | + function array_is_list( $array ) { | |
| 60 | + if ( ! is_array( $array ) ) { | |
| 61 | + return false; | |
| 62 | + } | |
| 63 | + | |
| 64 | + if ( array_values( $array ) === $array ) { | |
| 65 | + return true; | |
| 66 | + } | |
| 67 | + | |
| 68 | + $next_key = -1; | |
| 69 | + | |
| 70 | + foreach ( $array as $k => $v ) { | |
| 71 | + if ( ++$next_key !== $k ) { | |
| 72 | + return false; | |
| 73 | + } | |
| 74 | + } | |
| 75 | + | |
| 76 | + return true; | |
| 77 | + } | |
| 78 | +} | |
| 79 | + | |
| 80 | +if ( ! function_exists( 'str_contains' ) ) { | |
| 81 | + /** | |
| 82 | + * Polyfill for `str_contains()` function added in PHP 8.0. | |
| 83 | + * | |
| 84 | + * Performs a case-sensitive check indicating if needle is | |
| 85 | + * contained in haystack. | |
| 86 | + * | |
| 87 | + * @param string $haystack The string to search in. | |
| 88 | + * @param string $needle The substring to search for in the `$haystack`. | |
| 89 | + * | |
| 90 | + * @return bool True if `$needle` is in `$haystack`, otherwise false. | |
| 91 | + */ | |
| 92 | + function str_contains( $haystack, $needle ) { | |
| 93 | + if ( '' === $needle ) { | |
| 94 | + return true; | |
| 95 | + } | |
| 96 | + | |
| 97 | + return false !== strpos( $haystack, $needle ); | |
| 48 | 98 | } |
| 49 | 99 | } |