Polyfill.php
269 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * The framework polyfill functions. |
| 5 | * |
| 6 | * @package Framework |
| 7 | * @subpackage Polyfill |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | namespace Kirki\Framework\Polyfill; |
| 11 | |
| 12 | \defined('ABSPATH') || exit; |
| 13 | \class_exists(ArgumentCountError::class); |
| 14 | if (!\function_exists('Kirki\\Framework\\Polyfill\\array_first')) { |
| 15 | /** |
| 16 | * Get the first element of an array. |
| 17 | * |
| 18 | * @param array $array The array to get the first element from |
| 19 | * |
| 20 | * @return mixed|null The first element of the array or the default value |
| 21 | * |
| 22 | * @since 1.0.0 |
| 23 | */ |
| 24 | function array_first(array $array) |
| 25 | { |
| 26 | if (\function_exists('\\array_first')) { |
| 27 | return \array_first($array); |
| 28 | } |
| 29 | if (empty($array)) { |
| 30 | return null; |
| 31 | } |
| 32 | foreach ($array as $value) { |
| 33 | return $value; |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | if (!\function_exists('Kirki\\Framework\\Polyfill\\array_last')) { |
| 38 | /** |
| 39 | * Get the last element of an array. |
| 40 | * |
| 41 | * @param array $array The array to get the last element from |
| 42 | * |
| 43 | * @return mixed|null The last element of the array or null if empty |
| 44 | * |
| 45 | * @since 1.0.0 |
| 46 | */ |
| 47 | function array_last(array $array) |
| 48 | { |
| 49 | if (\function_exists('\\array_last')) { |
| 50 | return \array_last($array); |
| 51 | } |
| 52 | if (empty($array)) { |
| 53 | return null; |
| 54 | } |
| 55 | $reversed = \array_reverse($array, \true); |
| 56 | return array_first($reversed); |
| 57 | } |
| 58 | } |
| 59 | if (!\function_exists('Kirki\\Framework\\Polyfill\\is_iterable')) { |
| 60 | /** |
| 61 | * Determine if the given value is iterable. |
| 62 | * |
| 63 | * @param mixed $value The value to check |
| 64 | * |
| 65 | * @return bool True if the value is an array or Traversable, false otherwise |
| 66 | * |
| 67 | * @since 1.0.0 |
| 68 | */ |
| 69 | function is_iterable($value) |
| 70 | { |
| 71 | if (\function_exists('\\is_iterable')) { |
| 72 | return \is_iterable($value); |
| 73 | } |
| 74 | return \is_array($value) || $value instanceof \Traversable; |
| 75 | } |
| 76 | } |
| 77 | if (!\function_exists('Kirki\\Framework\\Polyfill\\array_key_first')) { |
| 78 | /** |
| 79 | * Get the first key of an array. |
| 80 | * |
| 81 | * @param array $array The array to get the first key from |
| 82 | * |
| 83 | * @return int|string|null The first key of the array or null if the array is empty |
| 84 | * |
| 85 | * @since 1.0.0 |
| 86 | */ |
| 87 | function array_key_first(array $array) |
| 88 | { |
| 89 | if (\function_exists('\\array_key_first')) { |
| 90 | return \array_key_first($array); |
| 91 | } |
| 92 | foreach ($array as $key => $value) { |
| 93 | return $key; |
| 94 | } |
| 95 | return null; |
| 96 | } |
| 97 | } |
| 98 | if (!\function_exists('Kirki\\Framework\\Polyfill\\array_key_last')) { |
| 99 | /** |
| 100 | * Get the last key of an array. |
| 101 | * |
| 102 | * @param array $array The array to get the last key from |
| 103 | * |
| 104 | * @return int|string|null The last key of the array or null if the array is empty |
| 105 | * |
| 106 | * @since 1.0.0 |
| 107 | */ |
| 108 | function array_key_last(array $array) |
| 109 | { |
| 110 | if (\function_exists('\\array_key_last')) { |
| 111 | return \array_key_last($array); |
| 112 | } |
| 113 | if (empty($array)) { |
| 114 | return null; |
| 115 | } |
| 116 | return \key(\array_slice($array, -1, 1, \true)); |
| 117 | } |
| 118 | } |
| 119 | if (!\function_exists('Kirki\\Framework\\Polyfill\\str_contains')) { |
| 120 | /** |
| 121 | * Determine if a string contains a given substring. |
| 122 | * |
| 123 | * @param string $haystack The string to search in |
| 124 | * @param string $needle The substring to search for |
| 125 | * |
| 126 | * @return bool True if the haystack contains the needle, false otherwise |
| 127 | * |
| 128 | * @since 1.0.0 |
| 129 | */ |
| 130 | function str_contains($haystack, $needle) |
| 131 | { |
| 132 | if (\function_exists('\\str_contains')) { |
| 133 | return \str_contains($haystack, $needle); |
| 134 | } |
| 135 | return (string) $needle === '' || \strpos((string) $haystack, (string) $needle) !== \false; |
| 136 | } |
| 137 | } |
| 138 | if (!\function_exists('Kirki\\Framework\\Polyfill\\str_starts_with')) { |
| 139 | /** |
| 140 | * Determine if a string starts with a given substring. |
| 141 | * |
| 142 | * @param string $haystack The string to search in |
| 143 | * @param string $needle The substring to search for |
| 144 | * |
| 145 | * @return bool True if the haystack starts with the needle, false otherwise |
| 146 | * |
| 147 | * @since 1.0.0 |
| 148 | */ |
| 149 | function str_starts_with($haystack, $needle) |
| 150 | { |
| 151 | if (\function_exists('\\str_starts_with')) { |
| 152 | return \str_starts_with($haystack, $needle); |
| 153 | } |
| 154 | return \strncmp((string) $haystack, (string) $needle, \strlen((string) $needle)) === 0; |
| 155 | } |
| 156 | } |
| 157 | if (!\function_exists('Kirki\\Framework\\Polyfill\\str_ends_with')) { |
| 158 | /** |
| 159 | * Determine if a string ends with a given substring. |
| 160 | * |
| 161 | * @param string $haystack The string to search in |
| 162 | * @param string $needle The substring to search for |
| 163 | * |
| 164 | * @return bool True if the haystack ends with the needle, false otherwise |
| 165 | * |
| 166 | * @since 1.0.0 |
| 167 | */ |
| 168 | function str_ends_with($haystack, $needle) |
| 169 | { |
| 170 | if (\function_exists('\\str_ends_with')) { |
| 171 | return \str_ends_with($haystack, $needle); |
| 172 | } |
| 173 | $haystack = (string) $haystack; |
| 174 | $needle = (string) $needle; |
| 175 | if ($needle === '' || $haystack === $needle) { |
| 176 | return \true; |
| 177 | } |
| 178 | if ($haystack === '') { |
| 179 | return \false; |
| 180 | } |
| 181 | $needle_length = \strlen($needle); |
| 182 | return $needle_length <= \strlen($haystack) && \substr_compare($haystack, $needle, -$needle_length) === 0; |
| 183 | } |
| 184 | } |
| 185 | if (!\function_exists('Kirki\\Framework\\Polyfill\\array_find')) { |
| 186 | /** |
| 187 | * Find the first item in the array that matches the key. |
| 188 | * |
| 189 | * @param array $array The array to find the item in |
| 190 | * @param callable $callback The callback to find the item by |
| 191 | * @return mixed The first item that matches the key |
| 192 | */ |
| 193 | function array_find(array $array, callable $callback) |
| 194 | { |
| 195 | if (\function_exists('\\array_find')) { |
| 196 | return \array_find($array, $callback); |
| 197 | } |
| 198 | foreach ($array as $key => $item) { |
| 199 | if ($callback($item, $key)) { |
| 200 | return $item; |
| 201 | } |
| 202 | } |
| 203 | return null; |
| 204 | } |
| 205 | } |
| 206 | if (!\function_exists('Kirki\\Framework\\Polyfill\\array_find_key')) { |
| 207 | /** |
| 208 | * Find the first item in the array that matches the key. |
| 209 | * |
| 210 | * @param array $array The array to find the item in |
| 211 | * @param callable $callback The callback to find the item by |
| 212 | * @return mixed The first item that matches the key |
| 213 | */ |
| 214 | function array_find_key(array $array, callable $callback) |
| 215 | { |
| 216 | if (\function_exists('\\array_find_key')) { |
| 217 | return \array_find_key($array, $callback); |
| 218 | } |
| 219 | foreach ($array as $key => $item) { |
| 220 | if ($callback($item, $key)) { |
| 221 | return $key; |
| 222 | } |
| 223 | } |
| 224 | return null; |
| 225 | } |
| 226 | } |
| 227 | if (!\function_exists('Kirki\\Framework\\Polyfill\\array_any')) { |
| 228 | /** |
| 229 | * Determine if any item in the array matches the callback. |
| 230 | * |
| 231 | * @param array $array The array to check |
| 232 | * @param callable $callback The callback to check |
| 233 | * @return bool True if any item matches the callback, false otherwise |
| 234 | */ |
| 235 | function array_any(array $array, callable $callback) |
| 236 | { |
| 237 | if (\function_exists('\\array_any')) { |
| 238 | return \array_any($array, $callback); |
| 239 | } |
| 240 | foreach ($array as $key => $item) { |
| 241 | if ($callback($item, $key)) { |
| 242 | return \true; |
| 243 | } |
| 244 | } |
| 245 | return \false; |
| 246 | } |
| 247 | } |
| 248 | if (!\function_exists('Kirki\\Framework\\Polyfill\\array_all')) { |
| 249 | /** |
| 250 | * Determine if any item in the array matches the callback. |
| 251 | * |
| 252 | * @param array $array The array to check |
| 253 | * @param callable $callback The callback to check |
| 254 | * @return bool True if any item matches the callback, false otherwise |
| 255 | */ |
| 256 | function array_all(array $array, callable $callback) |
| 257 | { |
| 258 | if (\function_exists('\\array_all')) { |
| 259 | return \array_all($array, $callback); |
| 260 | } |
| 261 | foreach ($array as $key => $item) { |
| 262 | if (!$callback($item, $key)) { |
| 263 | return \false; |
| 264 | } |
| 265 | } |
| 266 | return \true; |
| 267 | } |
| 268 | } |
| 269 |