Total Complexity | 7 |
Total Lines | 48 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
15 | final class Map implements Type |
||
16 | { |
||
17 | private const PATTERN = '~^map<(?<key>.+), ?(?<value>.+)>$~'; |
||
18 | private $key; |
||
19 | private $value; |
||
20 | |||
21 | 4 | public function __construct(string $key, string $value) |
|
25 | 4 | } |
|
26 | |||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | 2 | public function accepts($value): bool |
|
31 | { |
||
32 | 2 | if (!$value instanceof MapInterface) { |
|
33 | 1 | return false; |
|
34 | } |
||
35 | |||
36 | 2 | return (string) $value->keyType() === $this->key && |
|
37 | 2 | (string) $value->valueType() === $this->value; |
|
38 | } |
||
39 | |||
40 | /** |
||
41 | * {@inheritdoc} |
||
42 | */ |
||
43 | 15 | public static function fromString(Str $value): Type |
|
44 | { |
||
45 | 15 | if (!$value->matches(self::PATTERN)) { |
|
46 | 13 | throw new ValueNotSupported((string) $value); |
|
47 | } |
||
48 | |||
49 | 2 | $components = $value->capture(self::PATTERN); |
|
50 | |||
51 | 2 | return new self( |
|
52 | 2 | (string) $components->get('key'), |
|
53 | 2 | (string) $components->get('value') |
|
54 | ); |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | 1 | public function __toString(): string |
|
63 | } |
||
64 | } |
||
65 |