Completed
Push — master ( 54dd63...b30cd2 )
by Lars
01:50
created

Stringy::containsAny()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 8
ccs 5
cts 5
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 2227
    public function __construct($str = '', string $encoding = null)
55
    {
56 2227
        if (\is_array($str)) {
57 2
            throw new \InvalidArgumentException(
58 2
                'Passed value cannot be an array'
59
            );
60
        }
61
62
        if (
63 2225
            \is_object($str)
64
            &&
65 2225
            !\method_exists($str, '__toString')
66
        ) {
67 2
            throw new \InvalidArgumentException(
68 2
                'Passed object must have a __toString method'
69
            );
70
        }
71
72 2223
        $this->str = (string) $str;
73
74 2223
        static $ASCII = null;
75 2223
        if ($ASCII === null) {
76
            $ASCII = new ASCII();
77
        }
78 2223
        $this->ascii = $ASCII;
79
80 2223
        static $UTF8 = null;
81 2223
        if ($UTF8 === null) {
82
            $UTF8 = new UTF8();
83
        }
84 2223
        $this->utf8 = $UTF8;
85
86 2223
        if ($encoding !== 'UTF-8') {
87 1462
            $this->encoding = $this->utf8::normalize_encoding($encoding, 'UTF-8');
88
        } else {
89 1670
            $this->encoding = $encoding;
90
        }
91 2223
    }
92
93
    /**
94
     * Returns the value in $str.
95
     *
96
     * @return string
97
     *                <p>The current value of the $str property.</p>
98
     */
99 984
    public function __toString()
