|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Helper; |
|
6
|
|
|
|
|
7
|
|
|
class Str |
|
8
|
|
|
{ |
|
9
|
214 |
|
public static function camelCase(string $string): string |
|
10
|
|
|
{ |
|
11
|
214 |
|
$string = ucwords(str_replace(['-', '_'], ' ', trim($string))); |
|
12
|
214 |
|
return str_replace(' ', '', $string); |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
46 |
|
public static function contains(string $haystack, array $needles): bool |
|
16
|
|
|
{ |
|
17
|
46 |
|
$needles = array_filter($needles, Helper::class.'::isNotEmpty'); |
|
18
|
46 |
|
foreach ($needles as $needle) { |
|
19
|
46 |
|
if ('' !== $needle && str_contains($haystack, $needle)) { |
|
20
|
46 |
|
return true; |
|
21
|
|
|
} |
|
22
|
|
|
} |
|
23
|
17 |
|
return false; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Returns an unsanitized id attribute. |
|
28
|
|
|
*/ |
|
29
|
130 |
|
public static function convertNameToId(string $string, string $prefix = ''): string |
|
30
|
|
|
{ |
|
31
|
130 |
|
$string = preg_replace('/[^a-z\d\[_]+/', '[', strtolower($string)); |
|
32
|
130 |
|
$parts = explode('[', $string); |
|
33
|
130 |
|
$parts = array_filter([$prefix, ...$parts]); |
|
34
|
130 |
|
return implode('-', $parts); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
132 |
|
public static function convertNameToPath(string $name): string |
|
38
|
|
|
{ |
|
39
|
132 |
|
$parts = preg_split('/\[|\]/', $name); |
|
40
|
132 |
|
$parts = array_values(array_filter($parts)); |
|
41
|
132 |
|
return implode('.', $parts); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
132 |
|
public static function convertPathToName(string $path, string $prefix = ''): string |
|
45
|
|
|
{ |
|
46
|
132 |
|
$levels = explode('.', $path); |
|
47
|
132 |
|
$levels = array_filter($levels); |
|
48
|
132 |
|
return array_reduce($levels, function ($carry, $value) { |
|
49
|
132 |
|
return empty($carry) ? $value : "{$carry}[{$value}]"; |
|
50
|
132 |
|
}, $prefix); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
175 |
|
public static function dashCase(string $string): string |
|
54
|
|
|
{ |
|
55
|
175 |
|
return str_replace('_', '-', static::snakeCase($string)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
public static function endsWith(string $haystack, array $needles): bool |
|
59
|
|
|
{ |
|
60
|
1 |
|
$needles = array_filter($needles, Helper::class.'::isNotEmpty'); |
|
61
|
1 |
|
foreach ($needles as $needle) { |
|
62
|
1 |
|
if ('' !== (string) $needle && str_ends_with((string) $haystack, (string) $needle)) { |
|
63
|
1 |
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
1 |
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param mixed $value |
|
71
|
|
|
*/ |
|
72
|
3 |
|
public static function fallback($value, string $fallback): string |
|
73
|
|
|
{ |
|
74
|
3 |
|
return is_scalar($value) && '' !== trim($value) |
|
75
|
3 |
|
? Cast::toString($value) |
|
76
|
3 |
|
: Cast::toString($fallback); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
48 |
|
public static function hash(string $value, int $maxLength = 32): string |
|
80
|
|
|
{ |
|
81
|
48 |
|
require_once ABSPATH.WPINC.'/pluggable.php'; |
|
82
|
48 |
|
return substr(wp_hash($value, 'nonce'), 0, max(8, $maxLength)); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
public static function join(array $values, bool $quoted = false): string |
|
86
|
|
|
{ |
|
87
|
1 |
|
return $quoted |
|
88
|
1 |
|
? "'".implode("','", $values)."'" |
|
89
|
1 |
|
: implode(', ', $values); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
1 |
|
public static function mask(string $string, int $preserveStart = 0, int $preserveEnd = 0, int $maxLength = 13): string |
|
93
|
|
|
{ |
|
94
|
1 |
|
$encoding = 'UTF-8'; |
|
95
|
1 |
|
if (empty($string)) { |
|
96
|
|
|
return $string; |
|
97
|
|
|
} |
|
98
|
1 |
|
$startLength = max(0, $preserveStart); |
|
99
|
1 |
|
$endLength = max(0, $preserveEnd); |
|
100
|
1 |
|
$start = mb_substr($string, 0, $startLength, $encoding); |
|
101
|
1 |
|
$end = mb_substr($string, -$endLength, $endLength); |
|
102
|
1 |
|
$segmentLen = max($maxLength - ($startLength + $endLength), 0); |
|
103
|
1 |
|
if (0 === $segmentLen) { |
|
104
|
1 |
|
return $string; |
|
105
|
|
|
} |
|
106
|
1 |
|
return $start.str_repeat(mb_substr('*', 0, 1, $encoding), $segmentLen).$end; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
1 |
|
public static function naturalJoin(array $values): string |
|
110
|
|
|
{ |
|
111
|
1 |
|
$and = __('and', 'site-reviews'); |
|
112
|
1 |
|
$values[] = implode(" {$and} ", array_splice($values, -2)); |
|
113
|
1 |
|
return implode(', ', $values); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
71 |
|
public static function prefix(string $string, string $prefix, ?string $trim = null): string |
|
117
|
|
|
{ |
|
118
|
71 |
|
if ('' === $string) { |
|
119
|
26 |
|
return $string; |
|
120
|
|
|
} |
|
121
|
71 |
|
if (null === $trim) { |
|
122
|
71 |
|
$trim = $prefix; |
|
123
|
|
|
} |
|
124
|
71 |
|
return $prefix.trim(static::removePrefix($string, $trim)); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
1 |
|
public static function random(int $length = 8): string |
|
128
|
|
|
{ |
|
129
|
1 |
|
$text = base64_encode(wp_generate_password()); |
|
130
|
1 |
|
return substr(str_replace(['/', '+', '='], '', $text), 0, $length); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
211 |
|
public static function removePrefix(string $string, string $prefix): string |
|
134
|
|
|
{ |
|
135
|
211 |
|
return str_starts_with($string, $prefix) |
|
136
|
210 |
|
? substr($string, strlen($prefix)) |
|
137
|
211 |
|
: $string; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
public static function removeSuffix(string $string, string $suffix): string |
|
141
|
|
|
{ |
|
142
|
|
|
return str_ends_with($string, $suffix) |
|
143
|
|
|
? substr($string, 0, strrpos($string, $suffix)) |
|
144
|
|
|
: $string; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
1 |
|
public static function replaceFirst(string $search, string $replace, string $subject): string |
|
148
|
|
|
{ |
|
149
|
1 |
|
if ('' === $search) { |
|
150
|
1 |
|
return $subject; |
|
151
|
|
|
} |
|
152
|
1 |
|
$position = strpos($subject, $search); |
|
153
|
1 |
|
if (false !== $position) { |
|
154
|
1 |
|
return substr_replace($subject, $replace, $position, strlen($search)); |
|
155
|
|
|
} |
|
156
|
1 |
|
return $subject; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
173 |
|
public static function replaceLast(string $search, string $replace, string $subject): string |
|
160
|
|
|
{ |
|
161
|
173 |
|
$position = strrpos($subject, $search); |
|
162
|
173 |
|
if ('' !== $search && false !== $position) { |
|
163
|
173 |
|
return substr_replace($subject, $replace, $position, strlen($search)); |
|
164
|
|
|
} |
|
165
|
1 |
|
return $subject; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param string|string[] $restrictions |
|
170
|
|
|
*/ |
|
171
|
57 |
|
public static function restrictTo($restrictions, string $value, string $fallback = '', bool $strict = false): string |
|
172
|
|
|
{ |
|
173
|
57 |
|
$needle = $value; |
|
174
|
57 |
|
$haystack = Cast::toArray($restrictions); |
|
175
|
57 |
|
if (true !== $strict) { |
|
176
|
57 |
|
$needle = strtolower($needle); |
|
177
|
57 |
|
$haystack = array_map('strtolower', $haystack); |
|
178
|
|
|
} |
|
179
|
57 |
|
return in_array($needle, $haystack) |
|
180
|
57 |
|
? $value |
|
181
|
57 |
|
: $fallback; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
177 |
|
public static function snakeCase(string $string): string |
|
185
|
|
|
{ |
|
186
|
177 |
|
if (!ctype_lower($string)) { |
|
187
|
176 |
|
$string = preg_replace('/\s+/u', '', $string); |
|
188
|
176 |
|
$string = preg_replace('/(.)(?=[A-Z])/u', '$1_', $string); |
|
189
|
176 |
|
$string = mb_strtolower($string, 'UTF-8'); |
|
190
|
|
|
} |
|
191
|
177 |
|
return str_replace('-', '_', $string); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
27 |
|
public static function startsWith(string $haystack, array $needles): bool |
|
195
|
|
|
{ |
|
196
|
27 |
|
$needles = array_filter($needles, '\GeminiLabs\SiteReviews\Helper::isNotEmpty'); |
|
197
|
27 |
|
foreach ($needles as $needle) { |
|
198
|
27 |
|
if ('' !== (string) $needle && str_starts_with((string) $haystack, (string) $needle)) { |
|
199
|
26 |
|
return true; |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
27 |
|
return false; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
27 |
|
public static function suffix(string $string, string $suffix): string |
|
206
|
|
|
{ |
|
207
|
27 |
|
if (!str_ends_with($string, $suffix)) { |
|
208
|
27 |
|
return $string.$suffix; |
|
209
|
|
|
} |
|
210
|
1 |
|
return $string; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
public static function titleCase(string $string): string |
|
214
|
|
|
{ |
|
215
|
|
|
$value = str_replace(['-', '_'], ' ', $string); |
|
216
|
|
|
return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8'); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
1 |
|
public static function truncate(string $value, int $length, string $end = ''): string |
|
220
|
|
|
{ |
|
221
|
1 |
|
return mb_strwidth($value, 'UTF-8') > $length |
|
222
|
1 |
|
? mb_substr($value, 0, $length, 'UTF-8').$end |
|
223
|
1 |
|
: $value; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
public static function wpCase(string $value): string |
|
227
|
|
|
{ |
|
228
|
|
|
$value = static::snakeCase($value); |
|
229
|
|
|
return str_replace(' ', '_', ucwords(str_replace('_', ' ', $value))); |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
|