Total Complexity | 9 |
Total Lines | 65 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
16 | final class UnicodeString implements StringInterface |
||
17 | { |
||
18 | |||
19 | private const MIN_CHAR = 0x00; |
||
20 | |||
21 | private const MAX_ASCII_CHAR = 0x7F; |
||
22 | |||
23 | private const MAX_UNICODE_CHAR = 0x10FFFF; |
||
24 | |||
25 | /** |
||
26 | * @var int[] |
||
27 | * @psalm-var array<int, int> |
||
28 | */ |
||
29 | private $characters = []; |
||
30 | |||
31 | 13 | public function __construct(int ...$characters) |
|
32 | { |
||
33 | 13 | foreach ($characters as $character) { |
|
34 | 10 | if ($character > self::MAX_UNICODE_CHAR || $character < self::MIN_CHAR) { |
|
35 | 2 | throw new Exception\NonUnicodeCharacterException($character); |
|
36 | } |
||
37 | 8 | $this->characters[] = $character; |
|
38 | } |
||
39 | 11 | } |
|
40 | |||
41 | /** |
||
42 | * @return int[] |
||
43 | * @psalm-return array<int, int> |
||
44 | * @psalm-pure |
||
45 | */ |
||
46 | 3 | public function asCharacters(): array |
|
47 | { |
||
48 | 3 | return $this->characters; |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * @return string |
||
53 | * @psalm-pure |
||
54 | */ |
||
55 | 4 | public function asAscii(): string |
|
56 | { |
||
57 | 4 | return implode(array_map([$this, 'asciiChr'], $this->characters)); |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param int $character |
||
62 | * @return string |
||
63 | * @psalm-pure |
||
64 | */ |
||
65 | 3 | private function asciiChr(int $character): string |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * @return string |
||
76 | * @psalm-pure |
||
77 | */ |
||
78 | 4 | public function asUtf8(): string |
|
83 |