forked from Respect/StringFormatter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrimFormatter.php
More file actions
41 lines (35 loc) · 1.09 KB
/
TrimFormatter.php
File metadata and controls
41 lines (35 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?php
/*
* SPDX-FileCopyrightText: (c) Respect Project Contributors
* SPDX-License-Identifier: ISC
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
*/
declare(strict_types=1);
namespace Respect\StringFormatter;
use function in_array;
use function mb_ltrim;
use function mb_rtrim;
use function mb_trim;
use function sprintf;
final readonly class TrimFormatter implements Formatter
{
/** @param 'both'|'left'|'right' $side */
public function __construct(
private string $side = 'both',
private string|null $characters = null,
) {
if (!in_array($this->side, ['left', 'right', 'both'], true)) {
throw new InvalidFormatterException(
sprintf('Invalid side "%s". Must be "left", "right", or "both".', $this->side),
);
}
}
public function format(string $input): string
{
return match ($this->side) {
'left' => mb_ltrim($input, $this->characters),
'right' => mb_rtrim($input, $this->characters),
default => mb_trim($input, $this->characters),
};
}
}