Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Zur aktuellen Revision | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

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