|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Helper; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Helpers\Cast; |
|
7
|
|
|
|
|
8
|
|
|
class Str |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @param string $string |
|
12
|
|
|
* @return string |
|
13
|
|
|
*/ |
|
14
|
48 |
|
public static function camelCase($string) |
|
15
|
|
|
{ |
|
16
|
48 |
|
$string = ucwords(str_replace(['-', '_'], ' ', trim($string))); |
|
17
|
48 |
|
return str_replace(' ', '', $string); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param string|string[] $needles |
|
22
|
|
|
* @param string $haystack |
|
23
|
|
|
* @return bool |
|
24
|
|
|
*/ |
|
25
|
16 |
|
public static function contains($needles, $haystack) |
|
26
|
|
|
{ |
|
27
|
16 |
|
$needles = array_filter(Cast::toArray($needles), Helper::class.'::isNotEmpty'); |
|
28
|
16 |
|
foreach ($needles as $needle) { |
|
29
|
16 |
|
if (false !== strpos($haystack, $needle)) { |
|
30
|
9 |
|
return true; |
|
31
|
|
|
} |
|
32
|
|
|
} |
|
33
|
16 |
|
return false; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $name |
|
38
|
|
|
* @param string $nameType first|first_initial|initials|last|last_initial |
|
39
|
|
|
* @param string $initialType period|period_space|space |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
8 |
|
public static function convertName($name, $nameType = '', $initialType = '') |
|
43
|
|
|
{ |
|
44
|
8 |
|
$names = preg_split('/\W/u', $name, 0, PREG_SPLIT_NO_EMPTY); |
|
45
|
8 |
|
$firstName = array_shift($names); |
|
46
|
8 |
|
$lastName = array_pop($names); |
|
47
|
|
|
$initialTypes = [ |
|
48
|
8 |
|
'period' => '.', |
|
49
|
|
|
'period_space' => '. ', |
|
50
|
|
|
'space' => ' ', |
|
51
|
|
|
]; |
|
52
|
8 |
|
$initialPunctuation = (string) Arr::get($initialTypes, $initialType, ' '); |
|
53
|
8 |
|
if ('initials' == $nameType) { |
|
54
|
1 |
|
return static::convertToInitials($name, $initialPunctuation); |
|
55
|
|
|
} |
|
56
|
|
|
$nameTypes = [ |
|
57
|
8 |
|
'first' => $firstName, |
|
58
|
8 |
|
'first_initial' => static::convertToInitials($firstName).$initialPunctuation.$lastName, |
|
59
|
8 |
|
'last' => $lastName, |
|
60
|
8 |
|
'last_initial' => $firstName.' '.static::convertToInitials($lastName).$initialPunctuation, |
|
61
|
|
|
]; |
|
62
|
8 |
|
return trim((string) Arr::get($nameTypes, $nameType, $name)); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $path |
|
67
|
|
|
* @param string $prefix |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public static function convertPathToId($path, $prefix = '') |
|
71
|
|
|
{ |
|
72
|
1 |
|
return str_replace(['[', ']'], ['-', ''], static::convertPathToName($path, $prefix)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $path |
|
77
|
|
|
* @param string $prefix |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
2 |
|
public static function convertPathToName($path, $prefix = '') |
|
81
|
|
|
{ |
|
82
|
2 |
|
$levels = explode('.', $path); |
|
83
|
|
|
return array_reduce($levels, function ($result, $value) { |
|
84
|
2 |
|
return $result .= '['.$value.']'; |
|
85
|
2 |
|
}, $prefix); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string $name |
|
90
|
|
|
* @param string $initialPunctuation |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
9 |
|
public static function convertToInitials($name, $initialPunctuation = '') |
|
94
|
|
|
{ |
|
95
|
9 |
|
preg_match_all('/(?<=\s|\b)\pL/u', (string) $name, $matches); |
|
96
|
|
|
$result = (string) array_reduce($matches[0], function ($carry, $word) use ($initialPunctuation) { |
|
97
|
9 |
|
$initial = mb_substr($word, 0, 1, 'UTF-8'); |
|
98
|
9 |
|
$initial = mb_strtoupper($initial, 'UTF-8'); |
|
99
|
9 |
|
return $carry.$initial.$initialPunctuation; |
|
100
|
9 |
|
}); |
|
101
|
9 |
|
return trim($result); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param string $string |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
27 |
|
public static function dashCase($string) |
|
109
|
|
|
{ |
|
110
|
27 |
|
return str_replace('_', '-', static::snakeCase($string)); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param string|string[] $needles |
|
115
|
|
|
* @param string $haystack |
|
116
|
|
|
* @return bool |
|
117
|
|
|
*/ |
|
118
|
25 |
|
public static function endsWith($needles, $haystack) |
|
119
|
|
|
{ |
|
120
|
25 |
|
$needles = array_filter(Cast::toArray($needles), Helper::class.'::isNotEmpty'); |
|
121
|
25 |
|
foreach ($needles as $needle) { |
|
122
|
25 |
|
if (substr($haystack, -strlen(Cast::toString($needle))) === $needle) { |
|
123
|
25 |
|
return true; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
16 |
|
return false; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param mixed $value |
|
131
|
|
|
* @param string $fallback |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
2 |
|
public static function fallback($value, $fallback) |
|
135
|
|
|
{ |
|
136
|
2 |
|
return is_scalar($value) && '' !== trim($value) |
|
137
|
2 |
|
? Cast::toString($value) |
|
138
|
2 |
|
: Cast::toString($fallback); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param bool $quoted |
|
143
|
|
|
* @return string |
|
144
|
|
|
*/ |
|
145
|
1 |
|
public static function join(array $values, $quoted = false) |
|
146
|
|
|
{ |
|
147
|
1 |
|
return $quoted |
|
148
|
1 |
|
? "'".implode( "','", $values )."'" |
|
149
|
1 |
|
: implode(', ', $values); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
1 |
|
public static function naturalJoin(array $values) |
|
156
|
|
|
{ |
|
157
|
1 |
|
$and = __('and', 'site-reviews'); |
|
158
|
1 |
|
$values[] = implode(' '.$and.' ', array_splice($values, -2)); |
|
159
|
1 |
|
return implode(', ', $values); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param string $string |
|
164
|
|
|
* @param string $prefix |
|
165
|
|
|
* @param string|null $trim |
|
166
|
|
|
* @return string |
|
167
|
|
|
*/ |
|
168
|
26 |
|
public static function prefix($string, $prefix, $trim = null) |
|
169
|
|
|
{ |
|
170
|
26 |
|
if ('' === $string) { |
|
171
|
16 |
|
return $string; |
|
172
|
|
|
} |
|
173
|
26 |
|
if (null === $trim) { |
|
174
|
26 |
|
$trim = $prefix; |
|
175
|
|
|
} |
|
176
|
26 |
|
return $prefix.trim(static::removePrefix($string, $trim)); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param int $length |
|
181
|
|
|
* @return string |
|
182
|
|
|
*/ |
|
183
|
1 |
|
public static function random($length = 8) |
|
184
|
|
|
{ |
|
185
|
1 |
|
$text = base64_encode(wp_generate_password()); |
|
186
|
1 |
|
return substr(str_replace(['/','+','='], '', $text), 0, $length); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* @param string $prefix |
|
191
|
|
|
* @param string $string |
|
192
|
|
|
* @return string |
|
193
|
|
|
*/ |
|
194
|
32 |
|
public static function removePrefix($string, $prefix) |
|
195
|
|
|
{ |
|
196
|
32 |
|
return static::startsWith($prefix, $string) |
|
197
|
30 |
|
? substr($string, strlen($prefix)) |
|
198
|
32 |
|
: $string; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @param string $search |
|
203
|
|
|
* @param string $replace |
|
204
|
|
|
* @param string $subject |
|
205
|
|
|
* @return string |
|
206
|
|
|
*/ |
|
207
|
1 |
|
public static function replaceFirst($search, $replace, $subject) |
|
208
|
|
|
{ |
|
209
|
1 |
|
if ($search == '') { |
|
210
|
1 |
|
return $subject; |
|
211
|
|
|
} |
|
212
|
1 |
|
$position = strpos($subject, $search); |
|
213
|
1 |
|
if ($position !== false) { |
|
214
|
1 |
|
return substr_replace($subject, $replace, $position, strlen($search)); |
|
215
|
|
|
} |
|
216
|
1 |
|
return $subject; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @param string $search |
|
221
|
|
|
* @param string $replace |
|
222
|
|
|
* @param string $subject |
|
223
|
|
|
* @return string |
|
224
|
|
|
*/ |
|
225
|
27 |
|
public static function replaceLast($search, $replace, $subject) |
|
226
|
|
|
{ |
|
227
|
27 |
|
$position = strrpos($subject, $search); |
|
228
|
27 |
|
if ('' !== $search && false !== $position) { |
|
229
|
27 |
|
return substr_replace($subject, $replace, $position, strlen($search)); |
|
230
|
|
|
} |
|
231
|
1 |
|
return $subject; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* @param string|string[] $restrictions |
|
236
|
|
|
* @param string $value |
|
237
|
|
|
* @param string $fallback |
|
238
|
|
|
* @param bool $strict |
|
239
|
|
|
* @return string |
|
240
|
|
|
*/ |
|
241
|
24 |
|
public static function restrictTo($restrictions, $value, $fallback = '', $strict = false) |
|
242
|
|
|
{ |
|
243
|
24 |
|
$needle = $value; |
|
244
|
24 |
|
$haystack = Cast::toArray($restrictions); |
|
245
|
24 |
|
if (true !== $strict) { |
|
246
|
24 |
|
$needle = strtolower($needle); |
|
247
|
24 |
|
$haystack = array_map('strtolower', $haystack); |
|
248
|
|
|
} |
|
249
|
24 |
|
return in_array($needle, $haystack) |
|
250
|
24 |
|
? $value |
|
251
|
24 |
|
: $fallback; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* @param string $string |
|
256
|
|
|
* @return string |
|
257
|
|
|
*/ |
|
258
|
29 |
|
public static function snakeCase($string) |
|
259
|
|
|
{ |
|
260
|
29 |
|
if (!ctype_lower($string)) { |
|
261
|
29 |
|
$string = preg_replace('/\s+/u', '', $string); |
|
262
|
29 |
|
$string = preg_replace('/(.)(?=[A-Z])/u', '$1_', $string); |
|
263
|
29 |
|
$string = mb_strtolower($string, 'UTF-8'); |
|
264
|
|
|
} |
|
265
|
29 |
|
return str_replace('-', '_', $string); |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @param string|string[] $needles |
|
270
|
|
|
* @param string $haystack |
|
271
|
|
|
* @return bool |
|
272
|
|
|
*/ |
|
273
|
33 |
|
public static function startsWith($needles, $haystack = '') |
|
274
|
|
|
{ |
|
275
|
33 |
|
$needles = array_filter(Cast::toArray($needles), '\GeminiLabs\SiteReviews\Helper::isNotEmpty'); |
|
276
|
33 |
|
foreach ($needles as $needle) { |
|
277
|
33 |
|
if (substr((string) $haystack, 0, strlen(Cast::toString($needle))) === $needle) { |
|
278
|
32 |
|
return true; |
|
279
|
|
|
} |
|
280
|
|
|
} |
|
281
|
30 |
|
return false; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* @param string $string |
|
286
|
|
|
* @param string $suffix |
|
287
|
|
|
* @return string |
|
288
|
|
|
*/ |
|
289
|
1 |
|
public static function suffix($string, $suffix) |
|
290
|
|
|
{ |
|
291
|
1 |
|
if (!static::endsWith($suffix, $string)) { |
|
292
|
1 |
|
return $string.$suffix; |
|
293
|
|
|
} |
|
294
|
1 |
|
return $string; |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* @param string $string |
|
299
|
|
|
* @return string |
|
300
|
|
|
*/ |
|
301
|
|
|
public static function titleCase($string) |
|
302
|
|
|
{ |
|
303
|
|
|
$value = str_replace(['-', '_'], ' ', $string); |
|
304
|
|
|
return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8'); |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* @param string $value |
|
309
|
|
|
* @param int $length |
|
310
|
|
|
* @param string $end |
|
311
|
|
|
* @return string |
|
312
|
|
|
*/ |
|
313
|
1 |
|
public static function truncate($value, $length, $end = '') |
|
314
|
|
|
{ |
|
315
|
1 |
|
return mb_strwidth($value, 'UTF-8') > $length |
|
316
|
1 |
|
? mb_substr($value, 0, $length, 'UTF-8').$end |
|
317
|
1 |
|
: $value; |
|
318
|
|
|
} |
|
319
|
|
|
} |
|
320
|
|
|
|