PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 2.3.0
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v2.3.0
2.4.0 2.3.0 2.2.5 2.2.0 2.1.2 2.1.1 trunk 1.10.0 1.10.01 1.10.02 1.5.0 1.5.01 1.5.02 1.5.1 1.5.10 1.5.20 1.5.21 1.5.22 1.5.23 1.5.24 1.5.25 1.6.0 1.7.0 1.7.1 1.7.2 All 33 releases
fluent-booking / vendor / wpfluent / framework / src / WPFluent / Support / Stringable.php

Stringable.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 2.3.0, at vendor/wpfluent/framework/src/WPFluent/Support/Stringable.php

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