PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.2.0
Kirki – Freeform Page Builder, Website Builder & Customizer v6.2.0
6.2.1 6.2.0 6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / libraries / framework / Supports / Str.php
kirki / libraries / framework / Supports Last commit date
Facades 1 week ago Traits 1 month ago Arr.php 1 week ago DataCaster.php 1 month ago Fluent.php 1 week ago HigherOrderTapProxy.php 1 month ago MediaAttachment.php 1 month ago MessagesBag.php 1 week ago Somoy.php 1 week ago Str.php 1 week ago Url.php 1 month ago Utils.php 1 month ago
Str.php
514 lines
1 <?php
2
3 /**
4 * A utility class for string manipulation and formatting.
5 * Provides various static methods to handle common string operations such as type conversion,
6 * slug generation, and string incrementation.
7 *
8 * @package Framework
9 * @subpackage Supports
10 * @since 1.0.0
11 */
12 namespace Kirki\Framework\Supports;
13
14 \defined('ABSPATH') || exit;
15 use Kirki\Framework\Sanitizer;
16 use Kirki\Framework\Supports\Traits\Macroable;
17 use Traversable;
18 use function Kirki\Framework\Polyfill\array_first;
19 use function Kirki\Framework\Polyfill\array_last;
20 use function Kirki\Framework\Polyfill\str_ends_with;
21 use function Kirki\Framework\Polyfill\str_starts_with;
22 class Str
23 {
24 use Macroable;
25 /**
26 * The increment styles.
27 *
28 * @var array
29 *
30 * @since 1.0.0
31 */
32 protected static $increment_styles = ['dash' => ['/-(\\d+)$/', '-%d'], 'regular' => [['/\\((\\d+)\\)$/', '/\\(\\d+\\)$/'], [' (%d)', '(%d)']]];
33 /**
34 * Convert a numeric string to a corresponding number.
35 *
36 * @param mixed $value The value.
37 *
38 * @return string|int|float
39 *
40 * @since 1.0.0
41 */
42 public static function to_number($value)
43 {
44 if (empty($value) || !\is_numeric($value)) {
45 return $value;
46 }
47 return \strpos($value, '.') !== \false || \stripos($value, 'e') !== \false ? (float) $value : (int) $value;
48 }
49 /**
50 * Sanitize a string to use as a slug.
51 *
52 * This function sanitizes the input string according to the Sanitizer::TITLE rule,
53 * which is used to sanitize title strings.
54 *
55 * @param string $value The string to sanitize.
56 *
57 * @return string The sanitized string.
58 *
59 * @since 1.0.0
60 */
61 public static function slug($value)
62 {
63 return Sanitizer::apply_rule($value, Sanitizer::TITLE);
64 }
65 /**
66 * Increment the string.
67 *
68 * @param string $value The string to increment.
69 * @param string $style The style of the increment. Available styles are 'dash' and 'regular'.
70 *
71 * @return string The incremented string.
72 *
73 * @since 1.0.0
74 */
75 public static function increment($value, $style = 'dash')
76 {
77 $style = static::$increment_styles[$style] ?? static::$increment_styles['regular'];
78 if (\is_array($style[0])) {
79 $search_regex = array_first($style[0]);
80 $replace_regex = array_last($style[0]);
81 } else {
82 $search_regex = $replace_regex = $style[0];
83 }
84 if (\is_array($style[1])) {
85 $old_format = array_first($style[1]);
86 $new_format = array_last($style[1]);
87 } else {
88 $old_format = $new_format = $style[1];
89 }
90 if (\preg_match($search_regex, $value, $matches)) {
91 $count = \intval($matches[1]) + 1;
92 return \preg_replace($replace_regex, \sprintf($new_format, $count), $value, 1);
93 } else {
94 $count = 2;
95 return $value . \sprintf($old_format, $count);
96 }
97 }
98 /**
99 * Trim the string.
100 *
101 * @param string $value The string to trim.
102 * @param string $charlist The characters to trim.
103 *
104 * @return string The trimmed string.
105 *
106 * @since 1.0.0
107 */
108 public static function trim($value, $charlist = null)
109 {
110 if (\is_null($charlist)) {
111 $trim_default_chars = " \n\r\t\v\x00";
112 $quoted_chars = \preg_quote($trim_default_chars, '~');
113 $regex = '~^[\\s\\x{FEFF}\\x{200B}\\x{200E}' . $quoted_chars . ']+|[\\s\\x{FEFF}\\x{200B}\\x{200E}' . $quoted_chars . ']+$~u';
114 return \preg_replace($regex, '', $value) ?? \trim($value);
115 }
116 return \trim($value, $charlist);
117 }
118 /**
119 * Left trim the string.
120 *
121 * @param string $value The string to left trim.
122 * @param string $charlist The characters to left trim.
123 *
124 * @return string The left trimmed string.
125 *
126 * @since 1.0.0
127 */
128 public static function ltrim($value, $charlist = null)
129 {
130 if (\is_null($charlist)) {
131 $ltrim_default_chars = " \n\r\t\v\x00";
132 $quoted_chars = \preg_quote($ltrim_default_chars, '~');
133 $regex = '~^[\\s\\x{FEFF}\\x{200B}\\x{200E}' . $quoted_chars . ']+~u';
134 return \preg_replace($regex, '', $value) ?? \ltrim($value);
135 }
136 return \ltrim($value, $charlist);
137 }
138 /**
139 * Right trim the string.
140 *
141 * @param string $value The string to right trim.
142 * @param string $charlist The characters to right trim.
143 *
144 * @return string The right trimmed string.
145 *
146 * @since 1.0.0
147 */
148 public static function rtrim($value, $charlist = null)
149 {
150 if (\is_null($charlist)) {
151 $rtrim_default_chars = " \n\r\t\v\x00";
152 $quoted_chars = \preg_quote($rtrim_default_chars, '~');
153 $regex = '~[\\s\\x{FEFF}\\x{200B}\\x{200E}' . $quoted_chars . ']+$~u';
154 return \preg_replace($regex, '', $value) ?? \rtrim($value);
155 }
156 return \rtrim($value, $charlist);
157 }
158 /**
159 * Squish the string.
160 *
161 * @param string $value The string to squish.
162 *
163 * @return string The squished string.
164 *
165 * @since 1.0.0
166 */
167 public static function squish($value)
168 {
169 return \preg_replace('~(\\s|\\x{3164}|\\x{1160})+~u', ' ', static::trim($value));
170 }
171 /**
172 * Get a substring of the string.
173 *
174 * @param string $value The string to get a substring from.
175 * @param int $start The starting position of the substring.
176 * @param int $length The length of the substring.
177 * @param string $encoding The encoding of the string.
178 *
179 * @return string The substring.
180 *
181 * @since 1.0.0
182 */
183 public static function substr($value, $start, $length = null, $encoding = 'UTF-8')
184 {
185 return \mb_substr($value, $start, $length, $encoding);
186 }
187 /**
188 * Get a substring of the string.
189 *
190 * @param string $value The string to get a substring from.
191 * @param int $limit The limit of the substring.
192 *
193 * @return string The substring.
194 *
195 * @since 1.0.0
196 */
197 public static function take($value, int $limit)
198 {
199 if ($limit < 0) {
200 return static::substr($value, $limit);
201 }
202 return static::substr($value, 0, $limit);
203 }
204 /**
205 * Convert the string to base64.
206 *
207 * @param string $value The string to convert to base64.
208 *
209 * @return string The base64 string.
210 *
211 * @since 1.0.0
212 */
213 public static function to_base64($value)
214 {
215 return \base64_encode($value);
216 }
217 /**
218 * Uuid.
219 *
220 * @return void
221 *
222 * @since 1.0.0
223 */
224 public static function uuid()
225 {
226 return wp_generate_uuid4();
227 }
228 /**
229 * Replace the string.
230 *
231 * @param string|array $search The string to search for.
232 * @param string|array $replace The string to replace with.
233 * @param string|array $subject The string to replace in.
234 * @param bool $case_sensitive Whether to perform a case-sensitive search.
235 *
236 * @return string The replaced string.
237 *
238 * @since 1.0.0
239 */
240 public static function replace($search, $replace, $subject, $case_sensitive = \true)
241 {
242 if ($search instanceof Traversable) {
243 $search = Arr::from($search);
244 }
245 if ($replace instanceof Traversable) {
246 $replace = Arr::from($replace);
247 }
248 if ($subject instanceof Traversable) {
249 $subject = Arr::from($subject);
250 }
251 return $case_sensitive ? \str_replace($search, $replace, $subject) : \str_ireplace($search, $replace, $subject);
252 }
253 /**
254 * Convert the string to uppercase.
255 *
256 * @param string $value The string to convert to uppercase.
257 *
258 * @return string The uppercase string.
259 *
260 * @since 1.0.0
261 */
262 public static function upper($value)
263 {
264 return \mb_strtoupper($value, 'UTF-8');
265 }
266 /**
267 * Convert the string to lowercase.
268 *
269 * @param string $value The string to convert to lowercase.
270 *
271 * @return string The lowercase string.
272 *
273 * @since 1.0.0
274 */
275 public static function lower($value)
276 {
277 return \mb_strtolower($value, 'UTF-8');
278 }
279 /**
280 * Convert the first character of the string to uppercase.
281 *
282 * @param string $value The string to convert.
283 *
284 * @return string The string with the first character converted to uppercase.
285 *
286 * @since 1.0.0
287 */
288 public static function ucfirst($value)
289 {
290 return static::upper(static::substr($value, 0, 1)) . static::substr($value, 1);
291 }
292 /**
293 * Convert the first character of the string to lowercase.
294 *
295 * @param string $value The string to convert.
296 *
297 * @return string The string with the first character converted to lowercase.
298 *
299 * @since 1.0.0
300 */
301 public static function lcfirst($value)
302 {
303 return static::lower(static::substr($value, 0, 1)) . static::substr($value, 1);
304 }
305 /**
306 * Convert the first character of each word in the string to uppercase.
307 *
308 * @param string $value The string to convert.
309 * @param string $separators The characters to consider as word separators.
310 *
311 * @return string The string with the first character of each word converted to uppercase.
312 *
313 * @since 1.0.0
314 */
315 public static function ucwords($value, $separators = " \t\r\n\f\v")
316 {
317 $pattern = '/(^|[' . \preg_quote($separators, '/') . '])(\\p{Ll})/u';
318 return \preg_replace_callback($pattern, function ($matches) {
319 return $matches[1] . \mb_strtoupper($matches[2]);
320 }, $value);
321 }
322 /**
323 * Convert the string to pascal case.
324 *
325 * @param string $value The string to convert.
326 *
327 * @return string The pascal case string.
328 *
329 * @since 1.0.0
330 */
331 public static function pascal($value)
332 {
333 $words = \mb_split('\\s+', static::replace(['-', '_'], ' ', $value));
334 return \implode(\array_map(function ($word) {
335 return static::ucfirst($word);
336 }, $words));
337 }
338 /**
339 * Convert the string to camel case.
340 *
341 * @param string $value The string to convert.
342 *
343 * @return string The camel case string.
344 *
345 * @since 1.0.0
346 */
347 public static function camel($value)
348 {
349 return static::lcfirst(static::pascal($value));
350 }
351 /**
352 * Convert the string to snake case.
353 *
354 * @param string $value The string to convert.
355 *
356 * @return string The snake case string.
357 *
358 * @since 1.0.0
359 */
360 public static function snake($value)
361 {
362 return static::lower(static::replace([' ', '-'], '_', $value));
363 }
364 /**
365 * Determine if a given string starts with a given substring.
366 *
367 * @param string $haystack The string to search in.
368 * @param string|array $needles The substring to search for.
369 *
370 * @return bool Whether the string starts with the substring.
371 *
372 * @since 1.0.0
373 */
374 public static function starts_with($haystack, $needles)
375 {
376 if (\is_null($haystack)) {
377 return \false;
378 }
379 $needles = Arr::wrap($needles);
380 foreach ($needles as $needle) {
381 if (!empty((string) $needle) && str_starts_with($haystack, $needle)) {
382 return \true;
383 }
384 }
385 return \false;
386 }
387 /**
388 * Determine if a given string ends with a given substring.
389 *
390 * @param string $haystack The string to search in.
391 * @param string|iterable<string> $needles The substring to search for.
392 *
393 * @return bool Whether the string ends with the substring.
394 *
395 * @since 1.0.0
396 */
397 public static function ends_with($haystack, $needles)
398 {
399 if (\is_null($haystack)) {
400 return \false;
401 }
402 if (!\is_iterable($needles)) {
403 $needles = (array) $needles;
404 }
405 foreach ($needles as $needle) {
406 if ((string) $needle !== '' && str_ends_with($haystack, $needle)) {
407 return \true;
408 }
409 }
410 return \false;
411 }
412 /**
413 * Split the string into an array.
414 *
415 * @param string $separator The separator to use.
416 * @param string $value The string to split.
417 * @param bool $case_sensitive Whether to perform a case-sensitive search.
418 *
419 * @return array The array of strings.
420 *
421 * @since 1.0.0
422 */
423 public static function split(string $separator, string $value, bool $case_sensitive = \false)
424 {
425 $pattern = "/" . \preg_quote($separator, '/');
426 $pattern .= $case_sensitive ? '/' : '/i';
427 return \preg_split($pattern, $value);
428 }
429 /**
430 * After.
431 *
432 * @param string $subject The subject.
433 * @param string $search The search.
434 *
435 * @return void
436 *
437 * @since 1.0.0
438 */
439 public static function after(string $subject, string $search)
440 {
441 if (empty($search)) {
442 return $subject;
443 }
444 $position = \strpos($subject, $search);
445 if ($position === \false) {
446 return $subject;
447 }
448 return \substr($subject, $position + \strlen($search));
449 }
450 /**
451 * Before.
452 *
453 * @param string $subject The subject.
454 * @param string $search The search.
455 *
456 * @return void
457 *
458 * @since 1.0.0
459 */
460 public static function before(string $subject, string $search)
461 {
462 if (empty($search)) {
463 return $subject;
464 }
465 $position = \strpos($subject, $search);
466 if ($position === \false) {
467 return $subject;
468 }
469 return \substr($subject, 0, $position);
470 }
471 /**
472 * After last.
473 *
474 * @param string $subject The subject.
475 * @param string $search The search.
476 *
477 * @return string
478 *
479 * @since 1.0.0
480 */
481 public static function after_last(string $subject, string $search)
482 {
483 if (empty($search)) {
484 return $subject;
485 }
486 $position = \strrpos($subject, $search);
487 if ($position === \false) {
488 return $subject;
489 }
490 return \substr($subject, $position + \strlen($search));
491 }
492 /**
493 * Before last.
494 *
495 * @param string $subject The subject.
496 * @param string $search The search.
497 *
498 * @return string
499 *
500 * @since 1.0.0
501 */
502 public static function before_last(string $subject, string $search)
503 {
504 if (empty($search)) {
505 return $subject;
506 }
507 $position = \strrpos($subject, $search);
508 if ($position === \false) {
509 return $subject;
510 }
511 return \substr($subject, 0, $position);
512 }
513 }
514