Completed
Push — master ( 499bec...32c41e )
by Lars
02:45
created

Stringy::toTitleCase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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