-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathpolyfills.php
More file actions
91 lines (89 loc) · 2.77 KB
/
polyfills.php
File metadata and controls
91 lines (89 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/**
* Polyfills for PHP functions that may not be available in older versions.
*
* @since 0.1.0
*/
declare (strict_types=1);
namespace WordPress\AiClientDependencies;
if (!\function_exists('array_is_list') && !\function_exists('WordPress\AiClientDependencies\array_is_list')) {
/**
* Checks whether a given array is a list.
*
* An array is considered a list if its keys consist of consecutive numbers from 0 to count($array)-1.
*
* @since 0.1.0
*
* @param array<mixed> $array The array to check.
* @return bool True if the array is a list, false otherwise.
*/
function array_is_list(array $array): bool
{
if ($array === []) {
return \true;
}
$expectedKey = 0;
foreach (\array_keys($array) as $key) {
if ($key !== $expectedKey) {
return \false;
}
$expectedKey++;
}
return \true;
}
}
if (!\function_exists('str_starts_with') && !\function_exists('WordPress\AiClientDependencies\str_starts_with')) {
/**
* Checks if a string starts with a given substring.
*
* @since 0.1.0
*
* @param string $haystack The string to search in.
* @param string $needle The substring to search for.
* @return bool True if $haystack starts with $needle, false otherwise.
*/
function str_starts_with(string $haystack, string $needle): bool
{
if ('' === $needle) {
return \true;
}
return 0 === \strpos($haystack, $needle);
}
}
if (!\function_exists('str_contains') && !\function_exists('WordPress\AiClientDependencies\str_contains')) {
/**
* Checks if a string contains a given substring.
*
* @since 0.1.0
*
* @param string $haystack The string to search in.
* @param string $needle The substring to search for.
* @return bool True if $haystack contains $needle, false otherwise.
*/
function str_contains(string $haystack, string $needle): bool
{
if ('' === $needle) {
return \true;
}
return \false !== \strpos($haystack, $needle);
}
}
if (!\function_exists('str_ends_with') && !\function_exists('WordPress\AiClientDependencies\str_ends_with')) {
/**
* Checks if a string ends with a given substring.
*
* @since 0.1.0
*
* @param string $haystack The string to search in.
* @param string $needle The substring to search for.
* @return bool True if $haystack ends with $needle, false otherwise.
*/
function str_ends_with(string $haystack, string $needle): bool
{
if ('' === $haystack) {
return '' === $needle;
}
$len = \strlen($needle);
return \substr($haystack, -$len, $len) === $needle;
}
}