100
    {
101 984
        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 ...$suffix <p>The string to append.</p>
196
     *
197
     * @return static
198
     *                <p>Object with appended $string.</p>
199
     *
200
     * @noinspection PhpDocSignatureInspection
201
     */
202 8 View Code Duplication
    public function append(string ...$suffix): 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...
203
    {
204 8
        if (\count($suffix) <= 1) {
205
            /** @noinspection CallableParameterUseCaseInTypeContextInspection */
206 8
            $suffix = $suffix[0];
207
        } else {
208
            /** @noinspection CallableParameterUseCaseInTypeContextInspection */
209
            $suffix = \implode('', $suffix);
210
        }
211
212 8
        return static::create($this->str . $suffix, $this->encoding);
213
    }
214
215
    /**
216
     * Append an password (limited to chars that are good readable).
217
     *
218
     * @param int $length <p>Length of the random string.</p>
219
     *
220
     * @return static
221
     *                <p>Object with appended password.</p>
222
     */
223 1
    public function appendPassword(int $length): self
224
    {
225 1
        return $this->appendRandomString(
226 1
            $length,
227 1
            '2346789bcdfghjkmnpqrtvwxyzBCDFGHJKLMNPQRTVWXYZ!?_#'
228
        );
229
    }
230
231
    /**
232
     * Append an random string.
233
     *
234
     * @param int    $length        <p>Length of the random string.</p>
235
     * @param string $possibleChars [optional] <p>Characters string for the random selection.</p>
236
     *
237
     * @return static
238
     *                <p>Object with appended random string.</p>
239
     */
240 2
    public function appendRandomString(int $length, string $possibleChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'): self
241
    {
242 2
        $str = $this->utf8::get_random_string($length, $possibleChars);
243
244 2
        return $this->append($str);
245
    }
246
247
    /**
248
     * Append an unique identifier.
249
     *
250
     * @param int|string $entropyExtra [optional] <p>Extra entropy via a string or int value.</p>
251
     * @param bool       $md5          [optional] <p>Return the unique identifier as md5-hash? Default: true</p>
252
     *
253
     * @return static
254
     *                <p>Object with appended unique identifier as md5-hash.</p>
255
     */
256 1
    public function appendUniqueIdentifier($entropyExtra = '', bool $md5 = true): self
257
    {
258 1
        return $this->append(
259 1
            $this->utf8::get_unique_string($entropyExtra, $md5)
260
        );
261
    }
262
263
    /**
264
     * Returns the character at $index, with indexes starting at 0.
265
     *
266
     * @param int $index <p>Position of the character.</p>
267
     *
268
     * @return static
269
     *                <p>The character at $index.</p>
270
     */
271 16
    public function at(int $index): self
272
    {
273 16
        return static::create($this->utf8::char_at($this->str, $index), $this->encoding);
274
    }
275
276
    /**
277
     * Gets the substring before the first occurrence of a separator.
278
     * If no match is found returns new empty Stringy object.
279
     *
280
     * @param string $separator
281
     *
282
     * @return static
283
     */
284 1
    public function beforeFirst(string $separator): self
285
    {
286 1
        return static::create(
287 1
            $this->utf8::str_substr_before_first_separator(
288 1
                $this->str,
289 1
                $separator,
290 1
                $this->encoding
291
            )
292
        );
293
    }
294
295
    /**
296
     * Gets the substring before the first occurrence of a separator.
297
     * If no match is found returns new empty Stringy object.
298
     *
299
     * @param string $separator
300
     *
301
     * @return static
302
     */
303 1
    public function beforeFirstIgnoreCase(string $separator): self
304
    {
305 1
        return static::create(
306 1
            $this->utf8::str_isubstr_before_first_separator(
307 1
                $this->str,
308 1
                $separator,
309 1
                $this->encoding
310
            )
311
        );
312
    }
313
314
    /**
315
     * Gets the substring before the last occurrence of a separator.
316
     * If no match is found returns new empty Stringy object.
317
     *
318
     * @param string $separator
319
     *
320
     * @return static
321
     */
322 1
    public function beforeLast(string $separator): self
323
    {
324 1
        return static::create(
325 1
            $this->utf8::str_substr_before_last_separator(
326 1
                $this->str,
327 1
                $separator,
328 1
                $this->encoding
329
            )
330
        );
331
    }
332
333
    /**
334
     * Gets the substring before the last occurrence of a separator.
335
     * If no match is found returns new empty Stringy object.
336
     *
337
     * @param string $separator
338
     *
339
     * @return static
340
     */
341 1
    public function beforeLastIgnoreCase(string $separator): self
342
    {
343 1
        return static::create(
344 1
            $this->utf8::str_isubstr_before_last_separator(
345 1
                $this->str,
346 1
                $separator,
347 1
                $this->encoding
348
            )
349
        );
350
    }
351
352
    /**
353
     * Returns the substring between $start and $end, if found, or an empty
354
     * string. An optional offset may be supplied from which to begin the
355
     * search for the start string.
356
     *
357
     * @param string $start  <p>Delimiter marking the start of the substring.</p>
358
     * @param string $end    <p>Delimiter marking the end of the substring.</p>
359
     * @param int    $offset [optional] <p>Index from which to begin the search. Default: 0</p>
360
     *
361
     * @return static
362
     *                <p>Object whose $str is a substring between $start and $end.</p>
363
     */
364 32
    public function between(string $start, string $end, int $offset = null): self
365
    {
366
        /** @noinspection UnnecessaryCastingInspection */
367 32
        $str = $this->utf8::between(
368 32
            $this->str,
369 32
            $start,
370 32
            $end,
371 32
            (int) $offset,
372 32
            $this->encoding
373
        );
374
375 32
        return static::create($str, $this->encoding);
376
    }
377
378
    /**
379
     * Returns a camelCase version of the string. Trims surrounding spaces,
380
     * capitalizes letters following digits, spaces, dashes and underscores,
381
     * and removes spaces, dashes, as well as underscores.
382
     *
383
     * @return static
384
     *                <p>Object with $str in camelCase.</p>
385
     */
386 38
    public function camelize(): self
387
    {
388 38
        return static::create(
389 38
            $this->utf8::str_camelize($this->str, $this->encoding),
390 38
            $this->encoding
391
        );
392
    }
393
394
    /**
395
     * Returns the string with the first letter of each word capitalized,
396
     * except for when the word is a name which shouldn't be capitalized.
397
     *
398
     * @return static
399
     *                <p>Object with $str capitalized.</p>
400
     */
401 39
    public function capitalizePersonalName(): self
402
    {
403 39
        return static::create(
404 39
            $this->utf8::str_capitalize_name($this->str),
405 39
            $this->encoding
406
        );
407
    }
408
409
    /**
410
     * Returns an array consisting of the characters in the string.
411
     *
412
     * @return string[]
413
     *                  <p>An array of string chars.</p>
414
     */
415 8
    public function chars(): array
416
    {
417 8
        return $this->utf8::str_split($this->str);
418
    }
419
420
    /**
421
     * Trims the string and replaces consecutive whitespace characters with a
422
     * single space. This includes tabs and newline characters, as well as
423
     * multibyte whitespace such as the thin space and ideographic space.
424
     *
425
     * @return static
426
     *                <p>Object with a trimmed $str and condensed whitespace.</p>
427
     */
428 26
    public function collapseWhitespace(): self
429
    {
430 26
        return static::create(
431 26
            $this->utf8::collapse_whitespace($this->str),
432 26
            $this->encoding
433
        );
434
    }
435
436
    /**
437
     * Returns true if the string contains $needle, false otherwise. By default
438
     * the comparison is case-sensitive, but can be made insensitive by setting
439
     * $caseSensitive to false.
440
     *
441
     * @param string $needle        <p>Substring to look for.</p>
442
     * @param bool   $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p>
443
     *
444
     * @return bool
445
     *              <p>Whether or not $str contains $needle.</p>
446
     */
447 42
    public function contains(string $needle, bool $caseSensitive = true): bool
448
    {
449 42
        return $this->utf8::str_contains(
450 42
            $this->str,
451 42
            $needle,
452 42
            $caseSensitive
453
        );
454
    }
455
456
    /**
457
     * Returns true if the string contains all $needles, false otherwise. By
458
     * default the comparison is case-sensitive, but can be made insensitive by
459
     * setting $caseSensitive to false.
460
     *
461
     * @param array $needles       <p>SubStrings to look for.</p>
462
     * @param bool  $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p>
463
     *
464
     * @return bool
465
     *              <p>Whether or not $str contains $needle.</p>
466
     */
467 87
    public function containsAll(array $needles, bool $caseSensitive = true): bool
468
    {
469 87
        return $this->utf8::str_contains_all(
470 87
            $this->str,
471 87
            $needles,
472 87
            $caseSensitive
473
        );
474
    }
475
476
    /**
477
     * Returns true if the string contains any $needles, false otherwise. By
478
     * default the comparison is case-sensitive, but can be made insensitive by
479
     * setting $caseSensitive to false.
480
     *
481
     * @param array $needles       <p>SubStrings to look for.</p>
482
     * @param bool  $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p>
483
     *
484
     * @return bool
485
     *              <p>Whether or not $str contains $needle.</p>
486
     */
487 86
    public function containsAny(array $needles, bool $caseSensitive = true): bool
488
    {
489 86
        return $this->utf8::str_contains_any(
490 86
            $this->str,
491 86
            $needles,
492 86
            $caseSensitive
493
        );
494
    }
495
496
    /**
497
     * Returns the length of the string, implementing the countable interface.
498
     *
499
     * @return int
500
     *             <p>The number of characters in the string, given the encoding.</p>
501
     */
502 2
    public function count(): int
503
    {
504 2
        return $this->length();
505
    }
506
507
    /**
508
     * Returns the number of occurrences of $substring in the given string.
509
     * By default, the comparison is case-sensitive, but can be made insensitive
510
     * by setting $caseSensitive to false.
511
     *
512
     * @param string $substring     <p>The substring to search for.</p>
513
     * @param bool   $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p>
514
     *
515
     * @return int
516
     */
517 30
    public function countSubstr(string $substring, bool $caseSensitive = true): int
518
    {
519 30
        return $this->utf8::substr_count_simple(
520 30
            $this->str,
521 30
            $substring,
522 30
            $caseSensitive,
523 30
            $this->encoding
524
        );
525
    }
526
527
    /**
528
     * Creates a Stringy object and assigns both str and encoding properties
529
     * the supplied values. $str is cast to a string prior to assignment, and if
530
     * $encoding is not specified, it defaults to mb_internal_encoding(). It
531
     * then returns the initialized object. Throws an InvalidArgumentException
532
     * if the first argument is an array or object without a __toString method.
533
     *
534
     * @param mixed  $str      [optional] <p>Value to modify, after being cast to string. Default: ''</p>
535
     * @param string $encoding [optional] <p>The character encoding. Fallback: 'UTF-8'</p>
536
     *
537
     * @throws \InvalidArgumentException
538
     *                                   <p>if an array or object without a
539
     *                                   __toString method is passed as the first argument</p>
540
     *
541
     * @return static
542
     *                <p>A Stringy object.</p>
543
     */
544 2205
    public static function create($str = '', string $encoding = null): self
545
    {
546 2205
        return new static($str, $encoding);
547
    }
548
549
    /**
550
     * Returns a lowercase and trimmed string separated by dashes. Dashes are
551
     * inserted before uppercase characters (with the exception of the first
552
     * character of the string), and in place of spaces as well as underscores.
553
     *
554
     * @return static
555
     *                <p>Object with a dasherized $str</p>
556
     */
557 38
    public function dasherize(): self
558
    {
559 38
        return static::create(
560 38
            $this->utf8::str_dasherize($this->str),
561 38
            $this->encoding
562
        );
563
    }
564
565
    /**
566
     * Returns a lowercase and trimmed string separated by the given delimiter.
567
     * Delimiters are inserted before uppercase characters (with the exception
568
     * of the first character of the string), and in place of spaces, dashes,
569
     * and underscores. Alpha delimiters are not converted to lowercase.
570
     *
571
     * @param string $delimiter <p>Sequence used to separate parts of the string.</p>
572
     *
573
     * @return static
574
     *                <p>Object with a delimited $str.</p>
575
     */
576 60
    public function delimit(string $delimiter): self
577
    {
578 60
        return static::create(
579 60
            $this->utf8::str_delimit($this->str, $delimiter),
580 60
            $this->encoding
581
        );
582
    }
583
584
    /**
585
     * Returns true if the string ends with $substring, false otherwise. By
586
     * default, the comparison is case-sensitive, but can be made insensitive
587
     * by setting $caseSensitive to false.
588
     *
589
     * @param string $substring     <p>The substring to look for.</p>
590
     * @param bool   $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p>
591
     *
592
     * @return bool
593
     *              <p>Whether or not $str ends with $substring.</p>
594
     */
595 54
    public function endsWith(string $substring, bool $caseSensitive = true): bool
596
    {
597 54
        if ($caseSensitive) {
598 30
            return $this->utf8::str_ends_with($this->str, $substring);
599
        }
600
601 24
        return $this->utf8::str_iends_with($this->str, $substring);
602
    }
603
604
    /**
605
     * Returns true if the string ends with any of $substrings, false otherwise.
606
     * By default, the comparison is case-sensitive, but can be made insensitive
607
     * by setting $caseSensitive to false.
608
     *
609
     * @param string[] $substrings    <p>Substrings to look for.</p>
610
     * @param bool     $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p>
611
     *
612
     * @return bool
613
     *              <p>Whether or not $str ends with $substring.</p>
614
     */
615 22
    public function endsWithAny(array $substrings, bool $caseSensitive = true): bool
616
    {
617 22
        if ($caseSensitive) {
618 14
            return $this->utf8::str_ends_with_any($this->str, $substrings);
619
        }
620
621 8
        return $this->utf8::str_iends_with_any($this->str, $substrings);
622
    }
623
624
    /**
625
     * Ensures that the string begins with $substring. If it doesn't, it's
626
     * prepended.
627
     *
628
     * @param string $substring <p>The substring to add if not present.</p>
629
     *
630
     * @return static
631
     *                <p>Object with its $str prefixed by the $substring.</p>
632
     */
633 20
    public function ensureLeft(string $substring): self
634
    {
635 20
        return static::create(
636 20
            $this->utf8::str_ensure_left($this->str, $substring),
637 20
            $this->encoding
638
        );
639
    }
640
641
    /**
642
     * Ensures that the string ends with $substring. If it doesn't, it's appended.
643
     *
644
     * @param string $substring <p>The substring to add if not present.</p>
645
     *
646
     * @return static
647
     *                <p>Object with its $str suffixed by the $substring.</p>
648
     */
649 20
    public function ensureRight(string $substring): self
650
    {
651 20
        return static::create(
652 20
            $this->utf8::str_ensure_right($this->str, $substring),
653 20
            $this->encoding
654
        );
655
    }
656
657
    /**
658
     * Create a escape html version of the string via "$this->utf8::htmlspecialchars()".
659
     *
660
     * @return static
661
     */
662 6
    public function escape(): self
663
    {
664 6
        return static::create(
665 6
            $this->utf8::htmlspecialchars(
666 6
                $this->str,
667 6
                \ENT_QUOTES | \ENT_SUBSTITUTE,
668 6
                $this->encoding
669
            ),
670 6
            $this->encoding
671
        );
672
    }
673
674
    /**
675
     * Create an extract from a sentence, so if the search-string was found, it try to centered in the output.
676
     *
677
     * @param string   $search
678
     * @param int|null $length                 [optional] <p>Default: null === text->length / 2</p>
679
     * @param string   $replacerForSkippedText [optional] <p>Default: …</p>
680
     *
681
     * @return static
682
     */
683 1
    public function extractText(string $search = '', int $length = null, string $replacerForSkippedText = '…'): self
684
    {
685 1
        return static::create(
686 1
            $this->utf8::extract_text(
687 1
                $this->str,
688 1
                $search,
689 1
                $length,
690 1
                $replacerForSkippedText,
691 1
                $this->encoding
692
            ),
693 1
            $this->encoding
694
        );
695
    }
696
697
    /**
698
     * Returns the first $n characters of the string.
699
     *
700
     * @param int $n <p>Number of characters to retrieve from the start.</p>
701
     *
702
     * @return static
703
     *                <p>Object with its $str being the first $n chars.</p>
704
     */
705 25
    public function first(int $n): self
706
    {
707 25
        return static::create(
708 25
            $this->utf8::first_char($this->str, $n, $this->encoding),
709 25
            $this->encoding
710
        );
711
    }
712
713
    /**
714
     * Returns the encoding used by the Stringy object.
715
     *
716
     * @return string
717
     *                <p>The current value of the $encoding property.</p>
718
     */
719 5
    public function getEncoding(): string
720
    {
721 5
        return $this->encoding;
722
    }
723
724
    /**
725
     * Returns a new ArrayIterator, thus implementing the IteratorAggregate
726
     * interface. The ArrayIterator's constructor is passed an array of chars
727
     * in the multibyte string. This enables the use of foreach with instances
728
     * of Stringy\Stringy.
729
     *
730
     * @return \ArrayIterator
731
     *                        <p>An iterator for the characters in the string.</p>
732
     */
733 2
    public function getIterator(): \ArrayIterator
734
    {
735 2
        return new \ArrayIterator($this->chars());
736
    }
737
738
    /**
739
     * Returns true if the string contains a lower case char, false otherwise.
740
     *
741
     * @return bool
742
     *              <p>Whether or not the string contains a lower case character.</p>
743
     */
744 24
    public function hasLowerCase(): bool
745
    {
746 24
        return $this->utf8::has_lowercase($this->str);
747
    }
748
749
    /**
750
     * Returns true if the string contains an upper case char, false otherwise.
751
     *
752
     * @return bool
753
     *              <p>Whether or not the string contains an upper case character.</p>
754
     */
755 24
    public function hasUpperCase(): bool
756
    {
757 24
        return $this->utf8::has_uppercase($this->str);
758
    }
759
760
    /**
761
     * Convert all HTML entities to their applicable characters.
762
     *
763
     * @param int $flags [optional] <p>
764
     *                   A bitmask of one or more of the following flags, which specify how to handle quotes and
765
     *                   which document type to use. The default is ENT_COMPAT.
766
     *                   <table>
767
     *                   Available <i>flags</i> constants
768
     *                   <tr valign="top">
769
     *                   <td>Constant Name</td>
770
     *                   <td>Description</td>
771
     *                   </tr>
772
     *                   <tr valign="top">
773
     *                   <td><b>ENT_COMPAT</b></td>
774
     *                   <td>Will convert double-quotes and leave single-quotes alone.</td>
775
     *                   </tr>
776
     *                   <tr valign="top">
777
     *                   <td><b>ENT_QUOTES</b></td>
778
     *                   <td>Will convert both double and single quotes.</td>
779
     *                   </tr>
780
     *                   <tr valign="top">
781
     *                   <td><b>ENT_NOQUOTES</b></td>
782
     *                   <td>Will leave both double and single quotes unconverted.</td>
783
     *                   </tr>
784
     *                   <tr valign="top">
785
     *                   <td><b>ENT_HTML401</b></td>
786
     *                   <td>
787
     *                   Handle code as HTML 4.01.
788
     *                   </td>
789
     *                   </tr>
790
     *                   <tr valign="top">
791
     *                   <td><b>ENT_XML1</b></td>
792
     *                   <td>
793
     *                   Handle code as XML 1.
794
     *                   </td>
795
     *                   </tr>
796
     *                   <tr valign="top">
797
     *                   <td><b>ENT_XHTML</b></td>
798
     *                   <td>
799
     *                   Handle code as XHTML.
800
     *                   </td>
801
     *                   </tr>
802
     *                   <tr valign="top">
803
     *                   <td><b>ENT_HTML5</b></td>
804
     *                   <td>
805
     *                   Handle code as HTML 5.
806
     *                   </td>
807
     *                   </tr>
808
     *                   </table>
809
     *                   </p>
810
     *
811
     * @return static
812
     *                <p>Object with the resulting $str after being html decoded.</p>
813
     */
814 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...
815
    {
816 10
        return static::create(
817 10
            $this->utf8::html_entity_decode(
818 10
                $this->str,
819 10
                $flags,
820 10
                $this->encoding
821
            ),
822 10
            $this->encoding
823
        );
824
    }
825
826
    /**
827
     * Convert all applicable characters to HTML entities.
828
     *
829
     * @param int $flags [optional] <p>
830
     *                   A bitmask of one or more of the following flags, which specify how to handle quotes and
831
     *                   which document type to use. The default is ENT_COMPAT.
832
     *                   <table>
833
     *                   Available <i>flags</i> constants
834
     *                   <tr valign="top">
835
     *                   <td>Constant Name</td>
836
     *                   <td>Description</td>
837
     *                   </tr>
838
     *                   <tr valign="top">
839
     *                   <td><b>ENT_COMPAT</b></td>
840
     *                   <td>Will convert double-quotes and leave single-quotes alone.</td>
841
     *                   </tr>
842
     *                   <tr valign="top">
843
     *                   <td><b>ENT_QUOTES</b></td>
844
     *                   <td>Will convert both double and single quotes.</td>
845
     *                   </tr>
846
     *                   <tr valign="top">
847
     *                   <td><b>ENT_NOQUOTES</b></td>
848
     *                   <td>Will leave both double and single quotes unconverted.</td>
849
     *                   </tr>
850
     *                   <tr valign="top">
851
     *                   <td><b>ENT_HTML401</b></td>
852
     *                   <td>
853
     *                   Handle code as HTML 4.01.
854
     *                   </td>
855
     *                   </tr>
856
     *                   <tr valign="top">
857
     *                   <td><b>ENT_XML1</b></td>
858
     *                   <td>
859
     *                   Handle code as XML 1.
860
     *                   </td>
861
     *                   </tr>
862
     *                   <tr valign="top">
863
     *                   <td><b>ENT_XHTML</b></td>
864
     *                   <td>
865
     *                   Handle code as XHTML.
866
     *                   </td>
867
     *                   </tr>
868
     *                   <tr valign="top">
869
     *                   <td><b>ENT_HTML5</b></td>
870
     *                   <td>
871
     *                   Handle code as HTML 5.
872
     *                   </td>
873
     *                   </tr>
874
     *                   </table>
875
     *                   </p>
876
     *
877
     * @return static
878
     *                <p>Object with the resulting $str after being html encoded.</p>
879
     */
880 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...
881
    {
882 10
        return static::create(
883 10
            $this->utf8::htmlentities(
884 10
                $this->str,
885 10
                $flags,
886 10
                $this->encoding
887
            ),
888 10
            $this->encoding
889
        );
890
    }
891
892
    /**
893
     * Capitalizes the first word of the string, replaces underscores with
894
     * spaces, and strips '_id'.
895
     *
896
     * @return static
897
     *                <p>Object with a humanized $str.</p>
898
     */
899 6
    public function humanize(): self
900
    {
901 6
        return static::create(
902 6
            $this->utf8::str_humanize($this->str),
903 6
            $this->encoding
904
        );
905
    }
906
907
    /**
908
     * Returns the index of the first occurrence of $needle in the string,
909
     * and false if not found. Accepts an optional offset from which to begin
910
     * the search.
911
     *
912
     * @param string $needle <p>Substring to look for.</p>
913
     * @param int    $offset [optional] <p>Offset from which to search. Default: 0</p>
914
     *
915
     * @return false|int
916
     *                   <p>The occurrence's <strong>index</strong> if found, otherwise <strong>false</strong>.</p>
917
     */
918 20
    public function indexOf(string $needle, int $offset = 0)
919
    {
920 20
        return $this->utf8::strpos(
921 20
            $this->str,
922 20
            $needle,
923 20
            $offset,
924 20
            $this->encoding
925
        );
926
    }
927
928
    /**
929
     * Returns the index of the first occurrence of $needle in the string,
930
     * and false if not found. Accepts an optional offset from which to begin
931
     * the search.
932
     *
933
     * @param string $needle <p>Substring to look for.</p>
934
     * @param int    $offset [optional] <p>Offset from which to search. Default: 0</p>
935
     *
936
     * @return false|int
937
     *                   <p>The occurrence's <strong>index</strong> if found, otherwise <strong>false</strong>.</p>
938
     */
939 10
    public function indexOfIgnoreCase(string $needle, int $offset = 0)
940
    {
941 10
        return $this->utf8::stripos(
942 10
            $this->str,
943 10
            $needle,
944 10
            $offset,
945 10
            $this->encoding
946
        );
947
    }
948
949
    /**
950
     * Returns the index of the last occurrence of $needle in the string,
951
     * and false if not found. Accepts an optional offset from which to begin
952
     * the search. Offsets may be negative to count from the last character
953
     * in the string.
954
     *
955
     * @param string $needle <p>Substring to look for.</p>
956
     * @param int    $offset [optional] <p>Offset from which to search. Default: 0</p>
957
     *
958
     * @return false|int
959
     *                   <p>The last occurrence's <strong>index</strong> if found, otherwise <strong>false</strong>.</p>
960
     */
961 20
    public function indexOfLast(string $needle, int $offset = 0)
962
    {
963 20
        return $this->utf8::strrpos(
964 20
            $this->str,
965 20
            $needle,
966 20
            $offset,
967 20
            $this->encoding
968
        );
969
    }
970
971
    /**
972
     * Returns the index of the last occurrence of $needle in the string,
973
     * and false if not found. Accepts an optional offset from which to begin
974
     * the search. Offsets may be negative to count from the last character
975
     * in the string.
976
     *
977
     * @param string $needle <p>Substring to look for.</p>
978
     * @param int    $offset [optional] <p>Offset from which to search. Default: 0</p>
979
     *
980
     * @return false|int
981
     *                   <p>The last occurrence's <strong>index</strong> if found, otherwise <strong>false</strong>.</p>
982
     */
983 10
    public function indexOfLastIgnoreCase(string $needle, int $offset = 0)
984
    {
985 10
        return $this->utf8::strripos(
986 10
            $this->str,
987 10
            $needle,
988 10
            $offset,
989 10
            $this->encoding
990
        );
991
    }
992
993
    /**
994
     * Inserts $substring into the string at the $index provided.
995
     *
996
     * @param string $substring <p>String to be inserted.</p>
997
     * @param int    $index     <p>The index at which to insert the substring.</p>
998
     *
999
     * @return static
1000
     *                <p>Object with the resulting $str after the insertion.</p>
1001
     */
1002 16
    public function insert(string $substring, int $index): self
1003
    {
1004 16
        return static::create(
1005 16
            $this->utf8::str_insert(
1006 16
                $this->str,
1007 16
                $substring,
1008 16
                $index,
1009 16
                $this->encoding
1010
            ),
1011 16
            $this->encoding
1012
        );
1013
    }
1014
1015
    /**
1016
     * Returns true if the string contains the $pattern, otherwise false.
1017
     *
1018
     * WARNING: Asterisks ("*") are translated into (".*") zero-or-more regular
1019
     * expression wildcards.
1020
     *
1021
     * @credit Originally from Laravel, thanks Taylor.
1022
     *
1023
     * @param string $pattern <p>The string or pattern to match against.</p>
1024
     *
1025
     * @return bool
1026
     *              <p>Whether or not we match the provided pattern.</p>
1027
     */
1028 13
    public function is(string $pattern): bool
1029
    {
1030 13
        if ($this->toString() === $pattern) {
1031 1
            return true;
1032
        }
1033
1034 12
        $quotedPattern = \preg_quote($pattern, '/');
1035 12
        $replaceWildCards = \str_replace('\*', '.*', $quotedPattern);
1036
1037 12
        return $this->matchesPattern('^' . $replaceWildCards . '\z');
1038
    }
1039
1040
    /**
1041
     * Returns true if the string contains only alphabetic chars, false otherwise.
1042
     *
1043
     * @return bool
1044
     *              <p>Whether or not $str contains only alphabetic chars.</p>
1045
     */
1046 20
    public function isAlpha(): bool
1047
    {
1048 20
        return $this->utf8::is_alpha($this->str);
1049
    }
1050
1051
    /**
1052
     * Returns true if the string contains only alphabetic and numeric chars, false otherwise.
1053
     *
1054
     * @return bool
1055
     *              <p>Whether or not $str contains only alphanumeric chars.</p>
1056
     */
1057 26
    public function isAlphanumeric(): bool
1058
    {
1059 26
        return $this->utf8::is_alphanumeric($this->str);
1060
    }
1061
1062
    /**
1063
     * Returns true if the string is base64 encoded, false otherwise.
1064
     *
1065
     * @param bool $emptyStringIsValid
1066
     *
1067
     * @return bool
1068
     *              <p>Whether or not $str is base64 encoded.</p>
1069
     */
1070 14
    public function isBase64($emptyStringIsValid = true): bool
1071
    {
1072 14
        return $this->utf8::is_base64($this->str, $emptyStringIsValid);
1073
    }
1074
1075
    /**
1076
     * Returns true if the string contains only whitespace chars, false otherwise.
1077
     *
1078
     * @return bool
1079
     *              <p>Whether or not $str contains only whitespace characters.</p>
1080
     */
1081 30
    public function isBlank(): bool
1082
    {
1083 30
        return $this->utf8::is_blank($this->str);
1084
    }
1085
1086
    /**
1087
     * Returns true if the string contains a valid E-Mail address, false otherwise.
1088
     *
1089
     * @param bool $useExampleDomainCheck   [optional] <p>Default: false</p>
1090
     * @param bool $useTypoInDomainCheck    [optional] <p>Default: false</p>
1091
     * @param bool $useTemporaryDomainCheck [optional] <p>Default: false</p>
1092
     * @param bool $useDnsCheck             [optional] <p>Default: false</p>
1093
     *
1094
     * @return bool
1095
     *              <p>Whether or not $str contains a valid E-Mail address.</p>
1096
     */
1097 1
    public function isEmail(bool $useExampleDomainCheck = false, bool $useTypoInDomainCheck = false, bool $useTemporaryDomainCheck = false, bool $useDnsCheck = false): bool
1098
    {
1099 1
        return EmailCheck::isValid($this->str, $useExampleDomainCheck, $useTypoInDomainCheck, $useTemporaryDomainCheck, $useDnsCheck);
1100
    }
1101
1102
    /**
1103
     * Determine whether the string is considered to be empty.
1104
     *
1105
     * A variable is considered empty if it does not exist or if its value equals FALSE.
1106
     *
1107
     * @return bool
1108
     *              <p>Whether or not $str is empty().</p>
1109
     */
1110 5
    public function isEmpty(): bool
1111
    {
1112 5
        return $this->utf8::is_empty($this->str);
1113
    }
1114
1115
    /**
1116
     * Determine whether the string is considered to be NOT empty.
1117
     *
1118
     * A variable is considered NOT empty if it does exist or if its value equals TRUE.
1119
     *
1120
     * @return bool
1121
     *              <p>Whether or not $str is empty().</p>
1122
     */
1123 5
    public function isNotEmpty(): bool
1124
    {
1125 5
        return !$this->utf8::is_empty($this->str);
1126
    }
1127
1128
    /**
1129
     * Determine whether the string is equals to $str.
1130
     *
1131
     * @param string ...$str <p>The string to compare.</p>
1132
     *
1133
     * @return bool
1134
     *              <p>Whether or not $str is equals.</p>
1135
     *
1136
     * @noinspection PhpDocSignatureInspection
1137
     */
1138 7
    public function isEquals(string ...$str): bool
1139
    {
1140 7
        foreach ($str as $strTmp) {
1141 7
            if ($this->str !== $strTmp) {
1142 7
                return false;
1143
            }
1144
        }
1145
1146 1
        return true;
1147
    }
1148
1149
    /**
1150
     * Returns true if the string contains only hexadecimal chars, false otherwise.
1151
     *
1152
     * @return bool
1153
     *              <p>Whether or not $str contains only hexadecimal chars.</p>
1154
     */
1155 26
    public function isHexadecimal(): bool
1156
    {
1157 26
        return $this->utf8::is_hexadecimal($this->str);
1158
    }
1159
1160
    /**
1161
     * Returns true if the string contains HTML-Tags, false otherwise.
1162
     *
1163
     * @return bool
1164
     *              <p>Whether or not $str contains HTML-Tags.</p>
1165
     */
1166 1
    public function isHtml(): bool
1167
    {
1168 1
        return $this->utf8::is_html($this->str);
1169
    }
1170
1171
    /**
1172
     * Returns true if the string is JSON, false otherwise. Unlike json_decode
1173
     * in PHP 5.x, this method is consistent with PHP 7 and other JSON parsers,
1174
     * in that an empty string is not considered valid JSON.
1175
     *
1176
     * @param bool $onlyArrayOrObjectResultsAreValid
1177
     *
1178
     * @return bool
1179
     *              <p>Whether or not $str is JSON.</p>
1180
     */
1181 40
    public function isJson($onlyArrayOrObjectResultsAreValid = false): bool
1182
    {
1183 40
        return $this->utf8::is_json($this->str, $onlyArrayOrObjectResultsAreValid);
1184
    }
1185
1186
    /**
1187
     * Returns true if the string contains only lower case chars, false otherwise.
1188
     *
1189
     * @return bool
1190
     *              <p>Whether or not $str contains only lower case characters.</p>
1191
     */
1192 16
    public function isLowerCase(): bool
1193
    {
1194 16
        return $this->utf8::is_lowercase($this->str);
1195
    }
1196
1197
    /**
1198
     * Returns true if the string is serialized, false otherwise.
1199
     *
1200
     * @return bool
1201
     *              <p>Whether or not $str is serialized.</p>
1202
     */
1203 14
    public function isSerialized(): bool
1204
    {
1205 14
        return $this->utf8::is_serialized($this->str);
1206
    }
1207
1208
    /**
1209
     * Returns true if the string contains only lower case chars, false
1210
     * otherwise.
1211
     *
1212
     * @return bool
1213
     *              <p>Whether or not $str contains only lower case characters.</p>
1214
     */
1215 16
    public function isUpperCase(): bool
1216
    {
1217 16
        return $this->utf8::is_uppercase($this->str);
1218
    }
1219
1220
    /**
1221
     * Returns the last $n characters of the string.
1222
     *
1223
     * @param int $n <p>Number of characters to retrieve from the end.</p>
1224
     *
1225
     * @return static
1226
     *                <p>Object with its $str being the last $n chars.</p>
1227
     */
1228 24
    public function last(int $n): self
1229
    {
1230 24
        return static::create(
1231 24
            $this->utf8::str_last_char(
1232 24
                $this->str,
1233 24
                $n,
1234 24
                $this->encoding
1235
            ),
1236 24
            $this->encoding
1237
        );
1238
    }
1239
1240
    /**
1241
     * Gets the substring after (or before via "$beforeNeedle") the last occurrence of the "$needle".
1242
     * If no match is found returns new empty Stringy object.
1243
     *
1244
     * @param string $needle       <p>The string to look for.</p>
1245
     * @param bool   $beforeNeedle [optional] <p>Default: false</p>
1246
     *
1247
     * @return static
1248
     */
1249 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...
1250
    {
1251 2
        return static::create(
1252 2
            $this->utf8::str_substr_last(
1253 2
                $this->str,
1254 2
                $needle,
1255 2
                $beforeNeedle,
1256 2
                $this->encoding
1257
            ),
1258 2
            $this->encoding
1259
        );
1260
    }
1261
1262
    /**
1263
     * Gets the substring after (or before via "$beforeNeedle") the last occurrence of the "$needle".
1264
     * If no match is found returns new empty Stringy object.
1265
     *
1266
     * @param string $needle       <p>The string to look for.</p>
1267
     * @param bool   $beforeNeedle [optional] <p>Default: false</p>
1268
     *
1269
     * @return static
1270
     */
1271 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...
1272
    {
1273 1
        return static::create(
1274 1
            $this->utf8::str_isubstr_last(
1275 1
                $this->str,
1276 1
                $needle,
1277 1
                $beforeNeedle,
1278 1
                $this->encoding
1279
            ),
1280 1
            $this->encoding
1281
        );
1282
    }
1283
1284
    /**
1285
     * Returns the length of the string.
1286
     *
1287
     * @return int
1288
     *             <p>The number of characters in $str given the encoding.</p>
1289
     */
1290 11
    public function length(): int
1291
    {
1292 11
        return (int) $this->utf8::strlen($this->str, $this->encoding);
1293
    }
1294
1295
    /**
1296
     * Line-Wrap the string after $limit, but also after the next word.
1297
     *
1298
     * @param int $limit
1299
     *
1300
     * @return static
1301
     */
1302 1
    public function lineWrapAfterWord(int $limit): self
1303
    {
1304 1
        return static::create(
1305 1
            $this->utf8::wordwrap_per_line($this->str, $limit),
1306 1
            $this->encoding
1307
        );
1308
    }
1309
1310
    /**
1311
     * Splits on newlines and carriage returns, returning an array of Stringy
1312
     * objects corresponding to the lines in the string.
1313
     *
1314
     * @return static[]
1315
     *                  <p>An array of Stringy objects.</p>
1316
     */
1317 34
    public function lines(): array
1318
    {
1319 34
        $array = $this->utf8::str_to_lines($this->str);
1320 34
        foreach ($array as $i => &$value) {
1321 34
            $value = static::create($value, $this->encoding);
1322
        }
1323
1324 34
        return $array;
1325
    }
1326
1327
    /**
1328
     * Returns the longest common prefix between the string and $otherStr.
1329
     *
1330
     * @param string $otherStr <p>Second string for comparison.</p>
1331
     *
1332
     * @return static
1333
     *                <p>Object with its $str being the longest common prefix.</p>
1334
     */
1335 20
    public function longestCommonPrefix(string $otherStr): self
1336
    {
1337 20
        return static::create(
1338 20
            $this->utf8::str_longest_common_prefix(
1339 20
                $this->str,
1340 20
                $otherStr,
1341 20
                $this->encoding
1342
            ),
1343 20
            $this->encoding
1344
        );
1345
    }
1346
1347
    /**
1348
     * Returns the longest common substring between the string and $otherStr.
1349
     * In the case of ties, it returns that which occurs first.
1350
     *
1351
     * @param string $otherStr <p>Second string for comparison.</p>
1352
     *
1353
     * @return static
1354
     *                <p>Object with its $str being the longest common substring.</p>
1355
     */
1356 20
    public function longestCommonSubstring(string $otherStr): self
1357
    {
1358 20
        return static::create(
1359 20
            $this->utf8::str_longest_common_substring(
1360 20
                $this->str,
1361 20
                $otherStr,
1362 20
                $this->encoding
1363
            ),
1364 20
            $this->encoding
1365
        );
1366
    }
1367
1368
    /**
1369
     * Returns the longest common suffix between the string and $otherStr.
1370
     *
1371
     * @param string $otherStr <p>Second string for comparison.</p>
1372
     *
1373
     * @return static
1374
     *                <p>Object with its $str being the longest common suffix.</p>
1375
     */
1376 20
    public function longestCommonSuffix(string $otherStr): self
1377
    {
1378 20
        return static::create(
1379 20
            $this->utf8::str_longest_common_suffix(
1380 20
                $this->str,
1381 20
                $otherStr,
1382 20
                $this->encoding
1383
            ),
1384 20
            $this->encoding
1385
        );
1386
    }
1387
1388
    /**
1389
     * Converts the first character of the string to lower case.
1390
     *
1391
     * @return static
1392
     *                <p>Object with the first character of $str being lower case.</p>
1393
     */
1394 10
    public function lowerCaseFirst(): self
1395
    {
1396 10
        return static::create(
1397 10
            $this->utf8::lcfirst($this->str, $this->encoding),
1398 10
            $this->encoding
1399
        );
1400
    }
1401
1402
    /**
1403
     * Returns whether or not a character exists at an index. Offsets may be
1404
     * negative to count from the last character in the string. Implements
1405
     * part of the ArrayAccess interface.
1406
     *
1407
     * @param int $offset <p>The index to check.</p>
1408
     *
1409
     * @return bool
1410
     *              <p>Whether or not the index exists.</p>
1411
     */
1412 12
    public function offsetExists($offset): bool
1413
    {
1414 12
        return $this->utf8::str_offset_exists(
1415 12
            $this->str,
1416 12
            $offset,
1417 12
            $this->encoding
1418
        );
1419
    }
1420
1421
    /**
1422
     * Returns the character at the given index. Offsets may be negative to
1423
     * count from the last character in the string. Implements part of the
1424
     * ArrayAccess interface, and throws an OutOfBoundsException if the index
1425
     * does not exist.
1426
     *
1427
     * @param int $offset <p>The <strong>index</strong> from which to retrieve the char.</p>
1428
     *
1429
     * @throws \OutOfBoundsException
1430
     *                               <p>If the positive or negative offset does not exist.</p>
1431
     *
1432
     * @return string
1433
     *                <p>The character at the specified index.</p>
1434
     */
1435 4
    public function offsetGet($offset): string
1436
    {
1437 4
        return $this->utf8::str_offset_get($this->str, $offset, $this->encoding);
1438
    }
1439
1440
    /**
1441
     * Implements part of the ArrayAccess interface, but throws an exception
1442
     * when called. This maintains the immutability of Stringy objects.
1443
     *
1444
     * @param int   $offset <p>The index of the character.</p>
1445
     * @param mixed $value  <p>Value to set.</p>
1446
     *
1447
     * @throws \Exception
1448
     *                    <p>When called.</p>
1449
     */
1450 2
    public function offsetSet($offset, $value)
1451
    {
1452
        // Stringy is immutable, cannot directly set char
1453
        /** @noinspection ThrowRawExceptionInspection */
1454 2
        throw new \Exception('Stringy object is immutable, cannot modify char');
1455
    }
1456
1457
    /**
1458
     * Implements part of the ArrayAccess interface, but throws an exception
1459
     * when called. This maintains the immutability of Stringy objects.
1460
     *
1461
     * @param int $offset <p>The index of the character.</p>
1462
     *
1463
     * @throws \Exception
1464
     *                    <p>When called.</p>
1465
     */
1466 2
    public function offsetUnset($offset)
1467
    {
1468
        // Don't allow directly modifying the string
1469
        /** @noinspection ThrowRawExceptionInspection */
1470 2
        throw new \Exception('Stringy object is immutable, cannot unset char');
1471
    }
1472
1473
    /**
1474
     * Pads the string to a given length with $padStr. If length is less than
1475
     * or equal to the length of the string, no padding takes places. The
1476
     * default string used for padding is a space, and the default type (one of
1477
     * 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException
1478
     * if $padType isn't one of those 3 values.
1479
     *
1480
     * @param int    $length  <p>Desired string length after padding.</p>
1481
     * @param string $padStr  [optional] <p>String used to pad, defaults to space. Default: ' '</p>
1482
     * @param string $padType [optional] <p>One of 'left', 'right', 'both'. Default: 'right'</p>
1483
     *
1484
     * @throws \InvalidArgumentException
1485
     *                                   <p>If $padType isn't one of 'right', 'left' or 'both'.</p>
1486
     *
1487
     * @return static
1488
     *                <p>Object with a padded $str.</p>
1489
     */
1490 26
    public function pad(int $length, string $padStr = ' ', string $padType = 'right'): self
1491
    {
1492 26
        return static::create(
1493 26
            $this->utf8::str_pad(
1494 26
                $this->str,
1495 26
                $length,
1496 26
                $padStr,
1497 26
                $padType,
1498 26
                $this->encoding
1499
            )
1500
        );
1501
    }
1502
1503
    /**
1504
     * Returns a new string of a given length such that both sides of the
1505
     * string are padded. Alias for pad() with a $padType of 'both'.
1506
     *
1507
     * @param int    $length <p>Desired string length after padding.</p>
1508
     * @param string $padStr [optional] <p>String used to pad, defaults to space. Default: ' '</p>
1509
     *
1510
     * @return static
1511
     *                <p>String with padding applied.</p>
1512
     */
1513 22
    public function padBoth(int $length, string $padStr = ' '): self
1514
    {
1515 22
        return static::create(
1516 22
            $this->utf8::str_pad_both(
1517 22
                $this->str,
1518 22
                $length,
1519 22
                $padStr,
1520 22
                $this->encoding
1521
            )
1522
        );
1523
    }
1524
1525
    /**
1526
     * Returns a new string of a given length such that the beginning of the
1527
     * string is padded. Alias for pad() with a $padType of 'left'.
1528
     *
1529
     * @param int    $length <p>Desired string length after padding.</p>
1530
     * @param string $padStr [optional] <p>String used to pad, defaults to space. Default: ' '</p>
1531
     *
1532
     * @return static
1533
     *                <p>String with left padding.</p>
1534
     */
1535 14
    public function padLeft(int $length, string $padStr = ' '): self
1536
    {
1537 14
        return static::create(
1538 14
            $this->utf8::str_pad_left(
1539 14
                $this->str,
1540 14
                $length,
1541 14
                $padStr,
1542 14
                $this->encoding
1543
            )
1544
        );
1545
    }
1546
1547
    /**
1548
     * Returns a new string of a given length such that the end of the string
1549
     * is padded. Alias for pad() with a $padType of 'right'.
1550
     *
1551
     * @param int    $length <p>Desired string length after padding.</p>
1552
     * @param string $padStr [optional] <p>String used to pad, defaults to space. Default: ' '</p>
1553
     *
1554
     * @return static
1555
     *                <p>String with right padding.</p>
1556
     */
1557 14
    public function padRight(int $length, string $padStr = ' '): self
1558
    {
1559 14
        return static::create(
1560 14
            $this->utf8::str_pad_right(
1561 14
                $this->str,
1562 14
                $length,
1563 14
                $padStr,
1564 14
                $this->encoding
1565
            )
1566
        );
1567
    }
1568
1569
    /**
1570
     * Returns a new string starting with $string.
1571
     *
1572
     * @param string ...$prefix <p>The string to append.</p>
1573
     *
1574
     * @return static
1575
     *                <p>Object with appended $string.</p>
1576
     *
1577
     * @noinspection PhpDocSignatureInspection
1578
     */
1579 4 View Code Duplication
    public function prepend(string ...$prefix): 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...
1580
    {
1581 4
        if (\count($prefix) <= 1) {
1582
            /** @noinspection CallableParameterUseCaseInTypeContextInspection */
1583 4
            $prefix = $prefix[0];
1584
        } else {
1585
            /** @noinspection CallableParameterUseCaseInTypeContextInspection */
1586
            $prefix = \implode('', ...$prefix);
1587
        }
1588
1589 4
        return static::create($prefix . $this->str, $this->encoding);
1590
    }
1591
1592
    /**
1593
     * Replaces all occurrences of $pattern in $str by $replacement.
1594
     *
1595
     * @param string $pattern     <p>The regular expression pattern.</p>
1596
     * @param string $replacement <p>The string to replace with.</p>
1597
     * @param string $options     [optional] <p>Matching conditions to be used.</p>
1598
     * @param string $delimiter   [optional] <p>Delimiter the the regex. Default: '/'</p>
1599
     *
1600
     * @return static
1601
     *                <p>Object with the result2ing $str after the replacements.</p>
1602
     */
1603 19
    public function regexReplace(string $pattern, string $replacement, string $options = '', string $delimiter = '/'): self
1604
    {
1605 19
        return static::create(
1606 19
            $this->utf8::regex_replace(
1607 19
                $this->str,
1608 19
                $pattern,
1609 19
                $replacement,
1610 19
                $options,
1611 19
                $delimiter
1612
            ),
1613 19
            $this->encoding
1614
        );
1615
    }
1616
1617
    /**
1618
     * Remove html via "strip_tags()" from the string.
1619
     *
1620
     * @param string $allowableTags [optional] <p>You can use the optional second parameter to specify tags which should
1621
     *                              not be stripped. Default: null
1622
     *                              </p>
1623
     *
1624
     * @return static
1625
     */
1626 6
    public function removeHtml(string $allowableTags = ''): self
1627
    {
1628 6
        return static::create(
1629 6
            $this->utf8::remove_html($this->str, $allowableTags),
1630 6
            $this->encoding
1631
        );
1632
    }
1633
1634
    /**
1635
     * Remove all breaks [<br> | \r\n | \r | \n | ...] from the string.
1636
     *
1637
     * @param string $replacement [optional] <p>Default is a empty string.</p>
1638
     *
1639
     * @return static
1640
     */
1641 6
    public function removeHtmlBreak(string $replacement = ''): self
1642
    {
1643 6
        return static::create(
1644 6
            $this->utf8::remove_html_breaks($this->str, $replacement),
1645 6
            $this->encoding
1646
        );
1647
    }
1648
1649
    /**
1650
     * Returns a new string with the prefix $substring removed, if present.
1651
     *
1652
     * @param string $substring <p>The prefix to remove.</p>
1653
     *
1654
     * @return static
1655
     *                <p>Object having a $str without the prefix $substring.</p>
1656
     */
1657 24
    public function removeLeft(string $substring): self
1658
    {
1659 24
        return static::create(
1660 24
            $this->utf8::remove_left($this->str, $substring, $this->encoding),
1661 24
            $this->encoding
1662
        );
1663
    }
1664
1665
    /**
1666
     * Returns a new string with the suffix $substring removed, if present.
1667
     *
1668
     * @param string $substring <p>The suffix to remove.</p>
1669
     *
1670
     * @return static
1671
     *                <p>Object having a $str without the suffix $substring.</p>
1672
     */
1673 24
    public function removeRight(string $substring): self
1674
    {
1675 24
        return static::create(
1676 24
            $this->utf8::remove_right($this->str, $substring, $this->encoding),
1677 24
            $this->encoding
1678
        );
1679
    }
1680
1681
    /**
1682
     * Try to remove all XSS-attacks from the string.
1683
     *
1684
     * @return static
1685
     */
1686 6
    public function removeXss(): self
1687
    {
1688 6
        static $antiXss = null;
1689
1690 6
        if ($antiXss === null) {
1691 1
            $antiXss = new AntiXSS();
1692
        }
1693
1694 6
        $str = $antiXss->xss_clean($this->str);
1695
1696 6
        return static::create($str, $this->encoding);
1697
    }
1698
1699
    /**
1700
     * Returns a repeated string given a multiplier.
1701
     *
1702
     * @param int $multiplier <p>The number of times to repeat the string.</p>
1703
     *
1704
     * @return static
1705
     *                <p>Object with a repeated str.</p>
1706
     */
1707 14
    public function repeat(int $multiplier): self
1708
    {
1709 14
        return static::create(
1710 14
            \str_repeat($this->str, $multiplier),
1711 14
            $this->encoding
1712
        );
1713
    }
1714
1715
    /**
1716
     * Replaces all occurrences of $search in $str by $replacement.
1717
     *
1718
     * @param string $search        <p>The needle to search for.</p>
1719
     * @param string $replacement   <p>The string to replace with.</p>
1720
     * @param bool   $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p>
1721
     *
1722
     * @return static
1723
     *                <p>Object with the resulting $str after the replacements.</p>
1724
     */
1725 45
    public function replace(string $search, string $replacement, bool $caseSensitive = true): self
1726
    {
1727 45
        if ($search === '' && $replacement === '') {
1728 10
            return static::create($this->str, $this->encoding);
1729
        }
1730
1731 35
        if ($this->str === '' && $search === '') {
1732 2
            return static::create($replacement, $this->encoding);
1733
        }
1734
1735 33
        if ($caseSensitive) {
1736 28
            return static::create(
1737 28
                $this->utf8::str_replace($search, $replacement, $this->str),
1738 28
                $this->encoding
1739
            );
1740
        }
1741
1742 5
        return static::create(
1743 5
            $this->utf8::str_ireplace($search, $replacement, $this->str),
1744 5
            $this->encoding
1745
        );
1746
    }
1747
1748
    /**
1749
     * Replaces all occurrences of $search in $str by $replacement.
1750
     *
1751
     * @param array        $search        <p>The elements to search for.</p>
1752
     * @param array|string $replacement   <p>The string to replace with.</p>
1753
     * @param bool         $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p>
1754
     *
1755
     * @return static
1756
     *                <p>Object with the resulting $str after the replacements.</p>
1757
     */
1758 30
    public function replaceAll(array $search, $replacement, bool $caseSensitive = true): self
1759
    {
1760 30
        if ($caseSensitive) {
1761 23
            return static::create(
1762 23
                $this->utf8::str_replace($search, $replacement, $this->str),
1763 23
                $this->encoding
1764
            );
1765
        }
1766
1767 7
        return static::create(
1768 7
            $this->utf8::str_ireplace($search, $replacement, $this->str),
1769 7
            $this->encoding
1770
        );
1771
    }
1772
1773
    /**
1774
     * Replaces first occurrences of $search from the beginning of string with $replacement.
1775
     *
1776
     * @param string $search      <p>The string to search for.</p>
1777
     * @param string $replacement <p>The replacement.</p>
1778
     *
1779
     * @return static
1780
     *                <p>Object with the resulting $str after the replacements.</p>
1781
     */
1782 16
    public function replaceFirst(string $search, string $replacement): self
1783
    {
1784 16
        return static::create(
1785 16
            $this->utf8::str_replace_first($search, $replacement, $this->str),
1786 16
            $this->encoding
1787
        );
1788
    }
1789
1790
    /**
1791
     * Replaces last occurrences of $search from the ending of string with $replacement.
1792
     *
1793
     * @param string $search      <p>The string to search for.</p>
1794
     * @param string $replacement <p>The replacement.</p>
1795
     *
1796
     * @return static
1797
     *                <p>Object with the resulting $str after the replacements.</p>
1798
     */
1799 15
    public function replaceLast(string $search, string $replacement): self
1800
    {
1801 15
        return static::create(
1802 15
            $this->utf8::str_replace_last($search, $replacement, $this->str),
1803 15
            $this->encoding
1804
        );
1805
    }
1806
1807
    /**
1808
     * Replaces all occurrences of $search from the beginning of string with $replacement.
1809
     *
1810
     * @param string $search      <p>The string to search for.</p>
1811
     * @param string $replacement <p>The replacement.</p>
1812
     *
1813
     * @return static
1814
     *                <p>Object with the resulting $str after the replacements.</p>
1815
     */
1816 16
    public function replaceBeginning(string $search, string $replacement): self
1817
    {
1818 16
        return static::create(
1819 16
            $this->utf8::str_replace_beginning($this->str, $search, $replacement),
1820 16
            $this->encoding
1821
        );
1822
    }
1823
1824
    /**
1825
     * Replaces all occurrences of $search from the ending of string with $replacement.
1826
     *
1827
     * @param string $search      <p>The string to search for.</p>
1828
     * @param string $replacement <p>The replacement.</p>
1829
     *
1830
     * @return static
1831
     *                <p>Object with the resulting $str after the replacements.</p>
1832
     */
1833 16
    public function replaceEnding(string $search, string $replacement): self
1834
    {
1835 16
        return static::create(
1836 16
            $this->utf8::str_replace_ending($this->str, $search, $replacement),
1837 16
            $this->encoding
1838
        );
1839
    }
1840
1841
    /**
1842
     * Returns a reversed string. A multibyte version of strrev().
1843
     *
1844
     * @return static
1845
     *                <p>Object with a reversed $str.</p>
1846
     */
1847 10
    public function reverse(): self
1848
    {
1849 10
        return static::create($this->utf8::strrev($this->str), $this->encoding);
1850
    }
1851
1852
    /**
1853
     * Truncates the string to a given length, while ensuring that it does not
1854
     * split words. If $substring is provided, and truncating occurs, the
1855
     * string is further truncated so that the substring may be appended without
1856
     * exceeding the desired length.
1857
     *
1858
     * @param int    $length                          <p>Desired length of the truncated string.</p>
1859
     * @param string $substring                       [optional] <p>The substring to append if it can fit. Default: ''</p>
1860
     * @param bool   $ignoreDoNotSplitWordsForOneWord
1861
     *
1862
     * @return static
1863
     *                <p>Object with the resulting $str after truncating.</p>
1864
     */
1865 45
    public function safeTruncate(int $length, string $substring = '', bool $ignoreDoNotSplitWordsForOneWord = true): self
1866
    {
1867 45
        return static::create(
1868 45
            $this->utf8::str_truncate_safe(
1869 45
                $this->str,
1870 45
                $length,
1871 45
                $substring,
1872 45
                $this->encoding,
1873 45
                $ignoreDoNotSplitWordsForOneWord
1874
            ),
1875 45
            $this->encoding
1876
        );
1877
    }
1878
1879
    /**
1880
     * Shorten the string after $length, but also after the next word.
1881
     *
1882
     * @param int    $length
1883
     * @param string $strAddOn [optional] <p>Default: '…'</p>
1884
     *
1885
     * @return static
1886
     */
1887 4
    public function shortenAfterWord(int $length, string $strAddOn = '…'): self
1888
    {
1889 4
        return static::create(
1890 4
            $this->utf8::str_limit_after_word($this->str, $length, $strAddOn),
1891 4
            $this->encoding
1892
        );
1893
    }
1894
1895
    /**
1896
     * A multibyte string shuffle function. It returns a string with its
1897
     * characters in random order.
1898
     *
1899
     * @return static
1900
     *                <p>Object with a shuffled $str.</p>
1901
     */
1902 6
    public function shuffle(): self
1903
    {
1904 6
        return static::create($this->utf8::str_shuffle($this->str), $this->encoding);
1905
    }
1906
1907
    /**
1908
     * Returns the substring beginning at $start, and up to, but not including
1909
     * the index specified by $end. If $end is omitted, the function extracts
1910
     * the remaining string. If $end is negative, it is computed from the end
1911
     * of the string.
1912
     *
1913
     * @param int $start <p>Initial index from which to begin extraction.</p>
1914
     * @param int $end   [optional] <p>Index at which to end extraction. Default: null</p>
1915
     *
1916
     * @return static
1917
     *                <p>Object with its $str being the extracted substring.</p>
1918
     */
1919 34
    public function slice(int $start, int $end = null): self
1920
    {
1921 34
        return static::create(
1922 34
            $this->utf8::str_slice($this->str, $start, $end, $this->encoding),
1923 34
            $this->encoding
1924
        );
1925
    }
1926
1927
    /**
1928
     * Converts the string into an URL slug. This includes replacing non-ASCII
1929
     * characters with their closest ASCII equivalents, removing remaining
1930
     * non-ASCII and non-alphanumeric characters, and replacing whitespace with
1931
     * $separator. The separator defaults to a single dash, and the string
1932
     * is also converted to lowercase. The language of the source string can
1933
     * also be supplied for language-specific transliteration.
1934
     *
1935
     * @param string                $separator             [optional] <p>The string used to replace whitespace.</p>
1936
     * @param string                $language              [optional] <p>Language of the source string.</p>
1937
     * @param array<string, string> $replacements          [optional] <p>A map of replaceable strings.</p>
1938
     * @param bool                  $replace_extra_symbols [optional]  <p>Add some more replacements e.g. "£" with "
1939
     *                                                     pound ".</p>
1940
     * @param bool                  $use_str_to_lower      [optional] <p>Use "string to lower" for the input.</p>
1941
     * @param bool                  $use_transliterate     [optional]  <p>Use ASCII::to_transliterate() for unknown
1942
     *                                                     chars.</p>
1943
     *
1944
     * @return static
1945
     *                <p>Object whose $str has been converted to an URL slug.</p>
1946
     */
1947 17
    public function slugify(
1948
        string $separator = '-',
1949
        string $language = 'en',
1950
        array $replacements = [],
1951
        bool $replace_extra_symbols = true,
1952
        bool $use_str_to_lower = true,
1953
        bool $use_transliterate = false
1954
    ): self {
1955 17
        return static::create(
1956 17
            $this->ascii::to_slugify(
1957 17
                $this->str,
1958 17
                $separator,
1959 17
                $language,
1960 17
                $replacements,
1961 17
                $replace_extra_symbols,
1962 17
                $use_str_to_lower,
1963 17
                $use_transliterate
1964
            ),
1965 17
            $this->encoding
1966
        );
1967
    }
1968
1969
    /**
1970
     * Converts the string into an URL slug. This includes replacing non-ASCII
1971
     * characters with their closest ASCII equivalents, removing remaining
1972
     * non-ASCII and non-alphanumeric characters, and replacing whitespace with
1973
     * $separator. The separator defaults to a single dash, and the string
1974
     * is also converted to lowercase.
1975
     *
1976
     * @param string                $separator    [optional] <p>The string used to replace whitespace. Default: '-'</p>
1977
     * @param string                $language     [optional] <p>The language for the url. Default: 'en'</p>
1978
     * @param array<string, string> $replacements [optional] <p>A map of replaceable strings.</p>
1979
     * @param bool                  $strToLower   [optional] <p>string to lower. Default: true</p>
1980
     *
1981
     * @return static
1982
     *                <p>Object whose $str has been converted to an URL slug.</p>
1983
     */
1984 16
    public function urlify(
1985
        string $separator = '-',
1986
        string $language = 'en',
1987
        array $replacements = [],
1988
        bool $strToLower = true
1989
    ): self {
1990
        // init
1991 16
        $str = $this->str;
1992
1993 16
        foreach ($replacements as $from => $to) {
1994 16
            $str = \str_replace($from, $to, $str);
1995
        }
1996
1997 16
        return static::create(
1998 16
            URLify::slug(
1999 16
                $str,
2000 16
                $language,
2001 16
                $separator,
2002 16
                $strToLower
2003
            ),
2004 16
            $this->encoding
2005
        );
2006
    }
2007
2008
    /**
2009
     * Convert a string to e.g.: "snake_case"
2010
     *
2011
     * @return static
2012
     *                <p>Object with $str in snake_case.</p>
2013
     */
2014 20
    public function snakeize(): self
2015
    {
2016 20
        return static::create(
2017 20
            $this->utf8::str_snakeize($this->str, $this->encoding),
2018 20
            $this->encoding
2019
        );
2020
    }
2021
2022
    /**
2023
     * Splits the string into chunks of Stringy objects.
2024
     *
2025
     * @param int $length
2026
     *
2027
     * @return static[]
2028
     *                  <p>An array of Stringy objects.</p>
2029
     */
2030 4
    public function chunk(int $length = 1): array
2031
    {
2032 4
        if ($length < 1) {
2033
            throw new \InvalidArgumentException('The chunk length must be greater than zero.');
2034
        }
2035
2036 4
        if ($this->str === '') {
2037
            return [];
2038
        }
2039
2040 4
        $chunks = $this->utf8::str_split($this->str, $length);
2041 4
        foreach ($chunks as $i => &$value) {
2042 4
            $value = static::create($value, $this->encoding);
2043
        }
2044
2045 4
        return $chunks;
2046
    }
2047
2048
    /**
2049
     * Splits the string with the provided regular expression, returning an
2050
     * array of Stringy objects. An optional integer $limit will truncate the
2051
     * results.
2052
     *
2053
     * @param string $pattern <p>The regex with which to split the string.</p>
2054
     * @param int    $limit   [optional] <p>Maximum number of results to return. Default: -1 === no limit</p>
2055
     *
2056
     * @return static[]
2057
     *                  <p>An array of Stringy objects.</p>
2058
     */
2059 35
    public function split(string $pattern, int $limit = null): array
2060
    {
2061 35
        if ($limit === null) {
2062 7
            $limit = -1;
2063
        }
2064
2065 35
        $array = $this->utf8::str_split_pattern($this->str, $pattern, $limit);
2066 35
        foreach ($array as $i => &$value) {
2067 31
            $value = static::create($value, $this->encoding);
2068
        }
2069
2070 35
        return $array;
2071
    }
2072
2073
    /**
2074
     * Returns true if the string begins with $substring, false otherwise. By
2075
     * default, the comparison is case-sensitive, but can be made insensitive
2076
     * by setting $caseSensitive to false.
2077
     *
2078
     * @param string $substring     <p>The substring to look for.</p>
2079
     * @param bool   $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p>
2080
     *
2081
     * @return bool
2082
     *              <p>Whether or not $str starts with $substring.</p>
2083
     */
2084 55
    public function startsWith(string $substring, bool $caseSensitive = true): bool
2085
    {
2086 55
        if ($caseSensitive) {
2087 30
            return $this->utf8::str_starts_with($this->str, $substring);
2088
        }
2089
2090 25
        return $this->utf8::str_istarts_with($this->str, $substring);
2091
    }
2092
2093
    /**
2094
     * Returns true if the string begins with any of $substrings, false otherwise.
2095
     * By default the comparison is case-sensitive, but can be made insensitive by
2096
     * setting $caseSensitive to false.
2097
     *
2098
     * @param array $substrings    <p>Substrings to look for.</p>
2099
     * @param bool  $caseSensitive [optional] <p>Whether or not to enforce case-sensitivity. Default: true</p>
2100
     *
2101
     * @return bool
2102
     *              <p>Whether or not $str starts with $substring.</p>
2103
     */
2104 23
    public function startsWithAny(array $substrings, bool $caseSensitive = true): bool
2105
    {
2106 23
        if ($caseSensitive) {
2107 15
            return $this->utf8::str_starts_with_any($this->str, $substrings);
2108
        }
2109
2110 8
        return $this->utf8::str_istarts_with_any($this->str, $substrings);
2111
    }
2112
2113
    /**
2114
     * Strip all whitespace characters. This includes tabs and newline characters,
2115
     * as well as multibyte whitespace such as the thin space and ideographic space.
2116
     *
2117
     * @return static
2118
     */
2119 24
    public function stripWhitespace(): self
2120
    {
2121 24
        return static::create(
2122 24
            $this->utf8::strip_whitespace($this->str),
2123 24
            $this->encoding
2124
        );
2125
    }
2126
2127
    /**
2128
     * Remove css media-queries.
2129
     *
2130
     * @return static
2131
     */
2132 1
    public function stripeCssMediaQueries(): self
2133
    {
2134 1
        return static::create(
2135 1
            $this->utf8::css_stripe_media_queries($this->str),
2136 1
            $this->encoding
2137
        );
2138
    }
2139
2140
    /**
2141
     * Remove empty html-tag.
2142
     *
2143
     * e.g.: <tag></tag>
2144
     *
2145
     * @return static
2146
     */
2147 1
    public function stripeEmptyHtmlTags(): self
2148
    {
2149 1
        return static::create(
2150 1
            $this->utf8::html_stripe_empty_tags($this->str),
2151 1
            $this->encoding
2152
        );
2153
    }
2154
2155
    /**
2156
     * Returns the substring beginning at $start with the specified $length.
2157
     * It differs from the $this->utf8::substr() function in that providing a $length of
2158
     * null will return the rest of the string, rather than an empty string.
2159
     *
2160
     * @param int $start  <p>Position of the first character to use.</p>
2161
     * @param int $length [optional] <p>Maximum number of characters used. Default: null</p>
2162
     *
2163
     * @return static
2164
     *                <p>Object with its $str being the substring.</p>
2165
     */
2166 18
    public function substr(int $start, int $length = null): self
2167
    {
2168 18
        return static::create(
2169 18
            $this->utf8::substr(
2170 18
                $this->str,
2171 18
                $start,
2172 18
                $length,
2173 18
                $this->encoding
2174
            ),
2175 18
            $this->encoding
2176
        );
2177
    }
2178
2179
    /**
2180
     * Gets the substring after (or before via "$beforeNeedle") the first occurrence of the "$needle".
2181
     * If no match is found returns new empty Stringy object.
2182
     *
2183
     * @param string $needle       <p>The string to look for.</p>
2184
     * @param bool   $beforeNeedle [optional] <p>Default: false</p>
2185
     *
2186
     * @return static
2187
     */
2188 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...
2189
    {
2190 2
        return static::create(
2191 2
            $this->utf8::str_substr_first(
2192 2
                $this->str,
2193 2
                $needle,
2194 2
                $beforeNeedle,
2195 2
                $this->encoding
2196
            ),
2197 2
            $this->encoding
2198
        );
2199
    }
2200
2201
    /**
2202
     * Gets the substring after (or before via "$beforeNeedle") the first occurrence of the "$needle".
2203
     * If no match is found returns new empty Stringy object.
2204
     *
2205
     * @param string $needle       <p>The string to look for.</p>
2206
     * @param bool   $beforeNeedle [optional] <p>Default: false</p>
2207
     *
2208
     * @return static
2209
     */
2210 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...
2211
    {
2212 2
        return static::create(
2213 2
            $this->utf8::str_isubstr_first(
2214 2
                $this->str,
2215 2
                $needle,
2216 2
                $beforeNeedle,
2217 2
                $this->encoding
2218
            ),
2219 2
            $this->encoding
2220
        );
2221
    }
2222
2223
    /**
2224
     * Surrounds $str with the given substring.
2225
     *
2226
     * @param string $substring <p>The substring to add to both sides.</P>
2227
     *
2228
     * @return static
2229
     *                <p>Object whose $str had the substring both prepended and appended.</p>
2230
     */
2231 10
    public function surround(string $substring): self
2232
    {
2233 10
        return static::create(
2234 10
            $substring . $this->str . $substring,
2235 10
            $this->encoding
2236
        );
2237
    }
2238
2239
    /**
2240
     * Returns a case swapped version of the string.
2241
     *
2242
     * @return static
2243
     *                <p>Object whose $str has each character's case swapped.</P>
2244
     */
2245 10
    public function swapCase(): self
2246
    {
2247 10
        return static::create(
2248 10
            $this->utf8::swapCase($this->str, $this->encoding),
2249 10
            $this->encoding
2250
        );
2251
    }
2252
2253
    /**
2254
     * Returns a string with smart quotes, ellipsis characters, and dashes from
2255
     * Windows-1252 (commonly used in Word documents) replaced by their ASCII
2256
     * equivalents.
2257
     *
2258
     * @return static
2259
     *                <p>Object whose $str has those characters removed.</p>
2260
     */
2261 8
    public function tidy(): self
2262
    {
2263 8
        return static::create(
2264 8
            $this->ascii::normalize_msword($this->str),
2265 8
            $this->encoding
2266
        );
2267
    }
2268
2269
    /**
2270
     * Returns a trimmed string with the first letter of each word capitalized.
2271
     * Also accepts an array, $ignore, allowing you to list words not to be
2272
     * capitalized.
2273
     *
2274
     * @param array|string[]|null $ignore            [optional] <p>An array of words not to capitalize or null.
2275
     *                                               Default: null</p>
2276
     * @param string|null         $word_define_chars [optional] <p>An string of chars that will be used as whitespace
2277
     *                                               separator === words.</p>
2278
     * @param string|null         $language          [optional] <p>Language of the source string.</p>
2279
     *
2280
     * @return static
2281
     *                <p>Object with a titleized $str.</p>
2282
     */
2283 14
    public function titleize(
2284
        array $ignore = null,
2285
        string $word_define_chars = null,
2286
        string $language = null
2287
    ): self {
2288 14
        return static::create(
2289 14
            $this->utf8::str_titleize(
2290 14
                $this->str,
2291 14
                $ignore,
2292 14
                $this->encoding,
2293 14
                false,
2294 14
                $language,
2295 14
                false,
2296 14
                true,
2297 14
                $word_define_chars
2298
            ),
2299 14
            $this->encoding
2300
        );
2301
    }
2302
2303
    /**
2304
     * Returns a trimmed string in proper title case.
2305
     *
2306
     * Also accepts an array, $ignore, allowing you to list words not to be
2307
     * capitalized.
2308
     *
2309
     * Adapted from John Gruber's script.
2310
     *
2311
     * @see https://gist.github.com/gruber/9f9e8650d68b13ce4d78
2312
     *
2313
     * @param array $ignore <p>An array of words not to capitalize.</p>
2314
     *
2315
     * @return static
2316
     *                <p>Object with a titleized $str</p>
2317
     */
2318 35
    public function titleizeForHumans(array $ignore = []): self
2319
    {
2320 35
        return static::create(
2321 35
            $this->utf8::str_titleize_for_humans(
2322 35
                $this->str,
2323 35
                $ignore,
2324 35
                $this->encoding
2325
            ),
2326 35
            $this->encoding
2327
        );
2328
    }
2329
2330
    /**
2331
     * Returns an ASCII version of the string. A set of non-ASCII characters are
2332
     * replaced with their closest ASCII counterparts, and the rest are removed
2333
     * unless instructed otherwise.
2334
     *
2335
     * @param bool   $strict  [optional] <p>Use "transliterator_transliterate()" from PHP-Intl | WARNING: bad
2336
     *                        performance | Default: false</p>
2337
     * @param string $unknown [optional] <p>Character use if character unknown. (default is ?)</p>
2338
     *
2339
     * @return static
2340
     *                <p>Object whose $str contains only ASCII characters.</p>
2341
     */
2342 17
    public function toTransliterate(bool $strict = false, string $unknown = '?'): self
2343
    {
2344 17
        return static::create(
2345 17
            $this->ascii::to_transliterate($this->str, $unknown, $strict),
2346 17
            $this->encoding
2347
        );
2348
    }
2349
2350
    /**
2351
     * Returns an ASCII version of the string. A set of non-ASCII characters are
2352
     * replaced with their closest ASCII counterparts, and the rest are removed
2353
     * by default. The language or locale of the source string can be supplied
2354
     * for language-specific transliteration in any of the following formats:
2355
     * en, en_GB, or en-GB. For example, passing "de" results in "äöü" mapping
2356
     * to "aeoeue" rather than "aou" as in other languages.
2357
     *
2358
     * @param string $language          [optional] <p>Language of the source string.</p>
2359
     * @param bool   $removeUnsupported [optional] <p>Whether or not to remove the
2360
     *                                  unsupported characters.</p>
2361
     *
2362
     * @return static
2363
     *                <p>Object whose $str contains only ASCII characters.</p>
2364
     */
2365 23
    public function toAscii(string $language = 'en', bool $removeUnsupported = true): self
2366
    {
2367 23
        return static::create(
2368 23
            $this->ascii::to_ascii(
2369 23
                $this->str,
2370 23
                $language,
2371 23
                $removeUnsupported
2372
            ),
2373 23
            $this->encoding
2374
        );
2375
    }
2376
2377
    /**
2378
     * Returns a boolean representation of the given logical string value.
2379
     * For example, 'true', '1', 'on' and 'yes' will return true. 'false', '0',
2380
     * 'off', and 'no' will return false. In all instances, case is ignored.
2381
     * For other numeric strings, their sign will determine the return value.
2382
     * In addition, blank strings consisting of only whitespace will return
2383
     * false. For all other strings, the return value is a result of a
2384
     * boolean cast.
2385
     *
2386
     * @return bool
2387
     *              <p>A boolean value for the string.</p>
2388
     */
2389 30
    public function toBoolean(): bool
2390
    {
2391 30
        return $this->utf8::to_boolean($this->str);
2392
    }
2393
2394
    /**
2395
     * Converts all characters in the string to lowercase.
2396
     *
2397
     * @param bool        $tryToKeepStringLength [optional] <p>true === try to keep the string length: e.g. ẞ -> ß</p>
2398
     * @param string|null $lang                  [optional] <p>Set the language for special cases: az, el, lt, tr</p>
2399
     *
2400
     * @return static
2401
     *                <p>Object with all characters of $str being lowercase.</p>
2402
     */
2403 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...
2404
    {
2405 12
        return static::create(
2406 12
            $this->utf8::strtolower(
2407 12
                $this->str,
2408 12
                $this->encoding,
2409 12
                false,
2410 12
                $lang,
2411 12
                $tryToKeepStringLength
2412
            ),
2413 12
            $this->encoding
2414
        );
2415
    }
2416
2417
    /**
2418
     * Converts each tab in the string to some number of spaces, as defined by
2419
     * $tabLength. By default, each tab is converted to 4 consecutive spaces.
2420
     *
2421
     * @param int $tabLength [optional] <p>Number of spaces to replace each tab with. Default: 4</p>
2422
     *
2423
     * @return static
2424
     *                <p>Object whose $str has had tabs switched to spaces.</p>
2425
     */
2426 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...
2427
    {
2428 12
        if ($tabLength === 4) {
2429 6
            $tab = '    ';
2430 6
        } elseif ($tabLength === 2) {
2431 2
            $tab = '  ';
2432
        } else {
2433 4
            $tab = \str_repeat(' ', $tabLength);
2434
        }
2435
2436 12
        return static::create(
2437 12
            \str_replace("\t", $tab, $this->str),
2438 12
            $this->encoding
2439
        );
2440
    }
2441
2442
    /**
2443
     * Return Stringy object as string, but you can also use (string) for automatically casting the object into a
2444
     * string.
2445
     *
2446
     * @return string
2447
     */
2448 1083
    public function toString(): string
2449
    {
2450 1083
        return (string) $this->str;
2451
    }
2452
2453
    /**
2454
     * Converts each occurrence of some consecutive number of spaces, as
2455
     * defined by $tabLength, to a tab. By default, each 4 consecutive spaces
2456
     * are converted to a tab.
2457
     *
2458
     * @param int $tabLength [optional] <p>Number of spaces to replace with a tab. Default: 4</p>
2459
     *
2460
     * @return static
2461
     *                <p>Object whose $str has had spaces switched to tabs.</p>
2462
     */
2463 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...
2464
    {
2465 10
        if ($tabLength === 4) {
2466 6
            $tab = '    ';
2467 4
        } elseif ($tabLength === 2) {
2468 2
            $tab = '  ';
2469
        } else {
2470 2
            $tab = \str_repeat(' ', $tabLength);
2471
        }
2472
2473 10
        return static::create(
2474 10
            \str_replace($tab, "\t", $this->str),
2475 10
            $this->encoding
2476
        );
2477
    }
2478
2479
    /**
2480
     * Converts the first character of each word in the string to uppercase
2481
     * and all other chars to lowercase.
2482
     *
2483
     * @return static
2484
     *                <p>Object with all characters of $str being title-cased.</p>
2485
     */
2486 10
    public function toTitleCase(): self
2487
    {
2488 10
        return static::create(
2489 10
            $this->utf8::titlecase($this->str, $this->encoding),
2490 10
            $this->encoding
2491
        );
2492
    }
2493
2494
    /**
2495
     * Converts all characters in the string to uppercase.
2496
     *
2497
     * @param bool        $tryToKeepStringLength [optional] <p>true === try to keep the string length: e.g. ẞ -> ß</p>
2498
     * @param string|null $lang                  [optional] <p>Set the language for special cases: az, el, lt, tr</p>
2499
     *
2500
     * @return static
2501
     *                <p>Object with all characters of $str being uppercase.</p>
2502
     */
2503 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...
2504
    {
2505 11
        return static::create(
2506 11
            $this->utf8::strtoupper($this->str, $this->encoding, false, $lang, $tryToKeepStringLength),
2507 11
            $this->encoding
2508
        );
2509
    }
2510
2511
    /**
2512
     * Returns a string with whitespace removed from the start and end of the
2513
     * string. Supports the removal of unicode whitespace. Accepts an optional
2514
     * string of characters to strip instead of the defaults.
2515
     *
2516
     * @param string $chars [optional] <p>String of characters to strip. Default: null</p>
2517
     *
2518
     * @return static
2519
     *                <p>Object with a trimmed $str.</p>
2520
     */
2521 24
    public function trim(string $chars = null): self
2522
    {
2523 24
        return static::create(
2524 24
            $this->utf8::trim($this->str, $chars),
2525 24
            $this->encoding
2526
        );
2527
    }
2528
2529
    /**
2530
     * Returns a string with whitespace removed from the start of the string.
2531
     * Supports the removal of unicode whitespace. Accepts an optional
2532
     * string of characters to strip instead of the defaults.
2533
     *
2534
     * @param string $chars [optional] <p>Optional string of characters to strip. Default: null</p>
2535
     *
2536
     * @return static
2537
     *                <p>Object with a trimmed $str.</p>
2538
     */
2539 26
    public function trimLeft(string $chars = null): self
2540
    {
2541 26
        return static::create(
2542 26
            $this->utf8::ltrim($this->str, $chars),
2543 26
            $this->encoding
2544
        );
2545
    }
2546
2547
    /**
2548
     * Returns a string with whitespace removed from the end of the string.
2549
     * Supports the removal of unicode whitespace. Accepts an optional
2550
     * string of characters to strip instead of the defaults.
2551
     *
2552
     * @param string $chars [optional] <p>Optional string of characters to strip. Default: null</p>
2553
     *
2554
     * @return static
2555
     *                <p>Object with a trimmed $str.</p>
2556
     */
2557 26
    public function trimRight(string $chars = null): self
2558
    {
2559 26
        return static::create(
2560 26
            $this->utf8::rtrim($this->str, $chars),
2561 26
            $this->encoding
2562
        );
2563
    }
2564
2565
    /**
2566
     * Truncates the string to a given length. If $substring is provided, and
2567
     * truncating occurs, the string is further truncated so that the substring
2568
     * may be appended without exceeding the desired length.
2569
     *
2570
     * @param int    $length    <p>Desired length of the truncated string.</p>
2571
     * @param string $substring [optional] <p>The substring to append if it can fit. Default: ''</p>
2572
     *
2573
     * @return static
2574
     *                <p>Object with the resulting $str after truncating.</p>
2575
     */
2576 44
    public function truncate(int $length, string $substring = ''): self
2577
    {
2578 44
        return static::create(
2579 44
            $this->utf8::str_truncate($this->str, $length, $substring, $this->encoding),
2580 44
            $this->encoding
2581
        );
2582
    }
2583
2584
    /**
2585
     * Returns a lowercase and trimmed string separated by underscores.
2586
     * Underscores are inserted before uppercase characters (with the exception
2587
     * of the first character of the string), and in place of spaces as well as
2588
     * dashes.
2589
     *
2590
     * @return static
2591
     *                <p>Object with an underscored $str.</p>
2592
     */
2593 32
    public function underscored(): self
2594
    {
2595 32
        return $this->delimit('_');
2596
    }
2597
2598
    /**
2599
     * Returns an UpperCamelCase version of the supplied string. It trims
2600
     * surrounding spaces, capitalizes letters following digits, spaces, dashes
2601
     * and underscores, and removes spaces, dashes, underscores.
2602
     *
2603
     * @return static
2604
     *                <p>Object with $str in UpperCamelCase.</p>
2605
     */
2606 26
    public function upperCamelize(): self
2607
    {
2608 26
        return static::create(
2609 26
            $this->utf8::str_upper_camelize($this->str, $this->encoding),
2610 26
            $this->encoding
2611
        );
2612
    }
2613
2614
    /**
2615
     * Converts the first character of the supplied string to upper case.
2616
     *
2617
     * @return static
2618
     *                <p>Object with the first character of $str being upper case.</p>
2619
     */
2620 12
    public function upperCaseFirst(): self
2621
    {
2622 12
        return static::create($this->utf8::ucfirst($this->str, $this->encoding), $this->encoding);
2623
    }
2624
2625
    /**
2626
     * Converts the string into an valid UTF-8 string.
2627
     *
2628
     * @return static
2629
     */
2630 1
    public function utf8ify(): self
2631
    {
2632 1
        return static::create($this->utf8::cleanup($this->str), $this->encoding);
2633
    }
2634
2635
    /**
2636
     * Returns the replacements for the toAscii() method.
2637
     *
2638
     * @noinspection PhpUnused
2639
     *
2640
     * @return array<string, array<int, string>>
0 ignored issues
show
Documentation introduced by
The doc-type array<string, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
2641
     *                       <p>An array of replacements.</p>
2642
     *
2643
     * @deprecated   this is only here for backward-compatibly reasons
2644
     */
2645 1
    protected function charsArray(): array
2646
    {
2647 1
        return $this->ascii::charsArrayWithMultiLanguageValues();
2648
    }
2649
2650
    /**
2651
     * Returns true if $str matches the supplied pattern, false otherwise.
2652
     *
2653
     * @param string $pattern <p>Regex pattern to match against.</p>
2654
     *
2655
     * @return bool
2656
     *              <p>Whether or not $str matches the pattern.</p>
2657
     */
2658 12
    protected function matchesPattern(string $pattern): bool
2659
    {
2660 12
        return $this->utf8::str_matches_pattern($this->str, $pattern);
2661
    }
2662
}
2663