PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.41
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.41
2.1.0 2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 All 42 releases
fluent-boards / vendor / wpfluent / framework / src / WPFluent / Support / Stringable.php

Stringable.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 1.41, at vendor/wpfluent/framework/src/WPFluent/Support/Stringable.php

1,346 lines 31.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBoards\Framework\Support;
4
5 use Closure;
6 use ArrayAccess;
7 use JsonSerializable;
8 use FluentBoards\Framework\Support\DateTime;
9 use FluentBoards\Framework\Support\Tappable;
10 use FluentBoards\Framework\Support\Conditionable;
11 use FluentBoards\Framework\Support\MacroableTrait;
12 use FluentBoards\Framework\Support\HelperFunctionsTrait;
13
14 class Stringable implements JsonSerializable
15 {
16 use Tappable, Conditionable, MacroableTrait, HelperFunctionsTrait;
17
18 /**
19 * The underlying string value.
20 *
21 * @var string
22 */
23 protected $value;
24
25 /**
26 * Create a new instance of the class.
27 *
28 * @param string $value
29 * @return void
30 */
31 public function __construct($value = '')
32 {
33 $this->value = (string) $value;
34 }
35
36 /**
37 * Makes an acronum from a string of words
38 *
39 * @param string $delimiter
40 * @return self
41 */
42 public function acronym(string $delimiter = '')
43 {
44 return new static(Str::acronym($this->value, $delimiter));
45 }
46
47 /**
48 * Return the remainder of a string after the first occurrence of a given value.
49 *
50 * @param string $search
51 * @return static
52 */
53 public function after($search)
54 {
55 return new static(Str::after($this->value, $search));
56 }
57
58 /**
59 * Return the remainder of a string after the last occurrence of a given value.
60 *
61 * @param string $search
62 * @return static
63 */
64 public function afterLast($search)
65 {
66 return new static(Str::afterLast($this->value, $search));
67 }
68
69 /**
70 * Append the given values to the string.
71 *
72 * @param array|string ...$values
73 * @return static
74 */
75 public function append(...$values)
76 {
77 return new static($this->value.implode('', $values));
78 }
79
80 /**
81 * Append a new line to the string.
82 *
83 * @param int $count
84 * @return $this
85 */
86 public function newLine($count = 1)
87 {
88 return $this->append(str_repeat(PHP_EOL, $count));
89 }
90
91 /**
92 * Transliterate a UTF-8 value to ASCII.
93 *
94 * @param string $language
95 * @return static
96 */
97 public function ascii($language = 'en')
98 {
99 return new static(Str::ascii($this->value, $language));
100 }
101
102 /**
103 * Checks if the word(s) are in capitalized form.
104 *
105 * @param boolean $onlyFirst if true, checks only the first charatcer
106 * @return boolean
107 */
108 public function isCapitalized($onlyFirst = false)
109 {
110 return Str::isCapitalized($this->value, $onlyFirst);
111 }
112
113 /**
114 * Checks if the first character is in capital form.
115 *
116 * @return boolean
117 */
118 public function isCapital()
119 {
120 return $this->isCapitalized(true);
121 }
122
123 /**
124 * Checks if the chracters are in upper case
125 *
126 * @return boolean
127 */
128 public static function isUpper()
129 {
130 return Str::isUpper($this->value);
131 }
132
133 /**
134 * Checks if the chracters are in lower case
135 *
136 * @return boolean
137 */
138 public static function isLower()
139 {
140 return Str::isLower($this->value);
141 }
142
143 /**
144 * Checks if two words sounds alike
145 *
146 * @param string $str
147 *
148 * @return bool
149 */
150 public function soundsAlike($str)
151 {
152 return Str::soundsAlike($this->value, $str);
153 }
154
155 /**
156 * Checks if two words are similar
157 *
158 * @param string $str
159 * @param int $accuracy
160 *
161 * @return bool
162 */
163 public function isSimilar($str, $accuracy = 50)
164 {
165 return Str::isSimilar($this->value, $str, $accuracy);
166 }
167
168 /**
169 * Checks if two words are similar
170 *
171 * @param string $str
172 *
173 * @return bool
174 */
175 public function similarityOf($str)
176 {
177 return Str::similarityOf($this->value, $str);
178 }
179
180 /**
181 * Get the trailing name component of the path.
182 *
183 * @param string $suffix
184 * @return static
185 */
186 public function basename($suffix = '')
187 {
188 return new static(basename($this->value, $suffix));
189 }
190
191 /**
192 * Get the character at the specified index.
193 *
194 * @param int $index
195 * @return string|false
196 */
197 public function charAt($index)
198 {
199 return Str::charAt($this->value, $index);
200 }
201
202 /**
203 * Get the basename of the class path.
204 *
205 * @return static
206 */
207 public function classBasename()
208 {
209 return new static(static::classBasename($this->value));
210 }
211
212 /**
213 * Get the portion of a string before the first occurrence of a given value.
214 *
215 * @param string $search
216 * @return static
217 */
218 public function before($search)
219 {
220 return new static(Str::before($this->value, $search));
221 }
222
223 /**
224 * Get the portion of a string before the last occurrence of a given value.
225 *
226 * @param string $search
227 * @return static
228 */
229 public function beforeLast($search)
230 {
231 return new static(Str::beforeLast($this->value, $search));
232 }
233
234 /**
235 * Get the portion of a string between two given values.
236 *
237 * @param string $from
238 * @param string $to
239 * @return static
240 */
241 public function between($from, $to)
242 {
243 return new static(Str::between($this->value, $from, $to));
244 }
245
246 /**
247 * Get the smallest possible portion of a string between two given values.
248 *
249 * @param string $from
250 * @param string $to
251 * @return static
252 */
253 public function betweenFirst($from, $to)
254 {
255 return new static(Str::betweenFirst($this->value, $from, $to));
256 }
257
258 /**
259 * Convert a value to camel case.
260 *
261 * @return static
262 */
263 public function camel()
264 {
265 return new static(Str::camel($this->value));
266 }
267
268 /**
269 * Determine if a given string contains a given substring.
270 *
271 * @param string|iterable<string> $needles
272 * @param bool $ignoreCase
273 * @return bool
274 */
275 public function contains($needles, $ignoreCase = false)
276 {
277 return Str::contains($this->value, $needles, $ignoreCase);
278 }
279
280 /**
281 * Determine if a given string contains all array values.
282 *
283 * @param iterable<string> $needles
284 * @param bool $ignoreCase
285 * @return bool
286 */
287 public function containsAll($needles, $ignoreCase = false)
288 {
289 return Str::containsAll($this->value, $needles, $ignoreCase);
290 }
291
292 /**
293 * Replace consecutive instances of a given character with a single character.
294 *
295 * @param string $character
296 * @return static
297 */
298 public function deduplicate(string $character = ' ')
299 {
300 return new static(Str::deduplicate($this->value, $character));
301 }
302
303 /**
304 * Get the parent directory's path.
305 *
306 * @param int $levels
307 * @return static
308 */
309 public function dirname($levels = 1)
310 {
311 return new static(dirname($this->value, $levels));
312 }
313
314 /**
315 * Determine if a given string ends with a given substring.
316 *
317 * @param string|iterable<string> $needles
318 * @return bool
319 */
320 public function endsWith($needles)
321 {
322 return Str::endsWith($this->value, $needles);
323 }
324
325 /**
326 * Determine if the string is an exact match with the given value.
327 *
328 * @param \FluentBoards\Framework\Support\Stringable|string $value
329 * @return bool
330 */
331 public function exactly($value)
332 {
333 if ($value instanceof Stringable) {
334 $value = $value->toString();
335 }
336
337 return $this->value === $value;
338 }
339
340 /**
341 * Explode the string into an array.
342 *
343 * @param string $delimiter
344 * @param int $limit
345 * @return \FluentBoards\Framework\Support\Collection<int, string>
346 */
347 public function explode($delimiter, $limit = PHP_INT_MAX)
348 {
349 return Helper::collect(explode($delimiter, $this->value, $limit));
350 }
351
352 /**
353 * Split a string using a regular expression or by length.
354 *
355 * @param string|int $pattern
356 * @param int $limit
357 * @param int $flags
358 * @return \FluentBoards\Framework\Support\Collection<int, string>
359 */
360 public function split($pattern, $limit = -1, $flags = 0)
361 {
362 if (filter_var($pattern, FILTER_VALIDATE_INT) !== false) {
363 return Helper::collect(mb_str_split($this->value, $pattern));
364 }
365
366 $segments = preg_split($pattern, $this->value, $limit, $flags);
367
368 return ! empty($segments) ? Helper::collect($segments) : Helper::collect();
369 }
370
371 /**
372 * Cap a string with a single instance of a given value.
373 *
374 * @param string $cap
375 * @return static
376 */
377 public function finish($cap)
378 {
379 return new static(Str::finish($this->value, $cap));
380 }
381
382 /**
383 * Determine if a given string matches a given pattern.
384 *
385 * @param string|iterable<string> $pattern
386 * @return bool
387 */
388 public function is($pattern)
389 {
390 return Str::is($pattern, $this->value);
391 }
392
393 /**
394 * Determine if a given string is 7 bit ASCII.
395 *
396 * @return bool
397 */
398 public function isAscii()
399 {
400 return Str::isAscii($this->value);
401 }
402
403 /**
404 * Determine if a given string is valid JSON.
405 *
406 * @return bool
407 */
408 public function isJson()
409 {
410 return Str::isJson($this->value);
411 }
412
413 /**
414 * Determine if a given value is a valid URL.
415 *
416 * @return bool
417 */
418 public function isUrl()
419 {
420 return Str::isUrl($this->value);
421 }
422
423 /**
424 * Determine if a given string is a valid UUID.
425 *
426 * @return bool
427 */
428 public function isUuid()
429 {
430 return Str::isUuid($this->value);
431 }
432
433 /**
434 * Determine if the given string is empty.
435 *
436 * @return bool
437 */
438 public function isEmpty()
439 {
440 return $this->value === '';
441 }
442
443 /**
444 * Determine if the given string is not empty.
445 *
446 * @return bool
447 */
448 public function isNotEmpty()
449 {
450 return ! $this->isEmpty();
451 }
452
453 /**
454 * Convert a string to kebab case.
455 *
456 * @return static
457 */
458 public function kebab()
459 {
460 return new static(Str::kebab($this->value));
461 }
462
463 /**
464 * Return the length of the given string.
465 *
466 * @param string|null $encoding
467 * @return int
468 */
469 public function length($encoding = null)
470 {
471 return Str::length($this->value, $encoding);
472 }
473
474 /**
475 * Limit the number of characters in a string.
476 *
477 * @param int $limit
478 * @param string $end
479 * @return static
480 */
481 public function limit($limit = 100, $end = '...')
482 {
483 return new static(Str::limit($this->value, $limit, $end));
484 }
485
486 /**
487 * Convert the given string to lower-case.
488 *
489 * @return static
490 */
491 public function lower()
492 {
493 return new static(Str::lower($this->value));
494 }
495
496 /**
497 * Masks a portion of a string with a repeated character.
498 *
499 * @param string $character
500 * @param int $index
501 * @param int|null $length
502 * @param string $encoding
503 * @return static
504 */
505 public function mask($character, $index, $length = null, $encoding = 'UTF-8')
506 {
507 return new static(Str::mask($this->value, $character, $index, $length, $encoding));
508 }
509
510 /**
511 * Get the string matching the given pattern.
512 *
513 * @param string $pattern
514 * @return static
515 */
516 public function match($pattern)
517 {
518 return new static(Str::match($pattern, $this->value));
519 }
520
521 /**
522 * Determine if a given string matches a given pattern.
523 *
524 * @param string|iterable<string> $pattern
525 * @return bool
526 */
527 public function isMatch($pattern)
528 {
529 return Str::isMatch($pattern, $this->value);
530 }
531
532 /**
533 * Get the string matching the given pattern.
534 *
535 * @param string $pattern
536 * @return \FluentBoards\Framework\Support\Collection
537 */
538 public function matchAll($pattern)
539 {
540 return Str::matchAll($pattern, $this->value);
541 }
542
543 /**
544 * Determine if the string matches the given pattern.
545 *
546 * @param string $pattern
547 * @return bool
548 */
549 public function test($pattern)
550 {
551 return $this->isMatch($pattern);
552 }
553
554 /**
555 * Pad both sides of the string with another.
556 *
557 * @param int $length
558 * @param string $pad
559 * @return static
560 */
561 public function padBoth($length, $pad = ' ')
562 {
563 return new static(Str::padBoth($this->value, $length, $pad));
564 }
565
566 /**
567 * Pad the left side of the string with another.
568 *
569 * @param int $length
570 * @param string $pad
571 * @return static
572 */
573 public function padLeft($length, $pad = ' ')
574 {
575 return new static(Str::padLeft($this->value, $length, $pad));
576 }
577
578 /**
579 * Pad the right side of the string with another.
580 *
581 * @param int $length
582 * @param string $pad
583 * @return static
584 */
585 public function padRight($length, $pad = ' ')
586 {
587 return new static(Str::padRight($this->value, $length, $pad));
588 }
589
590 /**
591 * Parse a Class@method style callback into class and method.
592 *
593 * @param string|null $default
594 * @return array<int, string|null>
595 */
596 public function parseCallback($default = null)
597 {
598 return Str::parseCallback($this->value, $default);
599 }
600
601 /**
602 * Parse an integer from a string.
603 *
604 * @param string $value
605 * @return int|null
606 */
607 public static function parseInt($value)
608 {
609 return Str::parseInt($value);
610 }
611
612 /**
613 * Parse a floasting point number from a string.
614 *
615 * @param string $value
616 * @return float|null
617 */
618 public static function parseFloat($value)
619 {
620 return Str::parseFloat($value);
621 }
622
623 /**
624 * Remove all non-numeric characters from a string.
625 *
626 * @param string $value
627 * @return string
628 */
629 public static function parseNumber($value)
630 {
631 return Str::parseNumber($value);
632 }
633
634 /**
635 * Call the given callback and return a new string.
636 *
637 * @param callable $callback
638 * @return static
639 */
640 public function pipe(callable $callback)
641 {
642 return new static($callback($this));
643 }
644
645 /**
646 * Get the plural form of an English word.
647 *
648 * @param int|array|\Countable $count
649 * @return static
650 */
651 public function plural($count = 2)
652 {
653 return new static(Str::plural($this->value, $count));
654 }
655
656 /**
657 * Pluralize the last word of an English, studly caps case string.
658 *
659 * @param int|array|\Countable $count
660 * @return static
661 */
662 public function pluralStudly($count = 2)
663 {
664 return new static(Str::pluralStudly($this->value, $count));
665 }
666
667 /**
668 * Prepend the given values to the string.
669 *
670 * @param string ...$values
671 * @return static
672 */
673 public function prepend(...$values)
674 {
675 return new static(implode('', $values).$this->value);
676 }
677
678 /**
679 * Remove any occurrence of the given string in the subject.
680 *
681 * @param string|iterable<string> $search
682 * @param bool $caseSensitive
683 * @return static
684 */
685 public function remove($search, $caseSensitive = true)
686 {
687 return new static(Str::remove($search, $this->value, $caseSensitive));
688 }
689
690 /**
691 * Reverse the string.
692 *
693 * @return static
694 */
695 public function reverse()
696 {
697 return new static(Str::reverse($this->value));
698 }
699
700 /**
701 * Repeat the string.
702 *
703 * @param int $times
704 * @return static
705 */
706 public function repeat(int $times)
707 {
708 return new static(str_repeat($this->value, $times));
709 }
710
711 /**
712 * Replace the given value in the given string.
713 *
714 * @param string|iterable<string> $search
715 * @param string|iterable<string> $replace
716 * @param bool $caseSensitive
717 * @return static
718 */
719 public function replace($search, $replace, $caseSensitive = true)
720 {
721 return new static(Str::replace($search, $replace, $this->value, $caseSensitive));
722 }
723
724 /**
725 * Replace a given value in the string sequentially with an array.
726 *
727 * @param string $search
728 * @param iterable<string> $replace
729 * @return static
730 */
731 public function replaceArray($search, $replace)
732 {
733 return new static(Str::replaceArray($search, $replace, $this->value));
734 }
735
736 /**
737 * Replace the first occurrence of a given value in the string.
738 *
739 * @param string $search
740 * @param string $replace
741 * @return static
742 */
743 public function replaceFirst($search, $replace)
744 {
745 return new static(Str::replaceFirst($search, $replace, $this->value));
746 }
747
748 /**
749 * Replace the last occurrence of a given value in the string.
750 *
751 * @param string $search
752 * @param string $replace
753 * @return static
754 */
755 public function replaceLast($search, $replace)
756 {
757 return new static(Str::replaceLast($search, $replace, $this->value));
758 }
759
760 /**
761 * Replace the patterns matching the given regular expression.
762 *
763 * @param string $pattern
764 * @param \Closure|string $replace
765 * @param int $limit
766 * @return static
767 */
768 public function replaceMatches($pattern, $replace, $limit = -1)
769 {
770 if ($replace instanceof Closure) {
771 return new static(preg_replace_callback($pattern, $replace, $this->value, $limit));
772 }
773
774 return new static(preg_replace($pattern, $replace, $this->value, $limit));
775 }
776
777 /**
778 * Parse input from a string to a collection, according to a format.
779 *
780 * @param string $format
781 * @return \FluentBoards\Framework\Support\Collection
782 */
783 public function scan($format)
784 {
785 return Helper::collect(sscanf($this->value, $format));
786 }
787
788 /**
789 * Remove all "extra" blank space from the given string.
790 *
791 * @return static
792 */
793 public function squish()
794 {
795 return new static(Str::squish($this->value));
796 }
797
798 /**
799 * Begin a string with a single instance of a given value.
800 *
801 * @param string $prefix
802 * @return static
803 */
804 public function start($prefix)
805 {
806 return new static(Str::start($this->value, $prefix));
807 }
808
809 /**
810 * Strip HTML and PHP tags from the given string.
811 *
812 * @param string $allowedTags
813 * @return static
814 */
815 public function stripTags($allowedTags = null)
816 {
817 return new static(strip_tags($this->value, $allowedTags));
818 }
819
820 /**
821 * Convert the given string to upper-case.
822 *
823 * @return static
824 */
825 public function upper()
826 {
827 return new static(Str::upper($this->value));
828 }
829
830 /**
831 * Convert the given string to title case.
832 *
833 * @return static
834 */
835 public function title()
836 {
837 return new static(Str::title($this->value));
838 }
839
840 /**
841 * Convert the given string to title case for each word.
842 *
843 * @return static
844 */
845 public function headline()
846 {
847 return new static(Str::headline($this->value));
848 }
849
850 /**
851 * Get the singular form of an English word.
852 *
853 * @return static
854 */
855 public function singular()
856 {
857 return new static(Str::singular($this->value));
858 }
859
860 /**
861 * Generate a URL friendly "slug" from a given string.
862 *
863 * @param string $separator
864 * @param string|null $language
865 * @param array<string, string> $dictionary
866 * @return static
867 */
868 public function slug($separator = '-', $language = 'en', $dictionary = ['@' => 'at'])
869 {
870 return new static(Str::slug($this->value, $separator, $language, $dictionary));
871 }
872
873 /**
874 * Convert a string to snake case.
875 *
876 * @param string $delimiter
877 * @return static
878 */
879 public function snake($delimiter = '_')
880 {
881 return new static(Str::snake($this->value, $delimiter));
882 }
883
884 /**
885 * Determine if a given string starts with a given substring.
886 *
887 * @param string|iterable<string> $needles
888 * @return bool
889 */
890 public function startsWith($needles)
891 {
892 return Str::startsWith($this->value, $needles);
893 }
894
895 /**
896 * Convert a value to studly caps case.
897 *
898 * @return static
899 */
900 public function studly()
901 {
902 return new static(Str::studly($this->value));
903 }
904
905 /**
906 * Returns the portion of the string specified by the start and length parameters.
907 *
908 * @param int $start
909 * @param int|null $length
910 * @param string $encoding
911 * @return static
912 */
913 public function substr($start, $length = null, $encoding = 'UTF-8')
914 {
915 return new static(Str::substr($this->value, $start, $length, $encoding));
916 }
917
918 /**
919 * Returns the number of substring occurrences.
920 *
921 * @param string $needle
922 * @param int $offset
923 * @param int|null $length
924 * @return int
925 */
926 public function substrCount($needle, $offset = 0, $length = null)
927 {
928 return Str::substrCount($this->value, $needle, $offset, $length);
929 }
930
931 /**
932 * Replace text within a portion of a string.
933 *
934 * @param string|string[] $replace
935 * @param int|int[] $offset
936 * @param int|int[]|null $length
937 * @return static
938 */
939 public function substrReplace($replace, $offset = 0, $length = null)
940 {
941 return new static(Str::substrReplace($this->value, $replace, $offset, $length));
942 }
943
944 /**
945 * Swap multiple keywords in a string with other keywords.
946 *
947 * @param array $map
948 * @return static
949 */
950 public function swap(array $map)
951 {
952 return new static(strtr($this->value, $map));
953 }
954
955 /**
956 * Trim the string of the given characters.
957 *
958 * @param string $characters
959 * @return static
960 */
961 public function trim($characters = null)
962 {
963 return new static(trim(...array_merge([$this->value], func_get_args())));
964 }
965
966 /**
967 * Left trim the string of the given characters.
968 *
969 * @param string $characters
970 * @return static
971 */
972 public function ltrim($characters = null)
973 {
974 return new static(ltrim(...array_merge([$this->value], func_get_args())));
975 }
976
977 /**
978 * Right trim the string of the given characters.
979 *
980 * @param string $characters
981 * @return static
982 */
983 public function rtrim($characters = null)
984 {
985 return new static(rtrim(...array_merge([$this->value], func_get_args())));
986 }
987
988 /**
989 * Make a string's first character lowercase.
990 *
991 * @return static
992 */
993 public function lcfirst()
994 {
995 return new static(Str::lcfirst($this->value));
996 }
997
998 /**
999 * Make a string's first character uppercase.
1000 *
1001 * @return static
1002 */
1003 public function ucfirst()
1004 {
1005 return new static(Str::ucfirst($this->value));
1006 }
1007
1008 /**
1009 * Split a string by uppercase characters.
1010 *
1011 * @return \FluentBoards\Framework\Support\Collection<int, string>
1012 */
1013 public function ucsplit()
1014 {
1015 return Helper::collect(Str::ucsplit($this->value));
1016 }
1017
1018 /**
1019 * Execute the given callback if the string contains a given substring.
1020 *
1021 * @param string|iterable<string> $needles
1022 * @param callable $callback
1023 * @param callable|null $default
1024 * @return static
1025 */
1026 public function whenContains($needles, $callback, $default = null)
1027 {
1028 return $this->when($this->contains($needles), $callback, $default);
1029 }
1030
1031 /**
1032 * Execute the given callback if the string contains all array values.
1033 *
1034 * @param iterable<string> $needles
1035 * @param callable $callback
1036 * @param callable|null $default
1037 * @return static
1038 */
1039 public function whenContainsAll(array $needles, $callback, $default = null)
1040 {
1041 return $this->when($this->containsAll($needles), $callback, $default);
1042 }
1043
1044 /**
1045 * Execute the given callback if the string is empty.
1046 *
1047 * @param callable $callback
1048 * @param callable|null $default
1049 * @return static
1050 */
1051 public function whenEmpty($callback, $default = null)
1052 {
1053 return $this->when($this->isEmpty(), $callback, $default);
1054 }
1055
1056 /**
1057 * Execute the given callback if the string is not empty.
1058 *
1059 * @param callable $callback
1060 * @param callable|null $default
1061 * @return static
1062 */
1063 public function whenNotEmpty($callback, $default = null)
1064 {
1065 return $this->when($this->isNotEmpty(), $callback, $default);
1066 }
1067
1068 /**
1069 * Execute the given callback if the string ends with a given substring.
1070 *
1071 * @param string|iterable<string> $needles
1072 * @param callable $callback
1073 * @param callable|null $default
1074 * @return static
1075 */
1076 public function whenEndsWith($needles, $callback, $default = null)
1077 {
1078 return $this->when($this->endsWith($needles), $callback, $default);
1079 }
1080
1081 /**
1082 * Execute the given callback if the string is an exact match with the given value.
1083 *
1084 * @param string $value
1085 * @param callable $callback
1086 * @param callable|null $default
1087 * @return static
1088 */
1089 public function whenExactly($value, $callback, $default = null)
1090 {
1091 return $this->when($this->exactly($value), $callback, $default);
1092 }
1093
1094 /**
1095 * Execute the given callback if the string is not an exact match with the given value.
1096 *
1097 * @param string $value
1098 * @param callable $callback
1099 * @param callable|null $default
1100 * @return static
1101 */
1102 public function whenNotExactly($value, $callback, $default = null)
1103 {
1104 return $this->when(! $this->exactly($value), $callback, $default);
1105 }
1106
1107 /**
1108 * Execute the given callback if the string matches a given pattern.
1109 *
1110 * @param string|iterable<string> $pattern
1111 * @param callable $callback
1112 * @param callable|null $default
1113 * @return static
1114 */
1115 public function whenIs($pattern, $callback, $default = null)
1116 {
1117 return $this->when($this->is($pattern), $callback, $default);
1118 }
1119
1120 /**
1121 * Execute the given callback if the string is 7 bit ASCII.
1122 *
1123 * @param callable $callback
1124 * @param callable|null $default
1125 * @return static
1126 */
1127 public function whenIsAscii($callback, $default = null)
1128 {
1129 return $this->when($this->isAscii(), $callback, $default);
1130 }
1131
1132 /**
1133 * Execute the given callback if the string is a valid UUID.
1134 *
1135 * @param callable $callback
1136 * @param callable|null $default
1137 * @return static
1138 */
1139 public function whenIsUuid($callback, $default = null)
1140 {
1141 return $this->when($this->isUuid(), $callback, $default);
1142 }
1143
1144 /**
1145 * Execute the given callback if the string starts with a given substring.
1146 *
1147 * @param string|iterable<string> $needles
1148 * @param callable $callback
1149 * @param callable|null $default
1150 * @return static
1151 */
1152 public function whenStartsWith($needles, $callback, $default = null)
1153 {
1154 return $this->when($this->startsWith($needles), $callback, $default);
1155 }
1156
1157 /**
1158 * Execute the given callback if the string matches the given pattern.
1159 *
1160 * @param string $pattern
1161 * @param callable $callback
1162 * @param callable|null $default
1163 * @return static
1164 */
1165 public function whenTest($pattern, $callback, $default = null)
1166 {
1167 return $this->when($this->test($pattern), $callback, $default);
1168 }
1169
1170 /**
1171 * Limit the number of words in a string.
1172 *
1173 * @param int $words
1174 * @param string $end
1175 * @return static
1176 */
1177 public function words($words = 100, $end = '...')
1178 {
1179 return new static(Str::words($this->value, $words, $end));
1180 }
1181
1182 /**
1183 * Get the number of words a string contains.
1184 *
1185 * @param string|null $characters
1186 * @return int
1187 */
1188 public function wordCount($characters = null)
1189 {
1190 return Str::wordCount($this->value, $characters);
1191 }
1192
1193 /**
1194 * Wrap a string to a given number of characters.
1195 *
1196 * @param int $characters
1197 * @param string $break
1198 * @param bool $cutLongWords
1199 * @return static
1200 */
1201 public function wordWrap($characters = 75, $break = "\n", $cutLongWords = false)
1202 {
1203 return new static(Str::wordWrap($this->value, $characters, $break, $cutLongWords));
1204 }
1205
1206 /**
1207 * Wrap the string with the given strings.
1208 *
1209 * @param string $before
1210 * @param string|null $after
1211 * @return static
1212 */
1213 public function wrap($before, $after = null)
1214 {
1215 return new static(Str::wrap($this->value, $before, $after));
1216 }
1217
1218 /**
1219 * Dump the string.
1220 *
1221 * @return $this
1222 */
1223 public function dump()
1224 {
1225 echo "<pre>";
1226 print_r($this->value);
1227 echo "</pre>";
1228
1229 return $this;
1230 }
1231
1232 /**
1233 * Dump the string and end the script.
1234 *
1235 * @return never
1236 */
1237 public function dd()
1238 {
1239 $this->dump();
1240
1241 exit(1);
1242 }
1243
1244 /**
1245 * Get the underlying string value.
1246 *
1247 * @return string
1248 */
1249 public function value()
1250 {
1251 return $this->toString();
1252 }
1253
1254 /**
1255 * Get the underlying string value.
1256 *
1257 * @return string
1258 */
1259 public function toString()
1260 {
1261 return $this->value;
1262 }
1263
1264 /**
1265 * Get the underlying string value as an integer.
1266 *
1267 * @return int
1268 */
1269 public function toInteger()
1270 {
1271 return intval($this->value);
1272 }
1273
1274 /**
1275 * Get the underlying string value as a float.
1276 *
1277 * @return float
1278 */
1279 public function toFloat()
1280 {
1281 return floatval($this->value);
1282 }
1283
1284 /**
1285 * Get the underlying string value as a boolean.
1286 *
1287 * Returns true when value is "1", "true", "on", and "yes". Otherwise, returns false.
1288 *
1289 * @return bool
1290 */
1291 public function toBoolean()
1292 {
1293 return filter_var($this->value, FILTER_VALIDATE_BOOLEAN);
1294 }
1295
1296 /**
1297 * Get the underlying string value as a Carbon instance.
1298 *
1299 * @param string|null $format
1300 * @param string|null $tz
1301 * @return \FluentBoards\Framework\Support\DateTime
1302 *
1303 * @throws \InvalidArgumentException
1304 */
1305 public function toDate($format = null, $tz = null)
1306 {
1307 if (is_null($format)) {
1308 return DateTime::parse($this->value, $tz);
1309 }
1310
1311 return DateTime::createFromFormat($format, $this->value, $tz);
1312 }
1313
1314 /**
1315 * Convert the object to a string when JSON encoded.
1316 *
1317 * @return string
1318 */
1319 #[\ReturnTypeWillChange]
1320 public function jsonSerialize()
1321 {
1322 return $this->__toString();
1323 }
1324
1325 /**
1326 * Proxy dynamic properties onto methods.
1327 *
1328 * @param string $key
1329 * @return mixed
1330 */
1331 public function __get($key)
1332 {
1333 return $this->{$key}();
1334 }
1335
1336 /**
1337 * Get the raw string value.
1338 *
1339 * @return string
1340 */
1341 public function __toString()
1342 {
1343 return (string) $this->value;
1344 }
1345 }
1346