Debian Codegraph @

lib/3rdParty/composer/illuminate/support/Stringable.php

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Support;
4
5 use ArrayAccess;
6 use Closure;
7 use Illuminate\Support\Facades\Date;
8 use Illuminate\Support\Traits\Conditionable;
9 use Illuminate\Support\Traits\Dumpable;
10 use Illuminate\Support\Traits\Macroable;
11 use Illuminate\Support\Traits\Tappable;
12 use JsonSerializable;
13 use Stringable as BaseStringable;
14
15 class Stringable implements JsonSerializable, ArrayAccess, BaseStringable
16 {
17 use Conditionable, Dumpable, Macroable, Tappable;
18
19 /**
20 * The underlying string value.
21 *
22 * @var string
23 */
24 protected $value;
25
26 /**
27 * Create a new instance of the class.
28 *
29 * @param string $value
30 */
31 public function __construct($value = '')
32 {
33 $this->value = (string) $value;
34 }
35
36 /**
37 * Return the remainder of a string after the first occurrence of a given value.
38 *
39 * @param string $search
40 * @return static
41 */
42 public function after($search)
43 {
44 return new static(Str::after($this->value, $search));
45 }
46
47 /**
48 * Return the remainder of a string after the last occurrence of a given value.
49 *
50 * @param string $search
51 * @return static
52 */
53 public function afterLast($search)
54 {
55 return new static(Str::afterLast($this->value, $search));
56 }
57
58 /**
59 * Append the given values to the string.
60 *
61 * @param array|string ...$values
62 * @return static
63 */
64 public function append(...$values)
65 {
66 return new static($this->value.implode('', $values));
67 }
68
69 /**
70 * Append a new line to the string.
71 *
72 * @param int $count
73 * @return $this
74 */
75 public function newLine($count = 1)
76 {
77 return $this->append(str_repeat(PHP_EOL, $count));
78 }
79
80 /**
81 * Transliterate a UTF-8 value to ASCII.
82 *
83 * @param string $language
84 * @return static
85 */
86 public function ascii($language = 'en')
87 {
88 return new static(Str::ascii($this->value, $language));
89 }
90
91 /**
92 * Get the trailing name component of the path.
93 *
94 * @param string $suffix
95 * @return static
96 */
97 public function basename($suffix = '')
98 {
99 return new static(basename($this->value, $suffix));
100 }
101
102 /**
103 * Get the character at the specified index.
104 *
105 * @param int $index
106 * @return string|false
107 */
108 public function charAt($index)
109 {
110 return Str::charAt($this->value, $index);
111 }
112
113 /**
114 * Remove the given string if it exists at the start of the current string.
115 *
116 * @param string|array $needle
117 * @return static
118 */
119 public function chopStart($needle)
120 {
121 return new static(Str::chopStart($this->value, $needle));
122 }
123
124 /**
125 * Remove the given string if it exists at the end of the current string.
126 *
127 * @param string|array $needle
128 * @return static
129 */
130 public function chopEnd($needle)
131 {
132 return new static(Str::chopEnd($this->value, $needle));
133 }
134
135 /**
136 * Get the basename of the class path.
137 *
138 * @return static
139 */
140 public function classBasename()
141 {
142 return new static(class_basename($this->value));
143 }
144
145 /**
146 * Get the portion of a string before the first occurrence of a given value.
147 *
148 * @param string $search
149 * @return static
150 */
151 public function before($search)
152 {
153 return new static(Str::before($this->value, $search));
154 }
155
156 /**
157 * Get the portion of a string before the last occurrence of a given value.
158 *
159 * @param string $search
160 * @return static
161 */
162 public function beforeLast($search)
163 {
164 return new static(Str::beforeLast($this->value, $search));
165 }
166
167 /**
168 * Get the portion of a string between two given values.
169 *
170 * @param string $from
171 * @param string $to
172 * @return static
173 */
174 public function between($from, $to)
175 {
176 return new static(Str::between($this->value, $from, $to));
177 }
178
179 /**
180 * Get the smallest possible portion of a string between two given values.
181 *
182 * @param string $from
183 * @param string $to
184 * @return static
185 */
186 public function betweenFirst($from, $to)
187 {
188 return new static(Str::betweenFirst($this->value, $from, $to));
189 }
190
191 /**
192 * Convert a value to camel case.
193 *
194 * @return static
195 */
196 public function camel()
197 {
198 return new static(Str::camel($this->value));
199 }
200
201 /**
202 * Determine if a given string contains a given substring.
203 *
204 * @param string|iterable<string> $needles
205 * @param bool $ignoreCase
206 * @return bool
207 */
208 public function contains($needles, $ignoreCase = false)
209 {
210 return Str::contains($this->value, $needles, $ignoreCase);
211 }
212
213 /**
214 * Determine if a given string contains all array values.
215 *
216 * @param iterable<string> $needles
217 * @param bool $ignoreCase
218 * @return bool
219 */
220 public function containsAll($needles, $ignoreCase = false)
221 {
222 return Str::containsAll($this->value, $needles, $ignoreCase);
223 }
224
225 /**
226 * Determine if a given string doesn't contain a given substring.
227 *
228 * @param string|iterable<string> $needles
229 * @param bool $ignoreCase
230 * @return bool
231 */
232 public function doesntContain($needles, $ignoreCase = false)
233 {
234 return Str::doesntContain($this->value, $needles, $ignoreCase);
235 }
236
237 /**
238 * Convert the case of a string.
239 *
240 * @param int $mode
241 * @param string|null $encoding
242 * @return static
243 */
244 public function convertCase(int $mode = MB_CASE_FOLD, ?string $encoding = 'UTF-8')
245 {
246 return new static(Str::convertCase($this->value, $mode, $encoding));
247 }
248
249 /**
250 * Replace consecutive instances of a given character with a single character.
251 *
252 * @param array<string>|string $characters
253 * @return static
254 */
255 public function deduplicate(array|string $characters = ' ')
256 {
257 return new static(Str::deduplicate($this->value, $characters));
258 }
259
260 /**
261 * Get the parent directory's path.
262 *
263 * @param int $levels
264 * @return static
265 */
266 public function dirname($levels = 1)
267 {
268 return new static(dirname($this->value, $levels));
269 }
270
271 /**
272 * Determine if a given string ends with a given substring.
273 *
274 * @param string|iterable<string> $needles
275 * @return bool
276 */
277 public function endsWith($needles)
278 {
279 return Str::endsWith($this->value, $needles);
280 }
281
282 /**
283 * Determine if a given string doesn't end with a given substring.
284 *
285 * @param string|iterable<string> $needles
286 * @return bool
287 */
288 public function doesntEndWith($needles)
289 {
290 return Str::doesntEndWith($this->value, $needles);
291 }
292
293 /**
294 * Determine if the string is an exact match with the given value.
295 *
296 * @param \Illuminate\Support\Stringable|string $value
297 * @return bool
298 */
299 public function exactly($value)
300 {
301 if ($value instanceof Stringable) {
302 $value = $value->toString();
303 }
304
305 return $this->value === $value;
306 }
307
308 /**
309 * Extracts an excerpt from text that matches the first instance of a phrase.
310 *
311 * @param string $phrase
312 * @param array $options
313 * @return string|null
314 */
315 public function excerpt($phrase = '', $options = [])
316 {
317 return Str::excerpt($this->value, $phrase, $options);
318 }
319
320 /**
321 * Explode the string into a collection.
322 *
323 * @param string $delimiter
324 * @param int $limit
325 * @return \Illuminate\Support\Collection<int, string>
326 */
327 public function explode($delimiter, $limit = PHP_INT_MAX)
328 {
329 return new Collection(explode($delimiter, $this->value, $limit));
330 }
331
332 /**
333 * Split a string using a regular expression or by length.
334 *
335 * @param string|int $pattern
336 * @param int $limit
337 * @param int $flags
338 * @return \Illuminate\Support\Collection<int, string>
339 */
340 public function split($pattern, $limit = -1, $flags = 0)
341 {
342 if (filter_var($pattern, FILTER_VALIDATE_INT) !== false) {
343 return new Collection(mb_str_split($this->value, $pattern));
344 }
345
346 $segments = preg_split($pattern, $this->value, $limit, $flags);
347
348 return ! empty($segments) ? new Collection($segments) : new Collection;
349 }
350
351 /**
352 * Cap a string with a single instance of a given value.
353 *
354 * @param string $cap
355 * @return static
356 */
357 public function finish($cap)
358 {
359 return new static(Str::finish($this->value, $cap));
360 }
361
362 /**
363 * Determine if a given string matches a given pattern.
364 *
365 * @param string|iterable<string> $pattern
366 * @param bool $ignoreCase
367 * @return bool
368 */
369 public function is($pattern, $ignoreCase = false)
370 {
371 return Str::is($pattern, $this->value, $ignoreCase);
372 }
373
374 /**
375 * Determine if a given string is 7 bit ASCII.
376 *
377 * @return bool
378 */
379 public function isAscii()
380 {
381 return Str::isAscii($this->value);
382 }
383
384 /**
385 * Determine if a given string is valid JSON.
386 *
387 * @return bool
388 */
389 public function isJson()
390 {
391 return Str::isJson($this->value);
392 }
393
394 /**
395 * Determine if a given value is a valid URL.
396 *
397 * @param array $protocols
398 * @return bool
399 */
400 public function isUrl(array $protocols = [])
401 {
402 return Str::isUrl($this->value, $protocols);
403 }
404
405 /**
406 * Determine if a given string is a valid UUID.
407 *
408 * @param int<0, 8>|'max'|null $version
409 * @return bool
410 */
411 public function isUuid($version = null)
412 {
413 return Str::isUuid($this->value, $version);
414 }
415
416 /**
417 * Determine if a given string is a valid ULID.
418 *
419 * @return bool
420 */
421 public function isUlid()
422 {
423 return Str::isUlid($this->value);
424 }
425
426 /**
427 * Determine if the given string is empty.
428 *
429 * @return bool
430 */
431 public function isEmpty()
432 {
433 return $this->value === '';
434 }
435
436 /**
437 * Determine if the given string is not empty.
438 *
439 * @return bool
440 */
441 public function isNotEmpty()
442 {
443 return ! $this->isEmpty();
444 }
445
446 /**
447 * Convert a string to kebab case.
448 *
449 * @return static
450 */
451 public function kebab()
452 {
453 return new static(Str::kebab($this->value));
454 }
455
456 /**
457 * Return the length of the given string.
458 *
459 * @param string|null $encoding
460 * @return int
461 */
462 public function length($encoding = null)
463 {
464 return Str::length($this->value, $encoding);
465 }
466
467 /**
468 * Limit the number of characters in a string.
469 *
470 * @param int $limit
471 * @param string $end
472 * @param bool $preserveWords
473 * @return static
474 */
475 public function limit($limit = 100, $end = '...', $preserveWords = false)
476 {
477 return new static(Str::limit($this->value, $limit, $end, $preserveWords));
478 }
479
480 /**
481 * Convert the given string to lower-case.
482 *
483 * @return static
484 */
485 public function lower()
486 {
487 return new static(Str::lower($this->value));
488 }
489
490 /**
491 * Convert GitHub flavored Markdown into HTML.
492 *
493 * @param array $options
494 * @param array $extensions
495 * @return static
496 */
497 public function markdown(array $options = [], array $extensions = [])
498 {
499 return new static(Str::markdown($this->value, $options, $extensions));
500 }
501
502 /**
503 * Convert inline Markdown into HTML.
504 *
505 * @param array $options
506 * @param array $extensions
507 * @return static
508 */
509 public function inlineMarkdown(array $options = [], array $extensions = [])
510 {
511 return new static(Str::inlineMarkdown($this->value, $options, $extensions));
512 }
513
514 /**
515 * Masks a portion of a string with a repeated character.
516 *
517 * @param string $character
518 * @param int $index
519 * @param int|null $length
520 * @param string $encoding
521 * @return static
522 */
523 public function mask($character, $index, $length = null, $encoding = 'UTF-8')
524 {
525 return new static(Str::mask($this->value, $character, $index, $length, $encoding));
526 }
527
528 /**
529 * Get the string matching the given pattern.
530 *
531 * @param string $pattern
532 * @return static
533 */
534 public function match($pattern)
535 {
536 return new static(Str::match($pattern, $this->value));
537 }
538
539 /**
540 * Determine if a given string matches a given pattern.
541 *
542 * @param string|iterable<string> $pattern
543 * @return bool
544 */
545 public function isMatch($pattern)
546 {
547 return Str::isMatch($pattern, $this->value);
548 }
549
550 /**
551 * Get the string matching the given pattern.
552 *
553 * @param string $pattern
554 * @return \Illuminate\Support\Collection
555 */
556 public function matchAll($pattern)
557 {
558 return Str::matchAll($pattern, $this->value);
559 }
560
561 /**
562 * Determine if the string matches the given pattern.
563 *
564 * @param string $pattern
565 * @return bool
566 */
567 public function test($pattern)
568 {
569 return $this->isMatch($pattern);
570 }
571
572 /**
573 * Remove all non-numeric characters from a string.
574 *
575 * @return static
576 */
577 public function numbers()
578 {
579 return new static(Str::numbers($this->value));
580 }
581
582 /**
583 * Pad both sides of the string with another.
584 *
585 * @param int $length
586 * @param string $pad
587 * @return static
588 */
589 public function padBoth($length, $pad = ' ')
590 {
591 return new static(Str::padBoth($this->value, $length, $pad));
592 }
593
594 /**
595 * Pad the left side of the string with another.
596 *
597 * @param int $length
598 * @param string $pad
599 * @return static
600 */
601 public function padLeft($length, $pad = ' ')
602 {
603 return new static(Str::padLeft($this->value, $length, $pad));
604 }
605
606 /**
607 * Pad the right side of the string with another.
608 *
609 * @param int $length
610 * @param string $pad
611 * @return static
612 */
613 public function padRight($length, $pad = ' ')
614 {
615 return new static(Str::padRight($this->value, $length, $pad));
616 }
617
618 /**
619 * Parse a Class@method style callback into class and method.
620 *
621 * @param string|null $default
622 * @return array<int, string|null>
623 */
624 public function parseCallback($default = null)
625 {
626 return Str::parseCallback($this->value, $default);
627 }
628
629 /**
630 * Call the given callback and return a new string.
631 *
632 * @param callable $callback
633 * @return static
634 */
635 public function pipe(callable $callback)
636 {
637 return new static($callback($this));
638 }
639
640 /**
641 * Get the plural form of an English word.
642 *
643 * @param int|array|\Countable $count
644 * @param bool $prependCount
645 * @return static
646 */
647 public function plural($count = 2, $prependCount = false)
648 {
649 return new static(Str::plural($this->value, $count, $prependCount));
650 }
651
652 /**
653 * Pluralize the last word of an English, studly caps case string.
654 *
655 * @param int|array|\Countable $count
656 * @return static
657 */
658 public function pluralStudly($count = 2)
659 {
660 return new static(Str::pluralStudly($this->value, $count));
661 }
662
663 /**
664 * Pluralize the last word of an English, Pascal caps case string.
665 *
666 * @param int|array|\Countable $count
667 * @return static
668 */
669 public function pluralPascal($count = 2)
670 {
671 return new static(Str::pluralStudly($this->value, $count));
672 }
673
674 /**
675 * Find the multi-byte safe position of the first occurrence of the given substring.
676 *
677 * @param string $needle
678 * @param int $offset
679 * @param string|null $encoding
680 * @return int|false
681 */
682 public function position($needle, $offset = 0, $encoding = null)
683 {
684 return Str::position($this->value, $needle, $offset, $encoding);
685 }
686
687 /**
688 * Prepend the given values to the string.
689 *
690 * @param string ...$values
691 * @return static
692 */
693 public function prepend(...$values)
694 {
695 return new static(implode('', $values).$this->value);
696 }
697
698 /**
699 * Remove any occurrence of the given string in the subject.
700 *
701 * @param string|iterable<string> $search
702 * @param bool $caseSensitive
703 * @return static
704 */
705 public function remove($search, $caseSensitive = true)
706 {
707 return new static(Str::remove($search, $this->value, $caseSensitive));
708 }
709
710 /**
711 * Reverse the string.
712 *
713 * @return static
714 */
715 public function reverse()
716 {
717 return new static(Str::reverse($this->value));
718 }
719
720 /**
721 * Repeat the string.
722 *
723 * @param int $times
724 * @return static
725 */
726 public function repeat(int $times)
727 {
728 return new static(str_repeat($this->value, $times));
729 }
730
731 /**
732 * Replace the given value in the given string.
733 *
734 * @param string|iterable<string> $search
735 * @param string|iterable<string> $replace
736 * @param bool $caseSensitive
737 * @return static
738 */
739 public function replace($search, $replace, $caseSensitive = true)
740 {
741 return new static(Str::replace($search, $replace, $this->value, $caseSensitive));
742 }
743
744 /**
745 * Replace a given value in the string sequentially with an array.
746 *
747 * @param string $search
748 * @param iterable<string> $replace
749 * @return static
750 */
751 public function replaceArray($search, $replace)
752 {
753 return new static(Str::replaceArray($search, $replace, $this->value));
754 }
755
756 /**
757 * Replace the first occurrence of a given value in the string.
758 *
759 * @param string $search
760 * @param string $replace
761 * @return static
762 */
763 public function replaceFirst($search, $replace)
764 {
765 return new static(Str::replaceFirst($search, $replace, $this->value));
766 }
767
768 /**
769 * Replace the first occurrence of the given value if it appears at the start of the string.
770 *
771 * @param string $search
772 * @param string $replace
773 * @return static
774 */
775 public function replaceStart($search, $replace)
776 {
777 return new static(Str::replaceStart($search, $replace, $this->value));
778 }
779
780 /**
781 * Replace the last occurrence of a given value in the string.
782 *
783 * @param string $search
784 * @param string $replace
785 * @return static
786 */
787 public function replaceLast($search, $replace)
788 {
789 return new static(Str::replaceLast($search, $replace, $this->value));
790 }
791
792 /**
793 * Replace the last occurrence of a given value if it appears at the end of the string.
794 *
795 * @param string $search
796 * @param string $replace
797 * @return static
798 */
799 public function replaceEnd($search, $replace)
800 {
801 return new static(Str::replaceEnd($search, $replace, $this->value));
802 }
803
804 /**
805 * Replace the patterns matching the given regular expression.
806 *
807 * @param array|string $pattern
808 * @param \Closure|string[]|string $replace
809 * @param int $limit
810 * @return static
811 */
812 public function replaceMatches($pattern, $replace, $limit = -1)
813 {
814 if ($replace instanceof Closure) {
815 return new static(preg_replace_callback($pattern, $replace, $this->value, $limit));
816 }
817
818 return new static(preg_replace($pattern, $replace, $this->value, $limit));
819 }
820
821 /**
822 * Parse input from a string to a collection, according to a format.
823 *
824 * @param string $format
825 * @return \Illuminate\Support\Collection
826 */
827 public function scan($format)
828 {
829 return new Collection(sscanf($this->value, $format));
830 }
831
832 /**
833 * Remove all "extra" blank space from the given string.
834 *
835 * @return static
836 */
837 public function squish()
838 {
839 return new static(Str::squish($this->value));
840 }
841
842 /**
843 * Begin a string with a single instance of a given value.
844 *
845 * @param string $prefix
846 * @return static
847 */
848 public function start($prefix)
849 {
850 return new static(Str::start($this->value, $prefix));
851 }
852
853 /**
854 * Strip HTML and PHP tags from the given string.
855 *
856 * @param string[]|string|null $allowedTags
857 * @return static
858 */
859 public function stripTags($allowedTags = null)
860 {
861 return new static(strip_tags($this->value, $allowedTags));
862 }
863
864 /**
865 * Convert the given string to upper-case.
866 *
867 * @return static
868 */
869 public function upper()
870 {
871 return new static(Str::upper($this->value));
872 }
873
874 /**
875 * Convert the given string to proper case.
876 *
877 * @return static
878 */
879 public function title()
880 {
881 return new static(Str::title($this->value));
882 }
883
884 /**
885 * Convert the given string to proper case for each word.
886 *
887 * @return static
888 */
889 public function headline()
890 {
891 return new static(Str::headline($this->value));
892 }
893
894 /**
895 * Convert the given string to only its initials.
896 *
897 * @return static
898 */
899 public function initials()
900 {
901 return new static(Str::initials($this->value));
902 }
903
904 /**
905 * Convert the given string to APA-style title case.
906 *
907 * @return static
908 */
909 public function apa()
910 {
911 return new static(Str::apa($this->value));
912 }
913
914 /**
915 * Transliterate a string to its closest ASCII representation.
916 *
917 * @param string|null $unknown
918 * @param bool|null $strict
919 * @return static
920 */
921 public function transliterate($unknown = '?', $strict = false)
922 {
923 return new static(Str::transliterate($this->value, $unknown, $strict));
924 }
925
926 /**
927 * Get the singular form of an English word.
928 *
929 * @return static
930 */
931 public function singular()
932 {
933 return new static(Str::singular($this->value));
934 }
935
936 /**
937 * Generate a URL friendly "slug" from a given string.
938 *
939 * @param string $separator
940 * @param string|null $language
941 * @param array<string, string> $dictionary
942 * @return static
943 */
944 public function slug($separator = '-', $language = 'en', $dictionary = ['@' => 'at'])
945 {
946 return new static(Str::slug($this->value, $separator, $language, $dictionary));
947 }
948
949 /**
950 * Convert a string to snake case.
951 *
952 * @param string $delimiter
953 * @return static
954 */
955 public function snake($delimiter = '_')
956 {
957 return new static(Str::snake($this->value, $delimiter));
958 }
959
960 /**
961 * Determine if a given string starts with a given substring.
962 *
963 * @param string|iterable<string> $needles
964 * @return bool
965 */
966 public function startsWith($needles)
967 {
968 return Str::startsWith($this->value, $needles);
969 }
970
971 /**
972 * Determine if a given string doesn't start with a given substring.
973 *
974 * @param string|iterable<string> $needles
975 * @return bool
976 */
977 public function doesntStartWith($needles)
978 {
979 return Str::doesntStartWith($this->value, $needles);
980 }
981
982 /**
983 * Convert a value to studly caps case.
984 *
985 * @return static
986 */
987 public function studly()
988 {
989 return new static(Str::studly($this->value));
990 }
991
992 /**
993 * Convert the string to Pascal case.
994 *
995 * @return static
996 */
997 public function pascal()
998 {
999 return new static(Str::pascal($this->value));
1000 }
1001
1002 /**
1003 * Returns the portion of the string specified by the start and length parameters.
1004 *
1005 * @param int $start
1006 * @param int|null $length
1007 * @param string $encoding
1008 * @return static
1009 */
1010 public function substr($start, $length = null, $encoding = 'UTF-8')
1011 {
1012 return new static(Str::substr($this->value, $start, $length, $encoding));
1013 }
1014
1015 /**
1016 * Returns the number of substring occurrences.
1017 *
1018 * @param string $needle
1019 * @param int $offset
1020 * @param int|null $length
1021 * @return int
1022 */
1023 public function substrCount($needle, $offset = 0, $length = null)
1024 {
1025 return Str::substrCount($this->value, $needle, $offset, $length);
1026 }
1027
1028 /**
1029 * Replace text within a portion of a string.
1030 *
1031 * @param string|string[] $replace
1032 * @param int|int[] $offset
1033 * @param int|int[]|null $length
1034 * @return static
1035 */
1036 public function substrReplace($replace, $offset = 0, $length = null)
1037 {
1038 return new static(Str::substrReplace($this->value, $replace, $offset, $length));
1039 }
1040
1041 /**
1042 * Swap multiple keywords in a string with other keywords.
1043 *
1044 * @param array $map
1045 * @return static
1046 */
1047 public function swap(array $map)
1048 {
1049 return new static(strtr($this->value, $map));
1050 }
1051
1052 /**
1053 * Take the first or last {$limit} characters.
1054 *
1055 * @param int $limit
1056 * @return static
1057 */
1058 public function take(int $limit)
1059 {
1060 if ($limit < 0) {
1061 return $this->substr($limit);
1062 }
1063
1064 return $this->substr(0, $limit);
1065 }
1066
1067 /**
1068 * Trim the string of the given characters.
1069 *
1070 * @param string|null $characters
1071 * @return static
1072 */
1073 public function trim($characters = null)
1074 {
1075 return new static(Str::trim(...array_merge([$this->value], func_get_args())));
1076 }
1077
1078 /**
1079 * Left trim the string of the given characters.
1080 *
1081 * @param string|null $characters
1082 * @return static
1083 */
1084 public function ltrim($characters = null)
1085 {
1086 return new static(Str::ltrim(...array_merge([$this->value], func_get_args())));
1087 }
1088
1089 /**
1090 * Right trim the string of the given characters.
1091 *
1092 * @param string|null $characters
1093 * @return static
1094 */
1095 public function rtrim($characters = null)
1096 {
1097 return new static(Str::rtrim(...array_merge([$this->value], func_get_args())));
1098 }
1099
1100 /**
1101 * Make a string's first character lowercase.
1102 *
1103 * @return static
1104 */
1105 public function lcfirst()
1106 {
1107 return new static(Str::lcfirst($this->value));
1108 }
1109
1110 /**
1111 * Make a string's first character uppercase.
1112 *
1113 * @return static
1114 */
1115 public function ucfirst()
1116 {
1117 return new static(Str::ucfirst($this->value));
1118 }
1119
1120 /**
1121 * Capitalize the first character of each word in a string.
1122 *
1123 * @param string $separators
1124 * @return static
1125 */
1126 public function ucwords($separators = " \t\r\n\f\v")
1127 {
1128 return new static(Str::ucwords($this->value, $separators));
1129 }
1130
1131 /**
1132 * Split a string by uppercase characters.
1133 *
1134 * @return \Illuminate\Support\Collection<int, string>
1135 */
1136 public function ucsplit()
1137 {
1138 return new Collection(Str::ucsplit($this->value));
1139 }
1140
1141 /**
1142 * Execute the given callback if the string contains a given substring.
1143 *
1144 * @param string|iterable<string> $needles
1145 * @param callable $callback
1146 * @param callable|null $default
1147 * @return static
1148 */
1149 public function whenContains($needles, $callback, $default = null)
1150 {
1151 return $this->when($this->contains($needles), $callback, $default);
1152 }
1153
1154 /**
1155 * Execute the given callback if the string contains all array values.
1156 *
1157 * @param iterable<string> $needles
1158 * @param callable $callback
1159 * @param callable|null $default
1160 * @return static
1161 */
1162 public function whenContainsAll(array $needles, $callback, $default = null)
1163 {
1164 return $this->when($this->containsAll($needles), $callback, $default);
1165 }
1166
1167 /**
1168 * Execute the given callback if the string is empty.
1169 *
1170 * @param callable $callback
1171 * @param callable|null $default
1172 * @return static
1173 */
1174 public function whenEmpty($callback, $default = null)
1175 {
1176 return $this->when($this->isEmpty(), $callback, $default);
1177 }
1178
1179 /**
1180 * Execute the given callback if the string is not empty.
1181 *
1182 * @param callable $callback
1183 * @param callable|null $default
1184 * @return static
1185 */
1186 public function whenNotEmpty($callback, $default = null)
1187 {
1188 return $this->when($this->isNotEmpty(), $callback, $default);
1189 }
1190
1191 /**
1192 * Execute the given callback if the string ends with a given substring.
1193 *
1194 * @param string|iterable<string> $needles
1195 * @param callable $callback
1196 * @param callable|null $default
1197 * @return static
1198 */
1199 public function whenEndsWith($needles, $callback, $default = null)
1200 {
1201 return $this->when($this->endsWith($needles), $callback, $default);
1202 }
1203
1204 /**
1205 * Execute the given callback if the string doesn't end with a given substring.
1206 *
1207 * @param string|iterable<string> $needles
1208 * @param callable $callback
1209 * @param callable|null $default
1210 * @return static
1211 */
1212 public function whenDoesntEndWith($needles, $callback, $default = null)
1213 {
1214 return $this->when($this->doesntEndWith($needles), $callback, $default);
1215 }
1216
1217 /**
1218 * Execute the given callback if the string is an exact match with the given value.
1219 *
1220 * @param string $value
1221 * @param callable $callback
1222 * @param callable|null $default
1223 * @return static
1224 */
1225 public function whenExactly($value, $callback, $default = null)
1226 {
1227 return $this->when($this->exactly($value), $callback, $default);
1228 }
1229
1230 /**
1231 * Execute the given callback if the string is not an exact match with the given value.
1232 *
1233 * @param string $value
1234 * @param callable $callback
1235 * @param callable|null $default
1236 * @return static
1237 */
1238 public function whenNotExactly($value, $callback, $default = null)
1239 {
1240 return $this->when(! $this->exactly($value), $callback, $default);
1241 }
1242
1243 /**
1244 * Execute the given callback if the string matches a given pattern.
1245 *
1246 * @param string|iterable<string> $pattern
1247 * @param callable $callback
1248 * @param callable|null $default
1249 * @return static
1250 */
1251 public function whenIs($pattern, $callback, $default = null)
1252 {
1253 return $this->when($this->is($pattern), $callback, $default);
1254 }
1255
1256 /**
1257 * Execute the given callback if the string is 7 bit ASCII.
1258 *
1259 * @param callable $callback
1260 * @param callable|null $default
1261 * @return static
1262 */
1263 public function whenIsAscii($callback, $default = null)
1264 {
1265 return $this->when($this->isAscii(), $callback, $default);
1266 }
1267
1268 /**
1269 * Execute the given callback if the string is a valid UUID.
1270 *
1271 * @param callable $callback
1272 * @param callable|null $default
1273 * @return static
1274 */
1275 public function whenIsUuid($callback, $default = null)
1276 {
1277 return $this->when($this->isUuid(), $callback, $default);
1278 }
1279
1280 /**
1281 * Execute the given callback if the string is a valid ULID.
1282 *
1283 * @param callable $callback
1284 * @param callable|null $default
1285 * @return static
1286 */
1287 public function whenIsUlid($callback, $default = null)
1288 {
1289 return $this->when($this->isUlid(), $callback, $default);
1290 }
1291
1292 /**
1293 * Execute the given callback if the string starts with a given substring.
1294 *
1295 * @param string|iterable<string> $needles
1296 * @param callable $callback
1297 * @param callable|null $default
1298 * @return static
1299 */
1300 public function whenStartsWith($needles, $callback, $default = null)
1301 {
1302 return $this->when($this->startsWith($needles), $callback, $default);
1303 }
1304
1305 /**
1306 * Execute the given callback if the string doesn't start with a given substring.
1307 *
1308 * @param string|iterable<string> $needles
1309 * @param callable $callback
1310 * @param callable|null $default
1311 * @return static
1312 */
1313 public function whenDoesntStartWith($needles, $callback, $default = null)
1314 {
1315 return $this->when($this->doesntStartWith($needles), $callback, $default);
1316 }
1317
1318 /**
1319 * Execute the given callback if the string matches the given pattern.
1320 *
1321 * @param string $pattern
1322 * @param callable $callback
1323 * @param callable|null $default
1324 * @return static
1325 */
1326 public function whenTest($pattern, $callback, $default = null)
1327 {
1328 return $this->when($this->test($pattern), $callback, $default);
1329 }
1330
1331 /**
1332 * Limit the number of words in a string.
1333 *
1334 * @param int $words
1335 * @param string $end
1336 * @return static
1337 */
1338 public function words($words = 100, $end = '...')
1339 {
1340 return new static(Str::words($this->value, $words, $end));
1341 }
1342
1343 /**
1344 * Get the number of words a string contains.
1345 *
1346 * @param string|null $characters
1347 * @return int
1348 */
1349 public function wordCount($characters = null)
1350 {
1351 return Str::wordCount($this->value, $characters);
1352 }
1353
1354 /**
1355 * Wrap a string to a given number of characters.
1356 *
1357 * @param int $characters
1358 * @param string $break
1359 * @param bool $cutLongWords
1360 * @return static
1361 */
1362 public function wordWrap($characters = 75, $break = "\n", $cutLongWords = false)
1363 {
1364 return new static(Str::wordWrap($this->value, $characters, $break, $cutLongWords));
1365 }
1366
1367 /**
1368 * Wrap the string with the given strings.
1369 *
1370 * @param string $before
1371 * @param string|null $after
1372 * @return static
1373 */
1374 public function wrap($before, $after = null)
1375 {
1376 return new static(Str::wrap($this->value, $before, $after));
1377 }
1378
1379 /**
1380 * Unwrap the string with the given strings.
1381 *
1382 * @param string $before
1383 * @param string|null $after
1384 * @return static
1385 */
1386 public function unwrap($before, $after = null)
1387 {
1388 return new static(Str::unwrap($this->value, $before, $after));
1389 }
1390
1391 /**
1392 * Convert the string into a `HtmlString` instance.
1393 *
1394 * @return \Illuminate\Support\HtmlString
1395 */
1396 public function toHtmlString()
1397 {
1398 return new HtmlString($this->value);
1399 }
1400
1401 /**
1402 * Convert the string to Base64 encoding.
1403 *
1404 * @return static
1405 */
1406 public function toBase64()
1407 {
1408 return new static(base64_encode($this->value));
1409 }
1410
1411 /**
1412 * Decode the Base64 encoded string.
1413 *
1414 * @param bool $strict
1415 * @return static
1416 */
1417 public function fromBase64($strict = false)
1418 {
1419 return new static(base64_decode($this->value, $strict));
1420 }
1421
1422 /**
1423 * Hash the string using the given algorithm.
1424 *
1425 * @param string $algorithm
1426 * @return static
1427 */
1428 public function hash(string $algorithm)
1429 {
1430 return new static(hash($algorithm, $this->value));
1431 }
1432
1433 /**
1434 * Encrypt the string.
1435 *
1436 * @param bool $serialize
1437 * @return static
1438 */
1439 public function encrypt(bool $serialize = false)
1440 {
1441 return new static(encrypt($this->value, $serialize));
1442 }
1443
1444 /**
1445 * Decrypt the string.
1446 *
1447 * @param bool $serialize
1448 * @return static
1449 */
1450 public function decrypt(bool $serialize = false)
1451 {
1452 return new static(decrypt($this->value, $serialize));
1453 }
1454
1455 /**
1456 * Dump the string.
1457 *
1458 * @param mixed ...$args
1459 * @return $this
1460 */
1461 public function dump(...$args)
1462 {
1463 dump($this->value, ...$args);
1464
1465 return $this;
1466 }
1467
1468 /**
1469 * Get the underlying string value.
1470 *
1471 * @return string
1472 */
1473 public function value()
1474 {
1475 return $this->toString();
1476 }
1477
1478 /**
1479 * Get the underlying string value.
1480 *
1481 * @return string
1482 */
1483 public function toString()
1484 {
1485 return $this->value;
1486 }
1487
1488 /**
1489 * Get the underlying string value as an integer.
1490 *
1491 * @param int $base
1492 * @return int
1493 */
1494 public function toInteger($base = 10)
1495 {
1496 return intval($this->value, $base);
1497 }
1498
1499 /**
1500 * Get the underlying string value as a float.
1501 *
1502 * @return float
1503 */
1504 public function toFloat()
1505 {
1506 return (float) $this->value;
1507 }
1508
1509 /**
1510 * Get the underlying string value as a boolean.
1511 *
1512 * Returns true when value is "1", "true", "on", and "yes". Otherwise, returns false.
1513 *
1514 * @return bool
1515 */
1516 public function toBoolean()
1517 {
1518 return filter_var($this->value, FILTER_VALIDATE_BOOLEAN);
1519 }
1520
1521 /**
1522 * Get the underlying string value as a Carbon instance.
1523 *
1524 * @param string|null $format
1525 * @param string|null $tz
1526 * @return \Illuminate\Support\Carbon
1527 *
1528 * @throws \Carbon\Exceptions\InvalidFormatException
1529 */
1530 public function toDate($format = null, $tz = null)
1531 {
1532 if (is_null($format)) {
1533 return Date::parse($this->value, $tz);
1534 }
1535
1536 return Date::createFromFormat($format, $this->value, $tz);
1537 }
1538
1539 /**
1540 * Get the underlying string value as a Uri instance.
1541 *
1542 * @return \Illuminate\Support\Uri
1543 */
1544 public function toUri()
1545 {
1546 return Uri::of($this->value);
1547 }
1548
1549 /**
1550 * Convert the object to a string when JSON encoded.
1551 *
1552 * @return string
1553 */
1554 public function jsonSerialize(): string
1555 {
1556 return $this->__toString();
1557 }
1558
1559 /**
1560 * Determine if the given offset exists.
1561 *
1562 * @param mixed $offset
1563 * @return bool
1564 */
1565 public function offsetExists(mixed $offset): bool
1566 {
1567 return isset($this->value[$offset]);
1568 }
1569
1570 /**
1571 * Get the value at the given offset.
1572 *
1573 * @param mixed $offset
1574 * @return string
1575 */
1576 public function offsetGet(mixed $offset): string
1577 {
1578 return $this->value[$offset];
1579 }
1580
1581 /**
1582 * Set the value at the given offset.
1583 *
1584 * @param mixed $offset
1585 * @return void
1586 */
1587 public function offsetSet(mixed $offset, mixed $value): void
1588 {
1589 $this->value[$offset] = $value;
1590 }
1591
1592 /**
1593 * Unset the value at the given offset.
1594 *
1595 * @param mixed $offset
1596 * @return void
1597 */
1598 public function offsetUnset(mixed $offset): void
1599 {
1600 unset($this->value[$offset]);
1601 }
1602
1603 /**
1604 * Proxy dynamic properties onto methods.
1605 *
1606 * @param string $key
1607 * @return mixed
1608 */
1609 public function __get($key)
1610 {
1611 return $this->{$key}();
1612 }
1613
1614 /**
1615 * Get the raw string value.
1616 *
1617 * @return string
1618 */
1619 public function __toString()
1620 {
1621 return (string) $this->value;
1622 }
1623 }