GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — develop ( ab97fd...70bc1b )
by Baptiste
02:33
created

Str::ucfirst()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Immutable;
5
6
use Innmind\Immutable\Exception\RegexException;
7
use Innmind\Immutable\Exception\SubstringException;
8
9
class Str implements PrimitiveInterface, StringableInterface
10
{
11
    const PAD_RIGHT = STR_PAD_RIGHT;
12
    const PAD_LEFT = STR_PAD_LEFT;
13
    const PAD_BOTH = STR_PAD_BOTH;
14
    const PREG_NO_FLAGS = 0;
15
    const PREG_SPLIT_NO_EMPTY = PREG_SPLIT_NO_EMPTY;
16
    const PREG_SPLIT_DELIM_CAPTURE = PREG_SPLIT_DELIM_CAPTURE;
17
    const PREG_SPLIT_OFFSET_CAPTURE = PREG_SPLIT_OFFSET_CAPTURE;
18
    const PREG_OFFSET_CAPTURE = PREG_OFFSET_CAPTURE;
19
20
    private $value;
21
    private $encoding;
22
23 312
    public function __construct(string $value, string $encoding = null)
24
    {
25 312
        $this->value = $value;
26 312
        $this->encoding = $encoding ?? mb_internal_encoding();
27 312
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 2
    public function toPrimitive()
33
    {
34 2
        return $this->value;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 204
    public function __toString(): string
41
    {
42 204
        return $this->value;
43
    }
44
45 4
    public function encoding(): self
46
    {
47 4
        return new self($this->encoding);
48
    }
49
50 30
    public function toEncoding(string $encoding): self
51
    {
52 30
        return new self($this->value, $encoding);
53
    }
54
55
    /**
56
     * Split the string into a collection of ones
57
     *
58
     * @param string $delimiter
59
     *
60
     * @return StreamInterface<self>
0 ignored issues
show
Documentation introduced by
The doc-type StreamInterface<self> could not be parsed: Expected "|" or "end of type", but got "<" at position 15. (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...
61
     */
62 12
    public function split(string $delimiter = null): StreamInterface
63
    {
64 12
        if (is_null($delimiter) || $delimiter === '') {
65 6
            return $this->chunk();
66
        }
67
68 8
        $parts = explode($delimiter, $this->value);
69 8
        $stream = new Stream(self::class);
70
71 8
        foreach ($parts as $part) {
72 8
            $stream = $stream->add(new self($part, $this->encoding));
1 ignored issue
show
Documentation introduced by
new self($part, $this->encoding) is of type object<Innmind\Immutable\Str>, but the function expects a object<Innmind\Immutable\T>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
73
        }
74
75 8
        return $stream;
76
    }
77
78
    /**
79
     * Returns a collection of the string splitted by the given chunk size
80
     *
81
     * @param int $size
82
     *
83
     * @return StreamInterface<self>
0 ignored issues
show
Documentation introduced by
The doc-type StreamInterface<self> could not be parsed: Expected "|" or "end of type", but got "<" at position 15. (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...
84
     */
85 14
    public function chunk(int $size = 1): StreamInterface
86
    {
87 14
        $stream = new Stream(self::class);
88 14
        $string = $this;
89
90 14
        while ($string->length() > 0) {
91 14
            $stream = $stream->add($string->substring(0, $size));
1 ignored issue
show
Documentation introduced by
$string->substring(0, $size) is of type object<Innmind\Immutable\Str>, but the function expects a object<Innmind\Immutable\T>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
92 14
            $string = $string->substring($size);
93
        }
94
95 14
        return $stream;
96
    }
97
98
    /**
99
     * Returns the position of the first occurence of the string
100
     *
101
     * @param string $needle
102
     * @param int $offset
103
     *
104
     * @throws SubstringException If the string is not found
105
     *
106
     * @return int
107
     */
108 10
    public function position(string $needle, int $offset = 0): int
109
    {
110 10
        $position = mb_strpos($this->value, $needle, $offset, $this->encoding);
111
112 10
        if ($position === false) {
113 4
            throw new SubstringException(sprintf(
114 4
                'Substring "%s" not found',
115 4
                $needle
116
            ));
117
        }
118
119 8
        return (int) $position;
120
    }
121
122
    /**
123
     * Replace all occurences of the search string with the replacement one
124
     *
125
     * @param string $search
126
     * @param string $replacement
127
     *
128
     * @return self
129
     */
130 4
    public function replace(string $search, string $replacement): self
131
    {
132 4
        if ($this->usesDefaultEncoding()) {
133 4
            $string = (string) mb_ereg_replace(
134 4
                $search,
135 4
                $replacement,
136 4
                $this->value
137
            );
138
139 4
            return new self($string, $this->encoding);
140
        }
141
142 2
        if (!$this->contains($search)) {
143
            return $this;
144
        }
145
146
        return $this
147 2
            ->split($search)
148 2
            ->join($replacement);
149
    }
150
151
    /**
152
     * Returns the string following the given delimiter
153
     *
154
     * @param string $delimiter
155
     *
156
     * @throws SubstringException If the string is not found
157
     *
158
     * @return self
159
     */
160 6
    public function str(string $delimiter): self
161
    {
162 6
        $sub = mb_strstr($this->value, $delimiter, false, $this->encoding);
163
164 6
        if ($sub === false) {
165 2
            throw new SubstringException(sprintf(
166 2
                'Substring "%s" not found',
167 2
                $delimiter
168
            ));
169
        }
170
171 4
        return new self($sub, $this->encoding);
172
    }
173
174
    /**
175
     * Return the string in upper case
176
     *
177
     * @return self
178
     */
179 6
    public function toUpper(): self
180
    {
181 6
        return new self(mb_strtoupper($this->value), $this->encoding);
182
    }
183
184
    /**
185
     * Return the string in lower case
186
     *
187
     * @return self
188
     */
189 4
    public function toLower(): self
190
    {
191 4
        return new self(mb_strtolower($this->value), $this->encoding);
192
    }
193
194
    /**
195
     * Return the string length
196
     *
197
     * @return int
198
     */
199 28
    public function length(): int
200
    {
201 28
        return mb_strlen($this->value, $this->encoding);
202
    }
203
204
    /**
205
     * Reverse the string
206
     *
207
     * @return self
208
     */
209 2
    public function reverse(): self
210
    {
211
        return $this
212 2
            ->chunk()
213 2
            ->reverse()
214 2
            ->join('');
215
    }
216
217
    /**
218
     * Pad to the right
219
     *
220
     * @param int $length
221
     * @param string $character
222
     *
223
     * @return self
224
     */
225 2
    public function rightPad(int $length, string $character = ' '): self
226
    {
227 2
        return $this->pad($length, $character, self::PAD_RIGHT);
228
    }
229
230
    /**
231
     * Pad to the left
232
     *
233
     * @param int $length
234
     * @param string $character
235
     *
236
     * @return self
237
     */
238 2
    public function leftPad(int $length, string $character = ' '): self
239
    {
240 2
        return $this->pad($length, $character, self::PAD_LEFT);
241
    }
242
243
    /**
244
     * Pad both sides
245
     *
246
     * @param int $length
247
     * @param string $character
248
     *
249
     * @return self
250
     */
251 2
    public function uniPad(int $length, string $character = ' '): self
252
    {
253 2
        return $this->pad($length, $character, self::PAD_BOTH);
254
    }
255
256
    /**
257
     * Find length of initial segment not matching mask
258
     *
259
     * @param string $mask
260
     * @param int $start
261
     * @param int $length
262
     *
263
     * @return int
264
     */
265 2
    public function cspn(string $mask, int $start = 0, int $length = null): int
266
    {
267 2
        if ($length === null) {
268 2
            $value = strcspn($this->value, $mask, $start);
269
        } else {
270 2
            $value = strcspn(
271 2
                $this->value,
272 2
                $mask,
273 2
                $start,
274 2
                $length
275
            );
276
        }
277
278 2
        return (int) $value;
279
    }
280
281
    /**
282
     * Repeat the string n times
283
     *
284
     * @param int $repeat
285
     *
286
     * @return self
287
     */
288 2
    public function repeat(int $repeat): self
289
    {
290 2
        return new self(str_repeat($this->value, $repeat), $this->encoding);
291
    }
292
293
    /**
294
     * Shuffle the string
295
     *
296
     * @return self
297
     */
298 4
    public function shuffle(): self
299
    {
300 4
        $parts = $this->chunk()->toPrimitive();
301 4
        shuffle($parts);
302
303 4
        return new self(implode('', $parts), $this->encoding);
304
    }
305
306
    /**
307
     * Strip slashes
308
     *
309
     * @return self
310
     */
311 2
    public function stripSlashes(): self
312
    {
313 2
        return new self(stripslashes($this->value), $this->encoding);
314
    }
315
316
    /**
317
     * Strip C-like slashes
318
     *
319
     * @return self
320
     */
321 2
    public function stripCSlashes(): self
322
    {
323 2
        return new self(stripcslashes($this->value), $this->encoding);
324
    }
325
326
    /**
327
     * Return the word count
328
     *
329
     * @param string $charlist
330
     *
331
     * @return int
332
     */
333 2
    public function wordCount(string $charlist = ''): int
334
    {
335 2
        return (int) str_word_count(
336 2
            $this->value,
337 2
            0,
338 2
            $charlist
339
        );
340
    }
341
342
    /**
343
     * Return the collection of words
344
     *
345
     * @param string $charlist
346
     *
347
     * @return MapInterface<int, self>
0 ignored issues
show
Documentation introduced by
The doc-type MapInterface<int, could not be parsed: Expected "|" or "end of type", but got "<" at position 12. (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...
348
     */
349 2 View Code Duplication
    public function words(string $charlist = ''): MapInterface
1 ignored issue
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...
350
    {
351 2
        $words = str_word_count($this->value, 2, $charlist);
352 2
        $map = new Map('int', self::class);
353
354 2
        foreach ($words as $position => $word) {
355 2
            $map = $map->put($position, new self($word, $this->encoding));
2 ignored issues
show
Documentation introduced by
$position is of type integer|string, but the function expects a object<Innmind\Immutable\T>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
new self($word, $this->encoding) is of type object<Innmind\Immutable\Str>, but the function expects a object<Innmind\Immutable\S>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
356
        }
357
358 2
        return $map;
359
    }
360
361
    /**
362
     * Split the string using a regular expression
363
     *
364
     * @param string $regex
365
     * @param int $limit
366
     *
367
     * @return StreamInterface<self>
0 ignored issues
show
Documentation introduced by
The doc-type StreamInterface<self> could not be parsed: Expected "|" or "end of type", but got "<" at position 15. (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...
368
     */
369 4 View Code Duplication
    public function pregSplit(string $regex, int $limit = -1): StreamInterface
1 ignored issue
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...
370
    {
371 4
        $strings = preg_split($regex, $this->value, $limit);
372 4
        $stream = new Stream(self::class);
373
374 4
        foreach ($strings as $string) {
375 4
            $stream = $stream->add(new self($string, $this->encoding));
1 ignored issue
show
Documentation introduced by
new self($string, $this->encoding) is of type object<Innmind\Immutable\Str>, but the function expects a object<Innmind\Immutable\T>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
376
        }
377
378 4
        return $stream;
379
    }
380
381
    /**
382
     * Check if the string match the given regular expression
383
     *
384
     * @param string $regex
385
     * @param int $offset
386
     *
387
     * @throws Exception If the regex failed
388
     *
389
     * @return bool
390
     */
391 4
    public function matches(string $regex, int $offset = 0): bool
392
    {
393 4
        $matches = [];
394 4
        $value = preg_match($regex, $this->value, $matches, 0, $offset);
395
396 4
        if ($value === false) {
397 2
            throw new RegexException('', preg_last_error());
398
        }
399
400 2
        return (bool) $value;
401
    }
402
403
    /**
404
     * Return a collection of the elements matching the regex
405
     *
406
     * @deprecated replaced by self::capture, to be removed in 3.0
407
     *
408
     * @param string $regex
409
     * @param int $offset
410
     * @param int $flags
411
     *
412
     * @throws Exception If the regex failed
413
     *
414
     * @return MapInterface<scalar, self>
0 ignored issues
show
Documentation introduced by
The doc-type MapInterface<scalar, could not be parsed: Expected "|" or "end of type", but got "<" at position 12. (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...
415
     */
416 2
    public function getMatches(
417
        string $regex,
418
        int $offset = 0,
419
        int $flags = self::PREG_NO_FLAGS
420
    ): MapInterface {
421 2
        return $this->capture($regex, $offset, $flags);
422
    }
423
424
    /**
425
     * Return a collection of the elements matching the regex
426
     *
427
     * @param string $regex
428
     * @param int $offset
429
     * @param int $flags
430
     *
431
     * @throws Exception If the regex failed
432
     *
433
     * @return MapInterface<scalar, self>
0 ignored issues
show
Documentation introduced by
The doc-type MapInterface<scalar, could not be parsed: Expected "|" or "end of type", but got "<" at position 12. (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...
434
     */
435 6
    public function capture(
436
        string $regex,
437
        int $offset = 0,
438
        int $flags = self::PREG_NO_FLAGS
439
    ): MapInterface {
440 6
        $matches = [];
441 6
        $value = preg_match(
442 6
            $regex,
443 6
            $this->value,
444 6
            $matches,
445 6
            $flags,
446 6
            $offset
447
        );
448 6
        $map = new Map('scalar', self::class);
449
450 6
        foreach ($matches as $key => $match) {
451 4
            $map = $map->put($key, new self((string) $match, $this->encoding));
2 ignored issues
show
Documentation introduced by
$key is of type integer, but the function expects a object<Innmind\Immutable\T>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
new self((string) $match, $this->encoding) is of type object<Innmind\Immutable\Str>, but the function expects a object<Innmind\Immutable\S>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
452
        }
453
454 6
        if ($value === false) {
455 2
            throw new RegexException('', preg_last_error());
456
        }
457
458 4
        return $map;
459
    }
460
461
    /**
462
     * Replace part of the string by using a regular expression
463
     *
464
     * @param string $regex
465
     * @param string $replacement
466
     * @param int $limit
467
     *
468
     * @throws Exception If the regex failed
469
     *
470
     * @return self
471
     */
472 2
    public function pregReplace(
473
        string $regex,
474
        string $replacement,
475
        int $limit = -1
476
    ): self {
477 2
        $value = preg_replace(
478 2
            $regex,
479 2
            $replacement,
480 2
            $this->value,
481 2
            $limit
482
        );
483
484 2
        if ($value === null) {
485
            throw new RegexException('', preg_last_error());
486
        }
487
488 2
        return new self($value, $this->encoding);
489
    }
490
491
    /**
492
     * Return part of the string
493
     *
494
     * @param int $start
495
     * @param int $length
496
     *
497
     * @return self
498
     */
499 24
    public function substring(int $start, int $length = null): self
500
    {
501 24
        if ($this->length() === 0) {
502 2
            return $this;
503
        }
504
505 24
        $sub = mb_substr($this->value, $start, $length, $this->encoding);
506
507 24
        return new self($sub, $this->encoding);
508
    }
509
510
    /**
511
     * Return a formatted string
512
     *
513
     * @return self
514
     */
515 2
    public function sprintf(...$values): self
516
    {
517 2
        return new self(sprintf($this->value, ...$values), $this->encoding);
518
    }
519
520
    /**
521
     * Return the string with the first letter as uppercase
522
     *
523
     * @return self
524
     */
525 4
    public function ucfirst(): self
526
    {
527
        return $this
528 4
            ->substring(0, 1)
529 4
            ->toUpper()
530 4
            ->append((string) $this->substring(1));
531
    }
532
533
    /**
534
     * Return the string with the first letter as lowercase
535
     *
536
     * @return self
537
     */
538 2
    public function lcfirst(): self
539
    {
540
        return $this
541 2
            ->substring(0, 1)
542 2
            ->toLower()
543 2
            ->append((string) $this->substring(1));
544
    }
545
546
    /**
547
     * Return a CamelCase representation of the string
548
     *
549
     * @return self
550
     */
551 2
    public function camelize(): self
552
    {
553
        return $this
554 2
            ->pregSplit('/_| /')
555 2
            ->map(function(self $part) {
556 2
                return $part->ucfirst();
557 2
            })
558 2
            ->join('')
559 2
            ->toEncoding($this->encoding);
560
    }
561
562
    /**
563
     * Append a string at the end of the current one
564
     *
565
     * @param string $string
566
     *
567
     * @return self
568
     */
569 8
    public function append(string $string): self
570
    {
571 8
        return new self((string) $this.$string, $this->encoding);
572
    }
573
574
    /**
575
     * Prepend a string at the beginning of the current one
576
     *
577
     * @param string $string
578
     *
579
     * @return self
580
     */
581 2
    public function prepend(string $string): self
582
    {
583 2
        return new self($string.(string) $this, $this->encoding);
584
    }
585
586
    /**
587
     * Check if the 2 strings are equal
588
     *
589
     * @param self $string
590
     *
591
     * @return bool
592
     */
593 56
    public function equals(self $string): bool
594
    {
595 56
        return (string) $this === (string) $string;
596
    }
597
598
    /**
599
     * Trim the string
600
     *
601
     * @param string $mask
602
     *
603
     * @return self
604
     */
605 2 View Code Duplication
    public function trim(string $mask = null): self
1 ignored issue
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...
606
    {
607 2
        return new self(
608 2
            $mask === null ? trim((string) $this) : trim((string) $this, $mask),
609 2
            $this->encoding
610
        );
611
    }
612
613
    /**
614
     * Trim the right side of the string
615
     *
616
     * @param string $mask
617
     *
618
     * @return self
619
     */
620 2 View Code Duplication
    public function rightTrim(string $mask = null): self
1 ignored issue
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...
621
    {
622 2
        return new self(
623 2
            $mask === null ? rtrim((string) $this) : rtrim((string) $this, $mask),
624 2
            $this->encoding
625
        );
626
    }
627
628
    /**
629
     * Trim the left side of the string
630
     *
631
     * @param string $mask
632
     *
633
     * @return self
634
     */
635 2 View Code Duplication
    public function leftTrim(string $mask = null): self
1 ignored issue
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...
636
    {
637 2
        return new self(
638 2
            $mask === null ? ltrim((string) $this) : ltrim((string) $this, $mask),
639 2
            $this->encoding
640
        );
641
    }
642
643
    /**
644
     * Check if the given string is present in the current one
645
     *
646
     * @param string $value
647
     *
648
     * @return bool
649
     */
650 4
    public function contains(string $value): bool
651
    {
652
        try {
653 4
            $this->position($value);
654
655 4
            return true;
656 2
        } catch (SubstringException $e) {
657 2
            return false;
658
        }
659
    }
660
661
    /**
662
     * Quote regular expression characters
663
     *
664
     * @param string $delimiter
665
     *
666
     * @return self
667
     */
668 2
    public function pregQuote(string $delimiter = ''): self
669
    {
670 2
        return new self(preg_quote((string) $this, $delimiter), $this->encoding);
671
    }
672
673
    /**
674
     * Pad the string
675
     *
676
     * @param int $length
677
     * @param string $character
678
     * @param int $direction
679
     *
680
     * @return self
681
     */
682 2
    private function pad(
683
        int $length,
684
        string $character = ' ',
685
        int $direction = self::PAD_RIGHT
686
    ): self {
687 2
        return new self(str_pad(
688 2
            $this->value,
689 2
            $length,
690 2
            $character,
691 2
            $direction
692 2
        ), $this->encoding);
693
    }
694
695 4
    private function usesDefaultEncoding(): bool
696
    {
697 4
        return $this->encoding === mb_internal_encoding();
698
    }
699
}
700