Completed
Push — master ( 9301fb...70ac8c )
by Lars
02:48
created

Stringy::replaceLast()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stringy;
6
7
use voku\helper\AntiXSS;
8
use voku\helper\ASCII;
9
use voku\helper\EmailCheck;
10
use voku\helper\URLify;
11
use voku\helper\UTF8;
12
13
class Stringy implements \ArrayAccess, \Countable, \IteratorAggregate, \JsonSerializable
14
{
15
    /**
16
     * An instance's string.
17
     *
18
     * @var string
19
     */
20
    protected $str;
21
22
    /**
23
     * The string's encoding, which should be one of the mbstring module's
24
     * supported encodings.
25
     *
26
     * @var string
27
     */
28
    protected $encoding;
29
30
    /**
31
     * @var UTF8
32
     */
33
    private $utf8;
34
35
    /**
36
     * @var ASCII
37
     */
38
    private $ascii;
39
40
    /**
41
     * Initializes a Stringy object and assigns both str and encoding properties
42
     * the supplied values. $str is cast to a string prior to assignment, and if
43
     * $encoding is not specified, it defaults to mb_internal_encoding(). Throws
44
     * an InvalidArgumentException if the first argument is an array or object
45
     * without a __toString method.
46
     *
47
     * @param mixed  $str      [optional] <p>Value to modify, after being cast to string. Default: ''</p>
48
     * @param string $encoding [optional] <p>The character encoding. Fallback: 'UTF-8'</p>
49
     *
50
     * @throws \InvalidArgumentException
51
     *                                   <p>if an array or object without a
52
     *                                   __toString method is passed as the first argument</p>
53
     */
54 2206
    public function __construct($str = '', string $encoding = null)
55
    {
56 2206
        if (\is_array($str)) {
57 2
            throw new \InvalidArgumentException(
58 2
                'Passed value cannot be an array'
59
            );
60
        }
61
62
        if (
63 2204
            \is_object($str)
64
            &&
65 2204
            !\method_exists($str, '__toString')
66
        ) {
67 2
            throw new \InvalidArgumentException(
68 2
                'Passed object must have a __toString method'
69
            );
70
        }
71
72 2202
        $this->str = (string) $str;
73
74 2202
        static $ASCII = null;
75 2202
        if ($ASCII === null) {
76
            $ASCII = new ASCII();
77
        }
78 2202
        $this->ascii = $ASCII;
79
80 2202
        static $UTF8 = null;
81 2202
        if ($UTF8 === null) {
82
            $UTF8 = new UTF8();
83
        }
84 2202
        $this->utf8 = $UTF8;
85
86 2202
        if ($encoding !== 'UTF-8') {
87 1441
            $this->encoding = $this->utf8::normalize_encoding($encoding, 'UTF-8');
88
        } else {
89 1662
            $this->encoding = $encoding;
90
        }
91 2202
    }
92
93
    /**
94
     * Returns the value in $str.
95
     *
96
     * @return string
97
     *                <p>The current value of the $str property.</p>
98
     */
99 982
    public function __toString()
100
    {
101 982
        return (string) $this->str;
102
    }
103
104
    /**
105
     * Returns value which can be serialized by json_encode().
106
     *
107
     * @noinspection ReturnTypeCanBeDeclaredInspection
108
     *
109
     * @return string The current value of the $str property
110
     */
111 1
    public function jsonSerialize()
112
    {
113 1
        return (string) $this;
114
    }
115
116
    /**
117
     * Gets the substring after the first occurrence of a separator.
118
     * If no match is found returns new empty Stringy object.
119
     *
120
     * @param string $separator
121
     *
122
     * @return static
123
     */
124 2
    public function afterFirst(string $separator): self
125
    {
126 2
        return static::create(
127 2
            $this->utf8::str_substr_after_first_separator(
128 2
                $this->str,
129 2
                $separator,
130 2
                $this->encoding
131
            )
132
        );
133
    }
134
135
    /**
136
     * Gets the substring after the first occurrence of a separator.
137
     * If no match is found returns new empty Stringy object.
138
     *
139
     * @param string $separator
140
     *
141
     * @return static
142
     */
143 1
    public function afterFirstIgnoreCase(string $separator): self
144
    {
145 1
        return static::create(
146 1
            $this->utf8::str_isubstr_after_first_separator(
147 1
                $this->str,
148 1
                $separator,
149 1
                $this->encoding
150
            )
151
        );
152
    }
153
154
    /**
155
     * Gets the substring after the last occurrence of a separator.
156
     * If no match is found returns new empty Stringy object.
157
     *
158
     * @param string $separator
159
     *
160
     * @return static
161
     */
162 1
    public function afterLast(string $separator): self
163
    {
164 1
        return static::create(
165 1
            $this->utf8::str_substr_after_last_separator(
166 1
                $this->str,
167 1
                $separator,
168 1
                $this->encoding
169
            )
170
        );
171
    }
172
173
    /**
174
     * Gets the substring after the last occurrence of a separator.
175
     * If no match is found returns new empty Stringy object.
176
     *
177
     * @param string $separator
178
     *
179
     * @return static
180
     */
181 1
    public function afterLastIgnoreCase(string $separator): self
182
    {
183 1
        return static::create(
184 1
            $this->utf8::str_isubstr_after_last_separator(
185 1
                $this->str,
186 1
                $separator,
187 1
                $this->encoding
188
            )
189
        );
190
    }
191
192
    /**
193
     * Returns a new string with $string appended.
194
     *
195
     * @param string $string <p>The string to append.</p>
196
     *
197
     * @return static
198
     *                <p>Object with appended $string.</p>
199
     */
200 8
    public function append(string $string): self
201
    {
202 8
        return static::create($this->str . $string, $this->encoding);
203
    }
204
205
    /**
206
     * Append an password (limited to chars that are good readable).
207
     *
208
     * @param int $length <p>Length of the random string.</p>
209
     *
210
     * @return static
211
     *                <p>Object with appended password.</p>
212
     */
213 1
    public function appendPassword(int $length): self
214
    {
215 1
        return $this->appendRandomString(
216 1
            $length,
217 1
            '2346789bcdfghjkmnpqrtvwxyzBCDFGHJKLMNPQRTVWXYZ!?_#'
218
        );
219
    }
220
221
    /**
222
     * Append an random string.
223
     *
224
     * @param int    $length        <p>Length of the random string.</p>
225
     * @param string $possibleChars [optional] <p>Characters string for the random selection.</p>
226
     *
227
     * @return static
228
     *                <p>Object with appended random string.</p>
229
     */
230 2
    public function appendRandomString(int $length, string $possibleChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'): self
231
    {
232 2
        $str = $this->utf8::get_random_string($length, $possibleChars);
233
234 2
        return $this->append($str);
235
    }
236
237
    /**
238
     * Append an unique identifier.
239
     *
240
     * @param int|string $entropyExtra [optional] <p>Extra entropy via a string or int value.</p>
241
     * @param bool       $md5          [optional] <p>Return the unique identifier as md5-hash? Default: true</p>
242
     *
243
     * @return static
244
     *                <p>Object with appended unique identifier as md5-hash.</p>
245
     */
246 1
    public function appendUniqueIdentifier($entropyExtra = '', bool $md5 = true): self
247
    {
248 1
        return $this->append(
249 1
            $this->utf8::get_unique_string($entropyExtra, $md5)
250
        );
251
    }
252
253
    /**
254
     * Returns the character at $index, with indexes starting at 0.
255
     *
256
     * @param int $index <p>Position of the character.</p>
257
     *
258
     * @return static
259
     *                <p>The character at $index.</p>
260
     */
261 16
    public function at(int $index): self
262
    {
263 16
        return static::create($this->utf8::char_at($this->str, $index), $this->encoding);
264
    }
265
266
    /**
267
     * Gets the substring before the first occurrence of a separator.
268
     * If no match is found returns new empty Stringy object.
269
     *
270
     * @param string $separator
271
     *
272
     * @return static
273
     */
274 1
    public function beforeFirst(string $separator): self
275
    {
276 1
        return static::create(
277 1
            $this->utf8::str_substr_before_first_separator(
278 1
                $this->str,
279 1
                $separator,
280 1
                $this->encoding
281
            )
282
        );
283
    }
284
285
    /**
286
     * Gets the substring before the first occurrence of a separator.
287
     * If no match is found returns new empty Stringy object.
288
     *
289
     * @param string $separator
290
     *
291
     * @return static
292
     */
293 1
    public function beforeFirstIgnoreCase(string $separator): self
294
    {
295 1
        return static::create(
296 1
            $this->utf8::str_isubstr_before_first_separator(
297 1
                $this->str,
298 1
                $separator,
299 1
                $this->encoding
300
            )
301
        );
302
    }
303
304
    /**
305
     * Gets the substring before the last occurrence of a separator.
306
     * If no match is found returns new empty Stringy object.
307
     *
308
     * @param string $separator
309
     *
310
     * @return static
311
     */
312 1
    public function beforeLast(string $separator): self
313
    {
314 1
        return static::create(
315 1
            $this->utf8::str_substr_before_last_separator(
316 1
                $this->str,
317 1
                $separator,
318 1
                $this->encoding
319
            )
320
        );
321
    }
322
323
    /**
324
     * Gets the substring before the last occurrence of a separator.
325
     * If no match is found returns new empty Stringy object.
326
     *
327
     * @param string $separator
328
     *
329
     * @return static
330
     */
331 1
    public function beforeLastIgnoreCase(string $separator): self
332
    {
333 1
        return static::create(
334 1
            $this->utf8::str_isubstr_before_last_separator(
335 1
                $this->str,
336 1
                $separator,
337 1
                $this->encoding
338
            )
339
        );
340
    }
341
342
    /**
343
     * Returns the substring between $start and $end, if found, or an empty
344
     * string. An optional offset may be supplied from which to begin the
345
     * search for the start string.
346
     *
347
     * @param string $start  <p>Delimiter marking the start of the substring.</p>
348
     * @param string $end    <p>Delimiter marking the end of the substring.</p>
349
     * @param int    $offset [optional] <p>Index from which to begin the search. Default: 0</p>
350
     *
351
     * @return static
352
     *                <p>Object whose $str is a substring between $start and $end.</p>
353
     */
354 32
    public function between(string $start, string $end, int $offset = null): self
355
    {
356
        /** @noinspection UnnecessaryCastingInspection */
357 32
        $str = $this->utf8::between(
358 32
            $this->str,
359 32
            $start,
360 32
            $end,
361 32
            (int) $offset,
362 32
            $this->encoding
363
        );
364
365 32
        return static::create($str, $this->encoding);
366
    }
367
368
    /**
369
     * Returns a camelCase version of the string. Trims surrounding spaces,
370
     * capitalizes letters following digits, spaces, dashes and underscores,
371
     * and removes spaces, dashes, as well as underscores.
372
     *
373
     * @return static
374
     *                <p>Object with $str in camelCase.</p>
375
     */
376 38
    public function camelize(): self
377
    {
378 38
        return static::create(
379 38
            $this->utf8::str_camelize($this->str, $this->encoding),
380 38
            $this->encoding
381
        );
382
    }
383
384
    /**
385
     * Returns the string with the first letter of each word capitalized,
386
     * except for when the word is a name which shouldn't be capitalized.
387
     *
388
     * @return static
389
     *                <p>Object with $str capitalized.</p>
390
     */
391 39
    public function capitalizePersonalName(): self
392
    {
393 39
        return static::create(
394 39
            $this->utf8::str_capitalize_name($this->str),
395 39
            $this->encoding
396
        );
397
    }
398
399
    /**
400
     * Returns an array consisting of the characters in the string.
401
     *
402
     * @return array
403
     *               <p>An array of string chars.</p>
404
     */
405 8
    public function chars(): array
406
    {
407 8
        return $this->utf8::str_split($this->str);
408
    }
409
410
    /**
411
     * Trims the string and replaces consecutive whitespace characters with a
412
     * single space. This includes tabs and newline characters, as well as
413
     * multibyte whitespace such as the thin space and ideographic space.
414
     *
415
     * @return static
416
     *                <p>Object with a trimmed $str and condensed whitespace.</p>
417
     */
418 26
    public function collapseWhitespace(): self
419
    {
420 26
        return static::create(
421 26
            $this->utf8::collapse_whitespace($this->str),
422 26
            $this->encoding
423
        );
424
    }
425
426
    /**
427
     * Returns true if the string contains $needle, false otherwise. By default
428
     * the comparison is case-sensitive, but can be made insensitive by setting
429
     * $caseSensitive to false.
430
     *
431
     * @param string $needle        <p>Substring to look for.</p>
432
     * @param bool   $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p>
433
     *
434
     * @return bool
435
     *              <p>Whether or not $str contains $needle.</p>
436
     */
437 42
    public function contains(string $needle, bool $caseSensitive = true): bool
438
    {
439 42
        return $this->utf8::str_contains(
440 42
            $this->str,
441 42
            $needle,
442 42
            $caseSensitive
443
        );
444
    }
445
446
    /**
447
     * Returns true if the string contains all $needles, false otherwise. By
448
     * default the comparison is case-sensitive, but can be made insensitive by
449
     * setting $caseSensitive to false.
450
     *
451
     * @param array $needles       <p>SubStrings to look for.</p>
452
     * @param bool  $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p>
453
     *
454
     * @return bool
455
     *              <p>Whether or not $str contains $needle.</p>
456
     */
457 87
    public function containsAll(array $needles, bool $caseSensitive = true): bool
458
    {
459 87
        return $this->utf8::str_contains_all(
460 87
            $this->str,
461 87
            $needles,
462 87
            $caseSensitive
463
        );
464
    }
465
466
    /**
467
     * Returns true if the string contains any $needles, false otherwise. By
468
     * default the comparison is case-sensitive, but can be made insensitive by
469
     * setting $caseSensitive to false.
470
     *
471
     * @param array $needles       <p>SubStrings to look for.</p>
472
     * @param bool  $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p>
473
     *
474
     * @return bool
475
     *              <p>Whether or not $str contains $needle.</p>
476
     */
477 86
    public function containsAny(array $needles, bool $caseSensitive = true): bool
478
    {
479 86
        return $this->utf8::str_contains_any(
480 86
            $this->str,
481 86
            $needles,
482 86
            $caseSensitive
483
        );
484
    }
485
486
    /**
487
     * Returns the length of the string, implementing the countable interface.
488
     *
489
     * @return int
490
     *             <p>The number of characters in the string, given the encoding.</p>
491
     */
492 2
    public function count(): int
493
    {
494 2
        return $this->length();
495
    }
496
497
    /**
498
     * Returns the number of occurrences of $substring in the given string.
499
     * By default, the comparison is case-sensitive, but can be made insensitive
500
     * by setting $caseSensitive to false.
501
     *
502
     * @param string $substring     <p>The substring to search for.</p>
503
     * @param bool   $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p>
504
     *
505
     * @return int
506
     */
507 30
    public function countSubstr(string $substring, bool $caseSensitive = true): int
508
    {
509 30
        return $this->utf8::substr_count_simple(
510 30
            $this->str,
511 30
            $substring,
512 30
            $caseSensitive,
513 30
            $this->encoding
514
        );
515
    }
516
517
    /**
518
     * Creates a Stringy object and assigns both str and encoding properties
519
     * the supplied values. $str is cast to a string prior to assignment, and if
520
     * $encoding is not specified, it defaults to mb_internal_encoding(). It
521
     * then returns the initialized object. Throws an InvalidArgumentException
522
     * if the first argument is an array or object without a __toString method.
523
     *
524
     * @param mixed  $str      [optional] <p>Value to modify, after being cast to string. Default: ''</p>
525
     * @param string $encoding [optional] <p>The character encoding. Fallback: 'UTF-8'</p>
526
     *
527
     * @throws \InvalidArgumentException
528
     *                                   <p>if an array or object without a
529
     *                                   __toString method is passed as the first argument</p>
530
     *
531
     * @return static
532
     *                <p>A Stringy object.</p>
533
     */
534 2185
    public static function create($str = '', string $encoding = null): self
535
    {
536 2185
        return new static($str, $encoding);
537
    }
538
539
    /**
540
     * Returns a lowercase and trimmed string separated by dashes. Dashes are
541
     * inserted before uppercase characters (with the exception of the first
542
     * character of the string), and in place of spaces as well as underscores.
543
     *
544
     * @return static
545
     *                <p>Object with a dasherized $str</p>
546
     */
547 38
    public function dasherize(): self
548
    {
549 38
        return static::create(
550 38
            $this->utf8::str_dasherize($this->str),
551 38
            $this->encoding
552
        );
553
    }
554
555
    /**
556
     * Returns a lowercase and trimmed string separated by the given delimiter.
557
     * Delimiters are inserted before uppercase characters (with the exception
558
     * of the first character of the string), and in place of spaces, dashes,
559
     * and underscores. Alpha delimiters are not converted to lowercase.
560
     *
561
     * @param string $delimiter <p>Sequence used to separate parts of the string.</p>
562
     *
563
     * @return static
564
     *                <p>Object with a delimited $str.</p>
565
     */
566 60
    public function delimit(string $delimiter): self
567
    {
568 60
        return static::create(
569 60
            $this->utf8::str_delimit($this->str, $delimiter),
570 60
            $this->encoding
571
        );
572
    }
573
574
    /**
575
     * Returns true if the string ends with $substring, false otherwise. By
576
     * default, the comparison is case-sensitive, but can be made insensitive
577
     * by setting $caseSensitive to false.
578
     *
579
     * @param string $substring     <p>The substring to look for.</p>
580
     * @param bool   $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p>
581
     *
582
     * @return bool
583
     *              <p>Whether or not $str ends with $substring.</p>
584
     */
585 54
    public function endsWith(string $substring, bool $caseSensitive = true): bool
586
    {
587 54
        if ($caseSensitive) {
588 30
            return $this->utf8::str_ends_with($this->str, $substring);
589
        }
590
591 24
        return $this->utf8::str_iends_with($this->str, $substring);
592
    }
593
594
    /**
595
     * Returns true if the string ends with any of $substrings, false otherwise.
596
     * By default, the comparison is case-sensitive, but can be made insensitive
597
     * by setting $caseSensitive to false.
598
     *
599
     * @param string[] $substrings    <p>Substrings to look for.</p>
600
     * @param bool     $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p>
601
     *
602
     * @return bool
603
     *              <p>Whether or not $str ends with $substring.</p>
604
     */
605 22
    public function endsWithAny(array $substrings, bool $caseSensitive = true): bool
606
    {
607 22
        if ($caseSensitive) {
608 14
            return $this->utf8::str_ends_with_any($this->str, $substrings);
609
        }
610
611 8
        return $this->utf8::str_iends_with_any($this->str, $substrings);
612
    }
613
614
    /**
615
     * Ensures that the string begins with $substring. If it doesn't, it's
616
     * prepended.
617
     *
618
     * @param string $substring <p>The substring to add if not present.</p>
619
     *
620
     * @return static
621
     *                <p>Object with its $str prefixed by the $substring.</p>
622
     */
623 20
    public function ensureLeft(string $substring): self
624
    {
625 20
        return static::create(
626 20
            $this->utf8::str_ensure_left($this->str, $substring),
627 20
            $this->encoding
628
        );
629
    }
630
631
    /**
632
     * Ensures that the string ends with $substring. If it doesn't, it's appended.
633
     *
634
     * @param string $substring <p>The substring to add if not present.</p>
635
     *
636
     * @return static
637
     *                <p>Object with its $str suffixed by the $substring.</p>
638
     */
639 20
    public function ensureRight(string $substring): self
640
    {
641 20
        return static::create(
642 20
            $this->utf8::str_ensure_right($this->str, $substring),
643 20
            $this->encoding
644
        );
645
    }
646
647
    /**
648
     * Create a escape html version of the string via "$this->utf8::htmlspecialchars()".
649
     *
650
     * @return static
651
     */
652 6
    public function escape(): self
653
    {
654 6
        return static::create(
655 6
            $this->utf8::htmlspecialchars(
656 6
                $this->str,
657 6
                \ENT_QUOTES | \ENT_SUBSTITUTE,
658 6
                $this->encoding
659
            ),
660 6
            $this->encoding
661
        );
662
    }
663
664
    /**
665
     * Create an extract from a sentence, so if the search-string was found, it try to centered in the output.
666
     *
667
     * @param string   $search
668
     * @param int|null $length                 [optional] <p>Default: null === text->length / 2</p>
669
     * @param string   $replacerForSkippedText [optional] <p>Default: …</p>
670
     *
671
     * @return static
672
     */
673 1
    public function extractText(string $search = '', int $length = null, string $replacerForSkippedText = '…'): self
674
    {
675 1
        return static::create(
676 1
            $this->utf8::extract_text(
677 1
                $this->str,
678 1
                $search,
679 1
                $length,
680 1
                $replacerForSkippedText,
681 1
                $this->encoding
682
            ),
683 1
            $this->encoding
684
        );
685
    }
686
687
    /**
688
     * Returns the first $n characters of the string.
689
     *
690
     * @param int $n <p>Number of characters to retrieve from the start.</p>
691
     *
692
     * @return static
693
     *                <p>Object with its $str being the first $n chars.</p>
694
     */
695 25
    public function first(int $n): self
696
    {
697 25
        return static::create(
698 25
            $this->utf8::first_char($this->str, $n, $this->encoding),
699 25
            $this->encoding
700
        );
701
    }
702
703
    /**
704
     * Returns the encoding used by the Stringy object.
705
     *
706
     * @return string
707
     *                <p>The current value of the $encoding property.</p>
708
     */
709 5
    public function getEncoding(): string
710
    {
711 5
        return $this->encoding;
712
    }
713
714
    /**
715
     * Returns a new ArrayIterator, thus implementing the IteratorAggregate
716
     * interface. The ArrayIterator's constructor is passed an array of chars
717
     * in the multibyte string. This enables the use of foreach with instances
718
     * of Stringy\Stringy.
719
     *
720
     * @return \ArrayIterator
721
     *                        <p>An iterator for the characters in the string.</p>
722
     */
723 2
    public function getIterator(): \ArrayIterator
724
    {
725 2
        return new \ArrayIterator($this->chars());
726
    }
727
728
    /**
729
     * Returns true if the string contains a lower case char, false otherwise.
730
     *
731
     * @return bool
732
     *              <p>Whether or not the string contains a lower case character.</p>
733
     */
734 24
    public function hasLowerCase(): bool
735
    {
736 24
        return $this->utf8::has_lowercase($this->str);
737
    }
738
739
    /**
740
     * Returns true if the string contains an upper case char, false otherwise.
741
     *
742
     * @return bool
743
     *              <p>Whether or not the string contains an upper case character.</p>
744
     */
745 24
    public function hasUpperCase(): bool
746
    {
747 24
        return $this->utf8::has_uppercase($this->str);
748
    }
749
750
    /**
751
     * Convert all HTML entities to their applicable characters.
752
     *
753
     * @param int $flags [optional] <p>
754
     *                   A bitmask of one or more of the following flags, which specify how to handle quotes and
755
     *                   which document type to use. The default is ENT_COMPAT.
756
     *                   <table>
757
     *                   Available <i>flags</i> constants
758
     *                   <tr valign="top">
759
     *                   <td>Constant Name</td>
760
     *                   <td>Description</td>
761
     *                   </tr>
762
     *                   <tr valign="top">
763
     *                   <td><b>ENT_COMPAT</b></td>
764
     *                   <td>Will convert double-quotes and leave single-quotes alone.</td>
765
     *                   </tr>
766
     *                   <tr valign="top">
767
     *                   <td><b>ENT_QUOTES</b></td>
768
     *                   <td>Will convert both double and single quotes.</td>
769
     *                   </tr>
770
     *                   <tr valign="top">
771
     *                   <td><b>ENT_NOQUOTES</b></td>
772
     *                   <td>Will leave both double and single quotes unconverted.</td>
773
     *                   </tr>
774
     *                   <tr valign="top">
775
     *                   <td><b>ENT_HTML401</b></td>
776
     *                   <td>
777
     *                   Handle code as HTML 4.01.
778
     *                   </td>
779
     *                   </tr>
780
     *                   <tr valign="top">
781
     *                   <td><b>ENT_XML1</b></td>
782
     *                   <td>
783
     *                   Handle code as XML 1.
784
     *                   </td>
785
     *                   </tr>
786
     *                   <tr valign="top">
787
     *                   <td><b>ENT_XHTML</b></td>
788
     *                   <td>
789
     *                   Handle code as XHTML.
790
     *                   </td>
791
     *                   </tr>
792
     *                   <tr valign="top">
793
     *                   <td><b>ENT_HTML5</b></td>
794
     *                   <td>
795
     *                   Handle code as HTML 5.
796
     *                   </td>
797
     *                   </tr>
798
     *                   </table>
799
     *                   </p>
800
     *
801
     * @return static
802
     *                <p>Object with the resulting $str after being html decoded.</p>
803
     */
804 10 View Code Duplication
    public function htmlDecode(int $flags = \ENT_COMPAT): self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
805
    {
806 10
        return static::create(
807 10
            $this->utf8::html_entity_decode(
808 10
                $this->str,
809 10
                $flags,
810 10
                $this->encoding
811
            ),
812 10
            $this->encoding
813
        );
814
    }
815
816
    /**
817
     * Convert all applicable characters to HTML entities.
818
     *
819
     * @param int $flags [optional] <p>
820
     *                   A bitmask of one or more of the following flags, which specify how to handle quotes and
821
     *                   which document type to use. The default is ENT_COMPAT.
822
     *                   <table>
823
     *                   Available <i>flags</i> constants
824
     *                   <tr valign="top">
825
     *                   <td>Constant Name</td>
826
     *                   <td>Description</td>
827
     *                   </tr>
828
     *                   <tr valign="top">
829
     *                   <td><b>ENT_COMPAT</b></td>
830
     *                   <td>Will convert double-quotes and leave single-quotes alone.</td>
831
     *                   </tr>
832
     *                   <tr valign="top">
833
     *                   <td><b>ENT_QUOTES</b></td>
834
     *                   <td>Will convert both double and single quotes.</td>
835
     *                   </tr>
836
     *                   <tr valign="top">
837
     *                   <td><b>ENT_NOQUOTES</b></td>
838
     *                   <td>Will leave both double and single quotes unconverted.</td>
839
     *                   </tr>
840
     *                   <tr valign="top">
841
     *                   <td><b>ENT_HTML401</b></td>
842
     *                   <td>
843
     *                   Handle code as HTML 4.01.
844
     *                   </td>
845
     *                   </tr>
846
     *                   <tr valign="top">
847
     *                   <td><b>ENT_XML1</b></td>
848
     *                   <td>
849
     *                   Handle code as XML 1.
850
     *                   </td>
851
     *                   </tr>
852
     *                   <tr valign="top">
853
     *                   <td><b>ENT_XHTML</b></td>
854
     *                   <td>
855
     *                   Handle code as XHTML.
856
     *                   </td>
857
     *                   </tr>
858
     *                   <tr valign="top">
859
     *                   <td><b>ENT_HTML5</b></td>
860
     *                   <td>
861
     *                   Handle code as HTML 5.
862
     *                   </td>
863
     *                   </tr>
864
     *                   </table>
865
     *                   </p>
866
     *
867
     * @return static
868
     *                <p>Object with the resulting $str after being html encoded.</p>
869
     */
870 10 View Code Duplication
    public function htmlEncode(int $flags = \ENT_COMPAT): self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
871
    {
872 10
        return static::create(
873 10
            $this->utf8::htmlentities(
874 10
                $this->str,
875 10
                $flags,
876 10
                $this->encoding
877
            ),
878 10
            $this->encoding
879
        );
880
    }
881
882
    /**
883
     * Capitalizes the first word of the string, replaces underscores with
884
     * spaces, and strips '_id'.
885
     *
886
     * @return static
887
     *                <p>Object with a humanized $str.</p>
888
     */
889 6
    public function humanize(): self
890
    {
891 6
        return static::create(
892 6
            $this->utf8::str_humanize($this->str),
893 6
            $this->encoding
894
        );
895
    }
896
897
    /**
898
     * Returns the index of the first occurrence of $needle in the string,
899
     * and false if not found. Accepts an optional offset from which to begin
900
     * the search.
901
     *
902
     * @param string $needle <p>Substring to look for.</p>
903
     * @param int    $offset [optional] <p>Offset from which to search. Default: 0</p>
904
     *
905
     * @return false|int
906
     *                   <p>The occurrence's <strong>index</strong> if found, otherwise <strong>false</strong>.</p>
907
     */
908 20
    public function indexOf(string $needle, int $offset = 0)
909
    {
910 20
        return $this->utf8::strpos(
911 20
            $this->str,
912 20
            $needle,
913 20
            $offset,
914 20
            $this->encoding
915
        );
916
    }
917
918
    /**
919
     * Returns the index of the first occurrence of $needle in the string,
920
     * and false if not found. Accepts an optional offset from which to begin
921
     * the search.
922
     *
923
     * @param string $needle <p>Substring to look for.</p>
924
     * @param int    $offset [optional] <p>Offset from which to search. Default: 0</p>
925
     *
926
     * @return false|int
927
     *                   <p>The occurrence's <strong>index</strong> if found, otherwise <strong>false</strong>.</p>
928
     */
929 10
    public function indexOfIgnoreCase(string $needle, int $offset = 0)
930
    {
931 10
        return $this->utf8::stripos(
932 10
            $this->str,
933 10
            $needle,
934 10
            $offset,
935 10
            $this->encoding
936
        );
937
    }
938
939
    /**
940
     * Returns the index of the last occurrence of $needle in the string,
941
     * and false if not found. Accepts an optional offset from which to begin
942
     * the search. Offsets may be negative to count from the last character
943
     * in the string.
944
     *
945
     * @param string $needle <p>Substring to look for.</p>
946
     * @param int    $offset [optional] <p>Offset from which to search. Default: 0</p>
947
     *
948
     * @return false|int
949
     *                   <p>The last occurrence's <strong>index</strong> if found, otherwise <strong>false</strong>.</p>
950
     */
951 20
    public function indexOfLast(string $needle, int $offset = 0)
952
    {
953 20
        return $this->utf8::strrpos(
954 20
            $this->str,
955 20
            $needle,
956 20
            $offset,
957 20
            $this->encoding
958
        );
959
    }
960
961
    /**
962
     * Returns the index of the last occurrence of $needle in the string,
963
     * and false if not found. Accepts an optional offset from which to begin
964
     * the search. Offsets may be negative to count from the last character
965
     * in the string.
966
     *
967
     * @param string $needle <p>Substring to look for.</p>
968
     * @param int    $offset [optional] <p>Offset from which to search. Default: 0</p>
969
     *
970
     * @return false|int
971
     *                   <p>The last occurrence's <strong>index</strong> if found, otherwise <strong>false</strong>.</p>
972
     */
973 10
    public function indexOfLastIgnoreCase(string $needle, int $offset = 0)
974
    {
975 10
        return $this->utf8::strripos(
976 10
            $this->str,
977 10
            $needle,
978 10
            $offset,
979 10
            $this->encoding
980
        );
981
    }
982
983
    /**
984
     * Inserts $substring into the string at the $index provided.
985
     *
986
     * @param string $substring <p>String to be inserted.</p>
987
     * @param int    $index     <p>The index at which to insert the substring.</p>
988
     *
989
     * @return static
990
     *                <p>Object with the resulting $str after the insertion.</p>
991
     */
992 16
    public function insert(string $substring, int $index): self
993
    {
994 16
        return static::create(
995 16
            $this->utf8::str_insert(
996 16
                $this->str,
997 16
                $substring,
998 16
                $index,
999 16
                $this->encoding
1000
            ),
1001 16
            $this->encoding
1002
        );
1003
    }
1004
1005
    /**
1006
     * Returns true if the string contains the $pattern, otherwise false.
1007
     *
1008
     * WARNING: Asterisks ("*") are translated into (".*") zero-or-more regular
1009
     * expression wildcards.
1010
     *
1011
     * @credit Originally from Laravel, thanks Taylor.
1012
     *
1013
     * @param string $pattern <p>The string or pattern to match against.</p>
1014
     *
1015
     * @return bool
1016
     *              <p>Whether or not we match the provided pattern.</p>
1017
     */
1018 13
    public function is(string $pattern): bool
1019
    {
1020 13
        if ($this->toString() === $pattern) {
1021 1
            return true;
1022
        }
1023
1024 12
        $quotedPattern = \preg_quote($pattern, '/');
1025 12
        $replaceWildCards = \str_replace('\*', '.*', $quotedPattern);
1026
1027 12
        return $this->matchesPattern('^' . $replaceWildCards . '\z');
1028
    }
1029
1030
    /**
1031
     * Returns true if the string contains only alphabetic chars, false otherwise.
1032
     *
1033
     * @return bool
1034
     *              <p>Whether or not $str contains only alphabetic chars.</p>
1035
     */
1036 20
    public function isAlpha(): bool
1037
    {
1038 20
        return $this->utf8::is_alpha($this->str);
1039
    }
1040
1041
    /**
1042
     * Returns true if the string contains only alphabetic and numeric chars, false otherwise.
1043
     *
1044
     * @return bool
1045
     *              <p>Whether or not $str contains only alphanumeric chars.</p>
1046
     */
1047 26
    public function isAlphanumeric(): bool
1048
    {
1049 26
        return $this->utf8::is_alphanumeric($this->str);
1050
    }
1051
1052
    /**
1053
     * Returns true if the string is base64 encoded, false otherwise.
1054
     *
1055
     * @param bool $emptyStringIsValid
1056
     *
1057
     * @return bool
1058
     *              <p>Whether or not $str is base64 encoded.</p>
1059
     */
1060 14
    public function isBase64($emptyStringIsValid = true): bool
1061
    {
1062 14
        return $this->utf8::is_base64($this->str, $emptyStringIsValid);
1063
    }
1064
1065
    /**
1066
     * Returns true if the string contains only whitespace chars, false otherwise.
1067
     *
1068
     * @return bool
1069
     *              <p>Whether or not $str contains only whitespace characters.</p>
1070
     */
1071 30
    public function isBlank(): bool
1072
    {
1073 30
        return $this->utf8::is_blank($this->str);
1074
    }
1075
1076
    /**
1077
     * Returns true if the string contains a valid E-Mail address, false otherwise.
1078
     *
1079
     * @param bool $useExampleDomainCheck   [optional] <p>Default: false</p>
1080
     * @param bool $useTypoInDomainCheck    [optional] <p>Default: false</p>
1081
     * @param bool $useTemporaryDomainCheck [optional] <p>Default: false</p>
1082
     * @param bool $useDnsCheck             [optional] <p>Default: false</p>
1083
     *
1084
     * @return bool
1085
     *              <p>Whether or not $str contains a valid E-Mail address.</p>
1086
     */
1087 1
    public function isEmail(bool $useExampleDomainCheck = false, bool $useTypoInDomainCheck = false, bool $useTemporaryDomainCheck = false, bool $useDnsCheck = false): bool
1088
    {
1089 1
        return EmailCheck::isValid($this->str, $useExampleDomainCheck, $useTypoInDomainCheck, $useTemporaryDomainCheck, $useDnsCheck);
1090
    }
1091
1092
    /**
1093
     * Determine whether the string is considered to be empty.
1094
     *
1095
     * A variable is considered empty if it does not exist or if its value equals FALSE.
1096
     * empty() does not generate a warning if the variable does not exist.
1097
     *
1098
     * @return bool
1099
     *              <p>Whether or not $str is empty().</p>
1100
     */
1101 5
    public function isEmpty(): bool
1102
    {
1103 5
        return $this->utf8::is_empty($this->str);
1104
    }
1105
1106
    /**
1107
     * Returns true if the string contains only hexadecimal chars, false otherwise.
1108
     *
1109
     * @return bool
1110
     *              <p>Whether or not $str contains only hexadecimal chars.</p>
1111
     */
1112 26
    public function isHexadecimal(): bool
1113
    {
1114 26
        return $this->utf8::is_hexadecimal($this->str);
1115
    }
1116
1117
    /**
1118
     * Returns true if the string contains HTML-Tags, false otherwise.
1119
     *
1120
     * @return bool
1121
     *              <p>Whether or not $str contains HTML-Tags.</p>
1122
     */
1123 1
    public function isHtml(): bool
1124
    {
1125 1
        return $this->utf8::is_html($this->str);
1126
    }
1127
1128
    /**
1129
     * Returns true if the string is JSON, false otherwise. Unlike json_decode
1130
     * in PHP 5.x, this method is consistent with PHP 7 and other JSON parsers,
1131
     * in that an empty string is not considered valid JSON.
1132
     *
1133
     * @param bool $onlyArrayOrObjectResultsAreValid
1134
     *
1135
     * @return bool
1136
     *              <p>Whether or not $str is JSON.</p>
1137
     */
1138 40
    public function isJson($onlyArrayOrObjectResultsAreValid = false): bool
1139
    {
1140 40
        return $this->utf8::is_json($this->str, $onlyArrayOrObjectResultsAreValid);
1141
    }
1142
1143
    /**
1144
     * Returns true if the string contains only lower case chars, false otherwise.
1145
     *
1146
     * @return bool
1147
     *              <p>Whether or not $str contains only lower case characters.</p>
1148
     */
1149 16
    public function isLowerCase(): bool
1150
    {
1151 16
        return $this->utf8::is_lowercase($this->str);
1152
    }
1153
1154
    /**
1155
     * Returns true if the string is serialized, false otherwise.
1156
     *
1157
     * @return bool
1158
     *              <p>Whether or not $str is serialized.</p>
1159
     */
1160 14
    public function isSerialized(): bool
1161
    {
1162 14
        return $this->utf8::is_serialized($this->str);
1163
    }
1164
1165
    /**
1166
     * Returns true if the string contains only lower case chars, false
1167
     * otherwise.
1168
     *
1169
     * @return bool
1170
     *              <p>Whether or not $str contains only lower case characters.</p>
1171
     */
1172 16
    public function isUpperCase(): bool
1173
    {
1174 16
        return $this->utf8::is_uppercase($this->str);
1175
    }
1176
1177
    /**
1178
     * Returns the last $n characters of the string.
1179
     *
1180
     * @param int $n <p>Number of characters to retrieve from the end.</p>
1181
     *
1182
     * @return static
1183
     *                <p>Object with its $str being the last $n chars.</p>
1184
     */
1185 24
    public function last(int $n): self
1186
    {
1187 24
        return static::create(
1188 24
            $this->utf8::str_last_char(
1189 24
                $this->str,
1190 24
                $n,
1191 24
                $this->encoding
1192
            ),
1193 24
            $this->encoding
1194
        );
1195
    }
1196
1197
    /**
1198
     * Gets the substring after (or before via "$beforeNeedle") the last occurrence of the "$needle".
1199
     * If no match is found returns new empty Stringy object.
1200
     *
1201
     * @param string $needle       <p>The string to look for.</p>
1202
     * @param bool   $beforeNeedle [optional] <p>Default: false</p>
1203
     *
1204
     * @return static
1205
     */
1206 2 View Code Duplication
    public function lastSubstringOf(string $needle, bool $beforeNeedle = false): self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1207
    {
1208 2
        return static::create(
1209 2
            $this->utf8::str_substr_last(
1210 2
                $this->str,
1211 2
                $needle,
1212 2
                $beforeNeedle,
1213 2
                $this->encoding
1214
            ),
1215 2
            $this->encoding
1216
        );
1217
    }
1218
1219
    /**
1220
     * Gets the substring after (or before via "$beforeNeedle") the last occurrence of the "$needle".
1221
     * If no match is found returns new empty Stringy object.
1222
     *
1223
     * @param string $needle       <p>The string to look for.</p>
1224
     * @param bool   $beforeNeedle [optional] <p>Default: false</p>
1225
     *
1226
     * @return static
1227
     */
1228 1 View Code Duplication
    public function lastSubstringOfIgnoreCase(string $needle, bool $beforeNeedle = false): self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1229
    {
1230 1
        return static::create(
1231 1
            $this->utf8::str_isubstr_last(
1232 1
                $this->str,
1233 1
                $needle,
1234 1
                $beforeNeedle,
1235 1
                $this->encoding
1236
            ),
1237 1
            $this->encoding
1238
        );
1239
    }
1240
1241
    /**
1242
     * Returns the length of the string.
1243
     *
1244
     * @return int
1245
     *             <p>The number of characters in $str given the encoding.</p>
1246
     */
1247 11
    public function length(): int
1248
    {
1249 11
        return (int) $this->utf8::strlen($this->str, $this->encoding);
1250
    }
1251
1252
    /**
1253
     * Line-Wrap the string after $limit, but also after the next word.
1254
     *
1255
     * @param int $limit
1256
     *
1257
     * @return static
1258
     */
1259 1
    public function lineWrapAfterWord(int $limit): self
1260
    {
1261 1
        return static::create(
1262 1
            $this->utf8::wordwrap_per_line($this->str, $limit),
1263 1
            $this->encoding
1264
        );
1265
    }
1266
1267
    /**
1268
     * Splits on newlines and carriage returns, returning an array of Stringy
1269
     * objects corresponding to the lines in the string.
1270
     *
1271
     * @return static[]
1272
     *                  <p>An array of Stringy objects.</p>
1273
     */
1274 34
    public function lines(): array
1275
    {
1276 34
        $array = $this->utf8::str_to_lines($this->str);
1277 34
        foreach ($array as $i => &$value) {
1278 34
            $value = static::create($value, $this->encoding);
1279
        }
1280
1281 34
        return $array;
1282
    }
1283
1284
    /**
1285
     * Returns the longest common prefix between the string and $otherStr.
1286
     *
1287
     * @param string $otherStr <p>Second string for comparison.</p>
1288
     *
1289
     * @return static
1290
     *                <p>Object with its $str being the longest common prefix.</p>
1291
     */
1292 20
    public function longestCommonPrefix(string $otherStr): self
1293
    {
1294 20
        return static::create(
1295 20
            $this->utf8::str_longest_common_prefix(
1296 20
                $this->str,
1297 20
                $otherStr,
1298 20
                $this->encoding
1299
            ),
1300 20
            $this->encoding
1301
        );
1302
    }
1303
1304
    /**
1305
     * Returns the longest common substring between the string and $otherStr.
1306
     * In the case of ties, it returns that which occurs first.
1307
     *
1308
     * @param string $otherStr <p>Second string for comparison.</p>
1309
     *
1310
     * @return static
1311
     *                <p>Object with its $str being the longest common substring.</p>
1312
     */
1313 20
    public function longestCommonSubstring(string $otherStr): self
1314
    {
1315 20
        return static::create(
1316 20
            $this->utf8::str_longest_common_substring(
1317 20
                $this->str,
1318 20
                $otherStr,
1319 20
                $this->encoding
1320
            ),
1321 20
            $this->encoding
1322
        );
1323
    }
1324
1325
    /**
1326
     * Returns the longest common suffix between the string and $otherStr.
1327
     *
1328
     * @param string $otherStr <p>Second string for comparison.</p>
1329
     *
1330
     * @return static
1331
     *                <p>Object with its $str being the longest common suffix.</p>
1332
     */
1333 20
    public function longestCommonSuffix(string $otherStr): self
1334
    {
1335 20
        return static::create(
1336 20
            $this->utf8::str_longest_common_suffix(
1337 20
                $this->str,
1338 20
                $otherStr,
1339 20
                $this->encoding
1340
            ),
1341 20
            $this->encoding
1342
        );
1343
    }
1344
1345
    /**
1346
     * Converts the first character of the string to lower case.
1347
     *
1348
     * @return static
1349
     *                <p>Object with the first character of $str being lower case.</p>
1350
     */
1351 10
    public function lowerCaseFirst(): self
1352
    {
1353 10
        return static::create(
1354 10
            $this->utf8::lcfirst($this->str, $this->encoding),
1355 10
            $this->encoding
1356
        );
1357
    }
1358
1359
    /**
1360
     * Returns whether or not a character exists at an index. Offsets may be
1361
     * negative to count from the last character in the string. Implements
1362
     * part of the ArrayAccess interface.
1363
     *
1364
     * @param int $offset <p>The index to check.</p>
1365
     *
1366
     * @return bool
1367
     *              <p>Whether or not the index exists.</p>
1368
     */
1369 12
    public function offsetExists($offset): bool
1370
    {
1371 12
        return $this->utf8::str_offset_exists(
1372 12
            $this->str,
1373 12
            $offset,
1374 12
            $this->encoding
1375
        );
1376
    }
1377
1378
    /**
1379
     * Returns the character at the given index. Offsets may be negative to
1380
     * count from the last character in the string. Implements part of the
1381
     * ArrayAccess interface, and throws an OutOfBoundsException if the index
1382
     * does not exist.
1383
     *
1384
     * @param int $offset <p>The <strong>index</strong> from which to retrieve the char.</p>
1385
     *
1386
     *@throws \OutOfBoundsException
1387
     *                               <p>If the positive or negative offset does not exist.</p>
1388
     *
1389
     * @return string
1390
     *                <p>The character at the specified index.</p>
1391
     */
1392 4
    public function offsetGet($offset): string
1393
    {
1394 4
        return $this->utf8::str_offset_get($this->str, $offset, $this->encoding);
1395
    }
1396
1397
    /**
1398
     * Implements part of the ArrayAccess interface, but throws an exception
1399
     * when called. This maintains the immutability of Stringy objects.
1400
     *
1401
     * @param int   $offset <p>The index of the character.</p>
1402
     * @param mixed $value  <p>Value to set.</p>
1403
     *
1404
     * @throws \Exception
1405
     *                    <p>When called.</p>
1406
     */
1407 2
    public function offsetSet($offset, $value)
1408
    {
1409
        // Stringy is immutable, cannot directly set char
1410
        /** @noinspection ThrowRawExceptionInspection */
1411 2
        throw new \Exception('Stringy object is immutable, cannot modify char');
1412
    }
1413
1414
    /**
1415
     * Implements part of the ArrayAccess interface, but throws an exception
1416
     * when called. This maintains the immutability of Stringy objects.
1417
     *
1418
     * @param int $offset <p>The index of the character.</p>
1419
     *
1420
     * @throws \Exception
1421
     *                    <p>When called.</p>
1422
     */
1423 2
    public function offsetUnset($offset)
1424
    {
1425
        // Don't allow directly modifying the string
1426
        /** @noinspection ThrowRawExceptionInspection */
1427 2
        throw new \Exception('Stringy object is immutable, cannot unset char');
1428
    }
1429
1430
    /**
1431
     * Pads the string to a given length with $padStr. If length is less than
1432
     * or equal to the length of the string, no padding takes places. The
1433
     * default string used for padding is a space, and the default type (one of
1434
     * 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException
1435
     * if $padType isn't one of those 3 values.
1436
     *
1437
     * @param int    $length  <p>Desired string length after padding.</p>
1438
     * @param string $padStr  [optional] <p>String used to pad, defaults to space. Default: ' '</p>
1439
     * @param string $padType [optional] <p>One of 'left', 'right', 'both'. Default: 'right'</p>
1440
     *
1441
     * @throws \InvalidArgumentException
1442
     *                                   <p>If $padType isn't one of 'right', 'left' or 'both'.</p>
1443
     *
1444
     * @return static
1445
     *                <p>Object with a padded $str.</p>
1446
     */
1447 26
    public function pad(int $length, string $padStr = ' ', string $padType = 'right'): self
1448
    {
1449 26
        return static::create(
1450 26
            $this->utf8::str_pad(
1451 26
                $this->str,
1452 26
                $length,
1453 26
                $padStr,
1454 26
                $padType,
1455 26
                $this->encoding
1456
            )
1457
        );
1458
    }
1459
1460
    /**
1461
     * Returns a new string of a given length such that both sides of the
1462
     * string are padded. Alias for pad() with a $padType of 'both'.
1463
     *
1464
     * @param int    $length <p>Desired string length after padding.</p>
1465
     * @param string $padStr [optional] <p>String used to pad, defaults to space. Default: ' '</p>
1466
     *
1467
     * @return static
1468
     *                <p>String with padding applied.</p>
1469
     */
1470 22
    public function padBoth(int $length, string $padStr = ' '): self
1471
    {
1472 22
        return static::create(
1473 22
            $this->utf8::str_pad_both(
1474 22
                $this->str,
1475 22
                $length,
1476 22
                $padStr,
1477 22
                $this->encoding
1478
            )
1479
        );
1480
    }
1481
1482
    /**
1483
     * Returns a new string of a given length such that the beginning of the
1484
     * string is padded. Alias for pad() with a $padType of 'left'.
1485
     *
1486
     * @param int    $length <p>Desired string length after padding.</p>
1487
     * @param string $padStr [optional] <p>String used to pad, defaults to space. Default: ' '</p>
1488
     *
1489
     * @return static
1490
     *                <p>String with left padding.</p>
1491
     */
1492 14
    public function padLeft(int $length, string $padStr = ' '): self
1493
    {
1494 14
        return static::create(
1495 14
            $this->utf8::str_pad_left(
1496 14
                $this->str,
1497 14
                $length,
1498 14
                $padStr,
1499 14
                $this->encoding
1500
            )
1501
        );
1502
    }
1503
1504
    /**
1505
     * Returns a new string of a given length such that the end of the string
1506
     * is padded. Alias for pad() with a $padType of 'right'.
1507
     *
1508
     * @param int    $length <p>Desired string length after padding.</p>
1509
     * @param string $padStr [optional] <p>String used to pad, defaults to space. Default: ' '</p>
1510
     *
1511
     * @return static
1512
     *                <p>String with right padding.</p>
1513
     */
1514 14
    public function padRight(int $length, string $padStr = ' '): self
1515
    {
1516 14
        return static::create(
1517 14
            $this->utf8::str_pad_right(
1518 14
                $this->str,
1519 14
                $length,
1520 14
                $padStr,
1521 14
                $this->encoding
1522
            )
1523
        );
1524
    }
1525
1526
    /**
1527
     * Returns a new string starting with $string.
1528
     *
1529
     * @param string $string <p>The string to append.</p>
1530
     *
1531
     * @return static
1532
     *                <p>Object with appended $string.</p>
1533
     */
1534 4
    public function prepend(string $string): self
1535
    {
1536 4
        return static::create($string . $this->str, $this->encoding);
1537
    }
1538
1539
    /**
1540
     * Replaces all occurrences of $pattern in $str by $replacement.
1541
     *
1542
     * @param string $pattern     <p>The regular expression pattern.</p>
1543
     * @param string $replacement <p>The string to replace with.</p>
1544
     * @param string $options     [optional] <p>Matching conditions to be used.</p>
1545
     * @param string $delimiter   [optional] <p>Delimiter the the regex. Default: '/'</p>
1546
     *
1547
     * @return static
1548
     *                <p>Object with the result2ing $str after the replacements.</p>
1549
     */
1550 19
    public function regexReplace(string $pattern, string $replacement, string $options = '', string $delimiter = '/'): self
1551
    {
1552 19
        return static::create(
1553 19
            $this->utf8::regex_replace(
1554 19
                $this->str,
1555 19
                $pattern,
1556 19
                $replacement,
1557 19
                $options,
1558 19
                $delimiter
1559
            ),
1560 19
            $this->encoding
1561
        );
1562
    }
1563
1564
    /**
1565
     * Remove html via "strip_tags()" from the string.
1566
     *
1567
     * @param string $allowableTags [optional] <p>You can use the optional second parameter to specify tags which should
1568
     *                              not be stripped. Default: null
1569
     *                              </p>
1570
     *
1571
     * @return static
1572
     */
1573 6
    public function removeHtml(string $allowableTags = null): self
1574
    {
1575 6
        return static::create(
1576 6
            $this->utf8::remove_html($this->str, $allowableTags . ''),
1577 6
            $this->encoding
1578
        );
1579
    }
1580
1581
    /**
1582
     * Remove all breaks [<br> | \r\n | \r | \n | ...] from the string.
1583
     *
1584
     * @param string $replacement [optional] <p>Default is a empty string.</p>
1585
     *
1586
     * @return static
1587
     */
1588 6
    public function removeHtmlBreak(string $replacement = ''): self
1589
    {
1590 6
        return static::create(
1591 6
            $this->utf8::remove_html_breaks($this->str, $replacement),
1592 6
            $this->encoding
1593
        );
1594
    }
1595
1596
    /**
1597
     * Returns a new string with the prefix $substring removed, if present.
1598
     *
1599
     * @param string $substring <p>The prefix to remove.</p>
1600
     *
1601
     * @return static
1602
     *                <p>Object having a $str without the prefix $substring.</p>
1603
     */
1604 24
    public function removeLeft(string $substring): self
1605
    {
1606 24
        return static::create(
1607 24
            $this->utf8::remove_left($this->str, $substring, $this->encoding),
1608 24
            $this->encoding
1609
        );
1610
    }
1611
1612
    /**
1613
     * Returns a new string with the suffix $substring removed, if present.
1614
     *
1615
     * @param string $substring <p>The suffix to remove.</p>
1616
     *
1617
     * @return static
1618
     *                <p>Object having a $str without the suffix $substring.</p>
1619
     */
1620 24
    public function removeRight(string $substring): self
1621
    {
1622 24
        return static::create(
1623 24
            $this->utf8::remove_right($this->str, $substring, $this->encoding),
1624 24
            $this->encoding
1625
        );
1626
    }
1627
1628
    /**
1629
     * Try to remove all XSS-attacks from the string.
1630
     *
1631
     * @return static
1632
     */
1633 6
    public function removeXss(): self
1634
    {
1635 6
        static $antiXss = null;
1636
1637 6
        if ($antiXss === null) {
1638 1
            $antiXss = new AntiXSS();
1639
        }
1640
1641 6
        $str = $antiXss->xss_clean($this->str);
1642
1643 6
        return static::create($str, $this->encoding);
1644
    }
1645
1646
    /**
1647
     * Returns a repeated string given a multiplier.
1648
     *
1649
     * @param int $multiplier <p>The number of times to repeat the string.</p>
1650
     *
1651
     * @return static
1652
     *                <p>Object with a repeated str.</p>
1653
     */
1654 14
    public function repeat(int $multiplier): self
1655
    {
1656 14
        return static::create(
1657 14
            \str_repeat($this->str, $multiplier),
1658 14
            $this->encoding
1659
        );
1660
    }
1661
1662
    /**
1663
     * Replaces all occurrences of $search in $str by $replacement.
1664
     *
1665
     * @param string $search        <p>The needle to search for.</p>
1666
     * @param string $replacement   <p>The string to replace with.</p>
1667
     * @param bool   $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p>
1668
     *
1669
     * @return static
1670
     *                <p>Object with the resulting $str after the replacements.</p>
1671
     */
1672 45
    public function replace(string $search, string $replacement, bool $caseSensitive = true): self
1673
    {
1674 45
        if ($search === '' && $replacement === '') {
1675 10
            return static::create($this->str, $this->encoding);
1676
        }
1677
1678 35
        if ($this->str === '' && $search === '') {
1679 2
            return static::create($replacement, $this->encoding);
1680
        }
1681
1682 33
        if ($caseSensitive) {
1683 28
            return static::create(
1684 28
                $this->utf8::str_replace($search, $replacement, $this->str),
1685 28
                $this->encoding
1686
            );
1687
        }
1688
1689 5
        return static::create(
1690 5
            $this->utf8::str_ireplace($search, $replacement, $this->str),
1691 5
            $this->encoding
1692
        );
1693
    }
1694
1695
    /**
1696
     * Replaces all occurrences of $search in $str by $replacement.
1697
     *
1698
     * @param array        $search        <p>The elements to search for.</p>
1699
     * @param array|string $replacement   <p>The string to replace with.</p>
1700
     * @param bool         $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p>
1701
     *
1702
     * @return static
1703
     *                <p>Object with the resulting $str after the replacements.</p>
1704
     */
1705 30
    public function replaceAll(array $search, $replacement, bool $caseSensitive = true): self
1706
    {
1707 30
        if ($caseSensitive) {
1708 23
            return static::create(
1709 23
                $this->utf8::str_replace($search, $replacement, $this->str),
1710 23
                $this->encoding
1711
            );
1712
        }
1713
1714 7
        return static::create(
1715 7
            $this->utf8::str_ireplace($search, $replacement, $this->str),
1716 7
            $this->encoding
1717
        );
1718
    }
1719
1720
    /**
1721
     * Replaces first occurrences of $search from the beginning of string with $replacement.
1722
     *
1723
     * @param string $search      <p>The string to search for.</p>
1724
     * @param string $replacement <p>The replacement.</p>
1725
     *
1726
     * @return static
1727
     *                <p>Object with the resulting $str after the replacements.</p>
1728
     */
1729 16
    public function replaceFirst(string $search, string $replacement): self
1730
    {
1731 16
        return static::create(
1732 16
            $this->utf8::str_replace_first($search, $replacement, $this->str),
1733 16
            $this->encoding
1734
        );
1735
    }
1736
1737
    /**
1738
     * Replaces last occurrences of $search from the ending of string with $replacement.
1739
     *
1740
     * @param string $search      <p>The string to search for.</p>
1741
     * @param string $replacement <p>The replacement.</p>
1742
     *
1743
     * @return static
1744
     *                <p>Object with the resulting $str after the replacements.</p>
1745
     */
1746 15
    public function replaceLast(string $search, string $replacement): self
1747
    {
1748 15
        return static::create(
1749 15
            $this->utf8::str_replace_last($search, $replacement, $this->str),
1750 15
            $this->encoding
1751
        );
1752
    }
1753
1754
    /**
1755
     * Replaces all occurrences of $search from the beginning of string with $replacement.
1756
     *
1757
     * @param string $search      <p>The string to search for.</p>
1758
     * @param string $replacement <p>The replacement.</p>
1759
     *
1760
     * @return static
1761
     *                <p>Object with the resulting $str after the replacements.</p>
1762
     */
1763 16
    public function replaceBeginning(string $search, string $replacement): self
1764
    {
1765 16
        return static::create(
1766 16
            $this->utf8::str_replace_beginning($this->str, $search, $replacement),
1767 16
            $this->encoding
1768
        );
1769
    }
1770
1771
    /**
1772
     * Replaces all occurrences of $search from the ending of string with $replacement.
1773
     *
1774
     * @param string $search      <p>The string to search for.</p>
1775
     * @param string $replacement <p>The replacement.</p>
1776
     *
1777
     * @return static
1778
     *                <p>Object with the resulting $str after the replacements.</p>
1779
     */
1780 16
    public function replaceEnding(string $search, string $replacement): self
1781
    {
1782 16
        return static::create(
1783 16
            $this->utf8::str_replace_ending($this->str, $search, $replacement),
1784 16
            $this->encoding
1785
        );
1786
    }
1787
1788
    /**
1789
     * Returns a reversed string. A multibyte version of strrev().
1790
     *
1791
     * @return static
1792
     *                <p>Object with a reversed $str.</p>
1793
     */
1794 10
    public function reverse(): self
1795
    {
1796 10
        return static::create($this->utf8::strrev($this->str), $this->encoding);
1797
    }
1798
1799
    /**
1800
     * Truncates the string to a given length, while ensuring that it does not
1801
     * split words. If $substring is provided, and truncating occurs, the
1802
     * string is further truncated so that the substring may be appended without
1803
     * exceeding the desired length.
1804
     *
1805
     * @param int    $length                          <p>Desired length of the truncated string.</p>
1806
     * @param string $substring                       [optional] <p>The substring to append if it can fit. Default: ''</p>
1807
     * @param bool   $ignoreDoNotSplitWordsForOneWord
1808
     *
1809
     * @return static
1810
     *                <p>Object with the resulting $str after truncating.</p>
1811
     */
1812 45
    public function safeTruncate(int $length, string $substring = '', bool $ignoreDoNotSplitWordsForOneWord = true): self
1813
    {
1814 45
        return static::create(
1815 45
            $this->utf8::str_truncate_safe(
1816 45
                $this->str,
1817 45
                $length,
1818 45
                $substring,
1819 45
                $this->encoding,
1820 45
                $ignoreDoNotSplitWordsForOneWord
1821
            ),
1822 45
            $this->encoding
1823
        );
1824
    }
1825
1826
    /**
1827
     * Shorten the string after $length, but also after the next word.
1828
     *
1829
     * @param int    $length
1830
     * @param string $strAddOn [optional] <p>Default: '…'</p>
1831
     *
1832
     * @return static
1833
     */
1834 4
    public function shortenAfterWord(int $length, string $strAddOn = '…'): self
1835
    {
1836 4
        return static::create(
1837 4
            $this->utf8::str_limit_after_word($this->str, $length, $strAddOn),
1838 4
            $this->encoding
1839
        );
1840
    }
1841
1842
    /**
1843
     * A multibyte string shuffle function. It returns a string with its
1844
     * characters in random order.
1845
     *
1846
     * @return static
1847
     *                <p>Object with a shuffled $str.</p>
1848
     */
1849 6
    public function shuffle(): self
1850
    {
1851 6
        return static::create($this->utf8::str_shuffle($this->str), $this->encoding);
1852
    }
1853
1854
    /**
1855
     * Returns the substring beginning at $start, and up to, but not including
1856
     * the index specified by $end. If $end is omitted, the function extracts
1857
     * the remaining string. If $end is negative, it is computed from the end
1858
     * of the string.
1859
     *
1860
     * @param int $start <p>Initial index from which to begin extraction.</p>
1861
     * @param int $end   [optional] <p>Index at which to end extraction. Default: null</p>
1862
     *
1863
     * @return static
1864
     *                <p>Object with its $str being the extracted substring.</p>
1865
     */
1866 34
    public function slice(int $start, int $end = null): self
1867
    {
1868 34
        return static::create(
1869 34
            $this->utf8::str_slice($this->str, $start, $end, $this->encoding),
1870 34
            $this->encoding
1871
        );
1872
    }
1873
1874
    /**
1875
     * Converts the string into an URL slug. This includes replacing non-ASCII
1876
     * characters with their closest ASCII equivalents, removing remaining
1877
     * non-ASCII and non-alphanumeric characters, and replacing whitespace with
1878
     * $separator. The separator defaults to a single dash, and the string
1879
     * is also converted to lowercase. The language of the source string can
1880
     * also be supplied for language-specific transliteration.
1881
     *
1882
     * @param string   $separator    [optional] <p>The string used to replace whitespace.</p>
1883
     * @param string   $language     [optional] <p>Language of the source string.</p>
1884
     * @param string[] $replacements [optional] <p>A map of replaceable strings.</p>
1885
     *
1886
     * @return static
1887
     *                <p>Object whose $str has been converted to an URL slug.</p>
1888
     */
1889 17
    public function slugify(
1890
        string $separator = '-',
1891
        string $language = 'en',
1892
        array $replacements = []
1893
    ): self {
1894 17
        return static::create(
1895 17
            $this->ascii::to_slugify(
1896 17
                $this->str,
1897 17
                $separator,
1898 17
                $language,
1899 17
                $replacements
1900
            ),
1901 17
            $this->encoding
1902
        );
1903
    }
1904
1905
    /**
1906
     * Converts the string into an URL slug. This includes replacing non-ASCII
1907
     * characters with their closest ASCII equivalents, removing remaining
1908
     * non-ASCII and non-alphanumeric characters, and replacing whitespace with
1909
     * $replacement. The replacement defaults to a single dash, and the string
1910
     * is also converted to lowercase.
1911
     *
1912
     * @param string $separator  [optional] <p>The string used to replace whitespace. Default: '-'</p>
1913
     * @param string $language   [optional] <p>The language for the url. Default: 'de'</p>
1914
     * @param bool   $strToLower [optional] <p>string to lower. Default: true</p>
1915
     *
1916
     * @return static
1917
     *                <p>Object whose $str has been converted to an URL slug.</p>
1918
     */
1919 15
    public function urlify(
1920
        string $separator = '-',
1921
        string $language = 'de',
1922
        bool $strToLower = true
1923
    ): self {
1924 15
        return static::create(
1925 15
            URLify::slug(
1926 15
                $this->str,
1927 15
                $language,
1928 15
                $separator,
1929 15
                $strToLower
1930
            ),
1931 15
            $this->encoding
1932
        );
1933
    }
1934
1935
    /**
1936
     * Convert a string to e.g.: "snake_case"
1937
     *
1938
     * @return static
1939
     *                <p>Object with $str in snake_case.</p>
1940
     */
1941 20
    public function snakeize(): self
1942
    {
1943 20
        return static::create(
1944 20
            $this->utf8::str_snakeize($this->str, $this->encoding),
1945 20
            $this->encoding
1946
        );
1947
    }
1948
1949
    /**
1950
     * Splits the string with the provided regular expression, returning an
1951
     * array of Stringy objects. An optional integer $limit will truncate the
1952
     * results.
1953
     *
1954
     * @param string $pattern <p>The regex with which to split the string.</p>
1955
     * @param int    $limit   [optional] <p>Maximum number of results to return. Default: -1 === no limit</p>
1956
     *
1957
     * @return static[]
1958
     *                  <p>An array of Stringy objects.</p>
1959
     */
1960 35
    public function split(string $pattern, int $limit = null): array
1961
    {
1962 35
        if ($limit === null) {
1963 7
            $limit = -1;
1964
        }
1965
1966 35
        $array = $this->utf8::str_split_pattern($this->str, $pattern, $limit);
1967 35
        foreach ($array as $i => &$value) {
1968 31
            $value = static::create($value, $this->encoding);
1969
        }
1970
1971 35
        return $array;
1972
    }
1973
1974
    /**
1975
     * Returns true if the string begins with $substring, false otherwise. By
1976
     * default, the comparison is case-sensitive, but can be made insensitive
1977
     * by setting $caseSensitive to false.
1978
     *
1979
     * @param string $substring     <p>The substring to look for.</p>
1980
     * @param bool   $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p>
1981
     *
1982
     * @return bool
1983
     *              <p>Whether or not $str starts with $substring.</p>
1984
     */
1985 55
    public function startsWith(string $substring, bool $caseSensitive = true): bool
1986
    {
1987 55
        if ($caseSensitive) {
1988 30
            return $this->utf8::str_starts_with($this->str, $substring);
1989
        }
1990
1991 25
        return $this->utf8::str_istarts_with($this->str, $substring);
1992
    }
1993
1994
    /**
1995
     * Returns true if the string begins with any of $substrings, false otherwise.
1996
     * By default the comparison is case-sensitive, but can be made insensitive by
1997
     * setting $caseSensitive to false.
1998
     *
1999
     * @param array $substrings    <p>Substrings to look for.</p>
2000
     * @param bool  $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p>
2001
     *
2002
     * @return bool
2003
     *              <p>Whether or not $str starts with $substring.</p>
2004
     */
2005 23
    public function startsWithAny(array $substrings, bool $caseSensitive = true): bool
2006
    {
2007 23
        if ($caseSensitive) {
2008 15
            return $this->utf8::str_starts_with_any($this->str, $substrings);
2009
        }
2010
2011 8
        return $this->utf8::str_istarts_with_any($this->str, $substrings);
2012
    }
2013
2014
    /**
2015
     * Strip all whitespace characters. This includes tabs and newline characters,
2016
     * as well as multibyte whitespace such as the thin space and ideographic space.
2017
     *
2018
     * @return static
2019
     */
2020 24
    public function stripWhitespace(): self
2021
    {
2022 24
        return static::create(
2023 24
            $this->utf8::strip_whitespace($this->str),
2024 24
            $this->encoding
2025
        );
2026
    }
2027
2028
    /**
2029
     * Remove css media-queries.
2030
     *
2031
     * @return static
2032
     */
2033 1
    public function stripeCssMediaQueries(): self
2034
    {
2035 1
        return static::create(
2036 1
            $this->utf8::css_stripe_media_queries($this->str),
2037 1
            $this->encoding
2038
        );
2039
    }
2040
2041
    /**
2042
     * Remove empty html-tag.
2043
     *
2044
     * e.g.: <tag></tag>
2045
     *
2046
     * @return static
2047
     */
2048 1
    public function stripeEmptyHtmlTags(): self
2049
    {
2050 1
        return static::create(
2051 1
            $this->utf8::html_stripe_empty_tags($this->str),
2052 1
            $this->encoding
2053
        );
2054
    }
2055
2056
    /**
2057
     * Returns the substring beginning at $start with the specified $length.
2058
     * It differs from the $this->utf8::substr() function in that providing a $length of
2059
     * null will return the rest of the string, rather than an empty string.
2060
     *
2061
     * @param int $start  <p>Position of the first character to use.</p>
2062
     * @param int $length [optional] <p>Maximum number of characters used. Default: null</p>
2063
     *
2064
     * @return static
2065
     *                <p>Object with its $str being the substring.</p>
2066
     */
2067 18
    public function substr(int $start, int $length = null): self
2068
    {
2069 18
        return static::create(
2070 18
            $this->utf8::substr(
2071 18
                $this->str,
2072 18
                $start,
2073 18
                $length,
2074 18
                $this->encoding
2075
            ),
2076 18
            $this->encoding
2077
        );
2078
    }
2079
2080
    /**
2081
     * Gets the substring after (or before via "$beforeNeedle") the first occurrence of the "$needle".
2082
     * If no match is found returns new empty Stringy object.
2083
     *
2084
     * @param string $needle       <p>The string to look for.</p>
2085
     * @param bool   $beforeNeedle [optional] <p>Default: false</p>
2086
     *
2087
     * @return static
2088
     */
2089 2 View Code Duplication
    public function substringOf(string $needle, bool $beforeNeedle = false): self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2090
    {
2091 2
        return static::create(
2092 2
            $this->utf8::str_substr_first(
2093 2
                $this->str,
2094 2
                $needle,
2095 2
                $beforeNeedle,
2096 2
                $this->encoding
2097
            ),
2098 2
            $this->encoding
2099
        );
2100
    }
2101
2102
    /**
2103
     * Gets the substring after (or before via "$beforeNeedle") the first occurrence of the "$needle".
2104
     * If no match is found returns new empty Stringy object.
2105
     *
2106
     * @param string $needle       <p>The string to look for.</p>
2107
     * @param bool   $beforeNeedle [optional] <p>Default: false</p>
2108
     *
2109
     * @return static
2110
     */
2111 2 View Code Duplication
    public function substringOfIgnoreCase(string $needle, bool $beforeNeedle = false): self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2112
    {
2113 2
        return static::create(
2114 2
            $this->utf8::str_isubstr_first(
2115 2
                $this->str,
2116 2
                $needle,
2117 2
                $beforeNeedle,
2118 2
                $this->encoding
2119
            ),
2120 2
            $this->encoding
2121
        );
2122
    }
2123
2124
    /**
2125
     * Surrounds $str with the given substring.
2126
     *
2127
     * @param string $substring <p>The substring to add to both sides.</P>
2128
     *
2129
     * @return static
2130
     *                <p>Object whose $str had the substring both prepended and appended.</p>
2131
     */
2132 10
    public function surround(string $substring): self
2133
    {
2134 10
        return static::create(
2135 10
            $substring . $this->str . $substring,
2136 10
            $this->encoding
2137
        );
2138
    }
2139
2140
    /**
2141
     * Returns a case swapped version of the string.
2142
     *
2143
     * @return static
2144
     *                <p>Object whose $str has each character's case swapped.</P>
2145
     */
2146 10
    public function swapCase(): self
2147
    {
2148 10
        return static::create(
2149 10
            $this->utf8::swapCase($this->str, $this->encoding),
2150 10
            $this->encoding
2151
        );
2152
    }
2153
2154
    /**
2155
     * Returns a string with smart quotes, ellipsis characters, and dashes from
2156
     * Windows-1252 (commonly used in Word documents) replaced by their ASCII
2157
     * equivalents.
2158
     *
2159
     * @return static
2160
     *                <p>Object whose $str has those characters removed.</p>
2161
     */
2162 8
    public function tidy(): self
2163
    {
2164 8
        return static::create(
2165 8
            $this->ascii::normalize_msword($this->str),
2166 8
            $this->encoding
2167
        );
2168
    }
2169
2170
    /**
2171
     * Returns a trimmed string with the first letter of each word capitalized.
2172
     * Also accepts an array, $ignore, allowing you to list words not to be
2173
     * capitalized.
2174
     *
2175
     * @param array|string[]|null $ignore            [optional] <p>An array of words not to capitalize or null. Default: null</p>
2176
     * @param string|null         $word_define_chars [optional] <p>An string of chars that will be used as whitespace separator === words.</p>
2177
     * @param string|null         $language          [optional] <p>Language of the source string.</p>
2178
     *
2179
     * @return static
2180
     *                <p>Object with a titleized $str.</p>
2181
     */
2182 14
    public function titleize(
2183
        array $ignore = null,
2184
        string $word_define_chars = null,
2185
        string $language = null
2186
    ): self {
2187 14
        return static::create(
2188 14
            $this->utf8::str_titleize(
2189 14
                $this->str,
2190 14
                $ignore,
2191 14
                $this->encoding,
2192 14
                false,
2193 14
                $language,
2194 14
                false,
2195 14
                true,
2196 14
                $word_define_chars
2197
            ),
2198 14
            $this->encoding
2199
        );
2200
    }
2201
2202
    /**
2203
     * Returns a trimmed string in proper title case.
2204
     *
2205
     * Also accepts an array, $ignore, allowing you to list words not to be
2206
     * capitalized.
2207
     *
2208
     * Adapted from John Gruber's script.
2209
     *
2210
     * @see https://gist.github.com/gruber/9f9e8650d68b13ce4d78
2211
     *
2212
     * @param array $ignore <p>An array of words not to capitalize.</p>
2213
     *
2214
     * @return static
2215
     *                <p>Object with a titleized $str</p>
2216
     */
2217 35
    public function titleizeForHumans(array $ignore = []): self
2218
    {
2219 35
        return static::create(
2220 35
            $this->utf8::str_titleize_for_humans(
2221 35
                $this->str,
2222 35
                $ignore,
2223 35
                $this->encoding
2224
            ),
2225 35
            $this->encoding
2226
        );
2227
    }
2228
2229
    /**
2230
     * Returns an ASCII version of the string. A set of non-ASCII characters are
2231
     * replaced with their closest ASCII counterparts, and the rest are removed
2232
     * unless instructed otherwise.
2233
     *
2234
     * @param bool   $strict  [optional] <p>Use "transliterator_transliterate()" from PHP-Intl | WARNING: bad performance |
2235
     *                        Default: false</p>
2236
     * @param string $unknown [optional] <p>Character use if character unknown. (default is ?)</p>
2237
     *
2238
     * @return static
2239
     *                <p>Object whose $str contains only ASCII characters.</p>
2240
     */
2241 16
    public function toTransliterate(bool $strict = false, string $unknown = '?'): self
2242
    {
2243 16
        return static::create(
2244 16
            $this->ascii::to_transliterate($this->str, $unknown, $strict),
2245 16
            $this->encoding
2246
        );
2247
    }
2248
2249
    /**
2250
     * Returns an ASCII version of the string. A set of non-ASCII characters are
2251
     * replaced with their closest ASCII counterparts, and the rest are removed
2252
     * by default. The language or locale of the source string can be supplied
2253
     * for language-specific transliteration in any of the following formats:
2254
     * en, en_GB, or en-GB. For example, passing "de" results in "äöü" mapping
2255
     * to "aeoeue" rather than "aou" as in other languages.
2256
     *
2257
     * @param string $language          [optional] <p>Language of the source string.</p>
2258
     * @param bool   $removeUnsupported [optional] <p>Whether or not to remove the
2259
     *                                  unsupported characters.</p>
2260
     *
2261
     * @return static
2262
     *                <p>Object whose $str contains only ASCII characters.</p>
2263
     */
2264 21
    public function toAscii(string $language = 'en', bool $removeUnsupported = true): self
2265
    {
2266 21
        return static::create(
2267 21
            $this->ascii::to_ascii(
2268 21
                $this->str,
2269 21
                $language,
2270 21
                $removeUnsupported
2271
            ),
2272 21
            $this->encoding
2273
        );
2274
    }
2275
2276
    /**
2277
     * Returns a boolean representation of the given logical string value.
2278
     * For example, 'true', '1', 'on' and 'yes' will return true. 'false', '0',
2279
     * 'off', and 'no' will return false. In all instances, case is ignored.
2280
     * For other numeric strings, their sign will determine the return value.
2281
     * In addition, blank strings consisting of only whitespace will return
2282
     * false. For all other strings, the return value is a result of a
2283
     * boolean cast.
2284
     *
2285
     * @return bool
2286
     *              <p>A boolean value for the string.</p>
2287
     */
2288 30
    public function toBoolean(): bool
2289
    {
2290 30
        return $this->utf8::to_boolean($this->str);
2291
    }
2292
2293
    /**
2294
     * Converts all characters in the string to lowercase.
2295
     *
2296
     * @param bool        $tryToKeepStringLength [optional] <p>true === try to keep the string length: e.g. ẞ -> ß</p>
2297
     * @param string|null $lang                  [optional] <p>Set the language for special cases: az, el, lt, tr</p>
2298
     *
2299
     * @return static
2300
     *                <p>Object with all characters of $str being lowercase.</p>
2301
     */
2302 12 View Code Duplication
    public function toLowerCase($tryToKeepStringLength = false, $lang = null): self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2303
    {
2304 12
        return static::create(
2305 12
            $this->utf8::strtolower(
2306 12
                $this->str,
2307 12
                $this->encoding,
2308 12
                false,
2309 12
                $lang,
2310 12
                $tryToKeepStringLength
2311
            ),
2312 12
            $this->encoding
2313
        );
2314
    }
2315
2316
    /**
2317
     * Converts each tab in the string to some number of spaces, as defined by
2318
     * $tabLength. By default, each tab is converted to 4 consecutive spaces.
2319
     *
2320
     * @param int $tabLength [optional] <p>Number of spaces to replace each tab with. Default: 4</p>
2321
     *
2322
     * @return static
2323
     *                <p>Object whose $str has had tabs switched to spaces.</p>
2324
     */
2325 12 View Code Duplication
    public function toSpaces(int $tabLength = 4): self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2326
    {
2327 12
        if ($tabLength === 4) {
2328 6
            $tab = '    ';
2329 6
        } elseif ($tabLength === 2) {
2330 2
            $tab = '  ';
2331
        } else {
2332 4
            $tab = \str_repeat(' ', $tabLength);
2333
        }
2334
2335 12
        return static::create(
2336 12
            \str_replace("\t", $tab, $this->str),
2337 12
            $this->encoding
2338
        );
2339
    }
2340
2341
    /**
2342
     * Return Stringy object as string, but you can also use (string) for automatically casting the object into a
2343
     * string.
2344
     *
2345
     * @return string
2346
     */
2347 1077
    public function toString(): string
2348
    {
2349 1077
        return (string) $this->str;
2350
    }
2351
2352
    /**
2353
     * Converts each occurrence of some consecutive number of spaces, as
2354
     * defined by $tabLength, to a tab. By default, each 4 consecutive spaces
2355
     * are converted to a tab.
2356
     *
2357
     * @param int $tabLength [optional] <p>Number of spaces to replace with a tab. Default: 4</p>
2358
     *
2359
     * @return static
2360
     *                <p>Object whose $str has had spaces switched to tabs.</p>
2361
     */
2362 10 View Code Duplication
    public function toTabs(int $tabLength = 4): self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2363
    {
2364 10
        if ($tabLength === 4) {
2365 6
            $tab = '    ';
2366 4
        } elseif ($tabLength === 2) {
2367 2
            $tab = '  ';
2368
        } else {
2369 2
            $tab = \str_repeat(' ', $tabLength);
2370
        }
2371
2372 10
        return static::create(
2373 10
            \str_replace($tab, "\t", $this->str),
2374 10
            $this->encoding
2375
        );
2376
    }
2377
2378
    /**
2379
     * Converts the first character of each word in the string to uppercase
2380
     * and all other chars to lowercase.
2381
     *
2382
     * @return static
2383
     *                <p>Object with all characters of $str being title-cased.</p>
2384
     */
2385 10
    public function toTitleCase(): self
2386
    {
2387 10
        return static::create(
2388 10
            $this->utf8::titlecase($this->str, $this->encoding),
2389 10
            $this->encoding
2390
        );
2391
    }
2392
2393
    /**
2394
     * Converts all characters in the string to uppercase.
2395
     *
2396
     * @param bool        $tryToKeepStringLength [optional] <p>true === try to keep the string length: e.g. ẞ -> ß</p>
2397
     * @param string|null $lang                  [optional] <p>Set the language for special cases: az, el, lt, tr</p>
2398
     *
2399
     * @return static
2400
     *                <p>Object with all characters of $str being uppercase.</p>
2401
     */
2402 11 View Code Duplication
    public function toUpperCase($tryToKeepStringLength = false, $lang = null): self
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
2403
    {
2404 11
        return static::create(
2405 11
            $this->utf8::strtoupper($this->str, $this->encoding, false, $lang, $tryToKeepStringLength),
2406 11
            $this->encoding
2407
        );
2408
    }
2409
2410
    /**
2411
     * Returns a string with whitespace removed from the start and end of the
2412
     * string. Supports the removal of unicode whitespace. Accepts an optional
2413
     * string of characters to strip instead of the defaults.
2414
     *
2415
     * @param string $chars [optional] <p>String of characters to strip. Default: null</p>
2416
     *
2417
     * @return static
2418
     *                <p>Object with a trimmed $str.</p>
2419
     */
2420 24
    public function trim(string $chars = null): self
2421
    {
2422 24
        return static::create(
2423 24
            $this->utf8::trim($this->str, $chars),
2424 24
            $this->encoding
2425
        );
2426
    }
2427
2428
    /**
2429
     * Returns a string with whitespace removed from the start of the string.
2430
     * Supports the removal of unicode whitespace. Accepts an optional
2431
     * string of characters to strip instead of the defaults.
2432
     *
2433
     * @param string $chars [optional] <p>Optional string of characters to strip. Default: null</p>
2434
     *
2435
     * @return static
2436
     *                <p>Object with a trimmed $str.</p>
2437
     */
2438 26
    public function trimLeft(string $chars = null): self
2439
    {
2440 26
        return static::create(
2441 26
            $this->utf8::ltrim($this->str, $chars),
2442 26
            $this->encoding
2443
        );
2444
    }
2445
2446
    /**
2447
     * Returns a string with whitespace removed from the end of the string.
2448
     * Supports the removal of unicode whitespace. Accepts an optional
2449
     * string of characters to strip instead of the defaults.
2450
     *
2451
     * @param string $chars [optional] <p>Optional string of characters to strip. Default: null</p>
2452
     *
2453
     * @return static
2454
     *                <p>Object with a trimmed $str.</p>
2455
     */
2456 26
    public function trimRight(string $chars = null): self
2457
    {
2458 26
        return static::create(
2459 26
            $this->utf8::rtrim($this->str, $chars),
2460 26
            $this->encoding
2461
        );
2462
    }
2463
2464
    /**
2465
     * Truncates the string to a given length. If $substring is provided, and
2466
     * truncating occurs, the string is further truncated so that the substring
2467
     * may be appended without exceeding the desired length.
2468
     *
2469
     * @param int    $length    <p>Desired length of the truncated string.</p>
2470
     * @param string $substring [optional] <p>The substring to append if it can fit. Default: ''</p>
2471
     *
2472
     * @return static
2473
     *                <p>Object with the resulting $str after truncating.</p>
2474
     */
2475 44
    public function truncate(int $length, string $substring = ''): self
2476
    {
2477 44
        return static::create(
2478 44
            $this->utf8::str_truncate($this->str, $length, $substring, $this->encoding),
2479 44
            $this->encoding
2480
        );
2481
    }
2482
2483
    /**
2484
     * Returns a lowercase and trimmed string separated by underscores.
2485
     * Underscores are inserted before uppercase characters (with the exception
2486
     * of the first character of the string), and in place of spaces as well as
2487
     * dashes.
2488
     *
2489
     * @return static
2490
     *                <p>Object with an underscored $str.</p>
2491
     */
2492 32
    public function underscored(): self
2493
    {
2494 32
        return $this->delimit('_');
2495
    }
2496
2497
    /**
2498
     * Returns an UpperCamelCase version of the supplied string. It trims
2499
     * surrounding spaces, capitalizes letters following digits, spaces, dashes
2500
     * and underscores, and removes spaces, dashes, underscores.
2501
     *
2502
     * @return static
2503
     *                <p>Object with $str in UpperCamelCase.</p>
2504
     */
2505 26
    public function upperCamelize(): self
2506
    {
2507 26
        return static::create(
2508 26
            $this->utf8::str_upper_camelize($this->str, $this->encoding),
2509 26
            $this->encoding
2510
        );
2511
    }
2512
2513
    /**
2514
     * Converts the first character of the supplied string to upper case.
2515
     *
2516
     * @return static
2517
     *                <p>Object with the first character of $str being upper case.</p>
2518
     */
2519 12
    public function upperCaseFirst(): self
2520
    {
2521 12
        return static::create($this->utf8::ucfirst($this->str, $this->encoding), $this->encoding);
2522
    }
2523
2524
    /**
2525
     * Converts the string into an valid UTF-8 string.
2526
     *
2527
     * @return static
2528
     */
2529 1
    public function utf8ify(): self
2530
    {
2531 1
        return static::create($this->utf8::cleanup($this->str), $this->encoding);
2532
    }
2533
2534
    /**
2535
     * Returns the replacements for the toAscii() method.
2536
     *
2537
     * @noinspection PhpUnused
2538
     *
2539
     * @return array
2540
     *               <p>An array of replacements.</p>
2541
     *
2542
     * @deprecated this is only here for backward-compatibly reasons
2543
     */
2544 1
    protected function charsArray(): array
2545
    {
2546 1
        return $this->ascii::charsArrayWithMultiLanguageValues();
2547
    }
2548
2549
    /**
2550
     * Returns true if $str matches the supplied pattern, false otherwise.
2551
     *
2552
     * @param string $pattern <p>Regex pattern to match against.</p>
2553
     *
2554
     * @return bool
2555
     *              <p>Whether or not $str matches the pattern.</p>
2556
     */
2557 12
    protected function matchesPattern(string $pattern): bool
2558
    {
2559 12
        return $this->utf8::str_matches_pattern($this->str, $pattern);
2560
    }
2561
}
2562