1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Compose\Definition\Service\Constructor; |
5
|
|
|
|
6
|
|
|
use Innmind\Compose\{ |
7
|
|
|
Definition\Service\Constructor, |
8
|
|
|
Exception\ValueNotSupported, |
9
|
|
|
Lazy\Map as LazyMap, |
10
|
|
|
Compilation\Service\Constructor as CompiledConstructor, |
11
|
|
|
Compilation\Service\Constructor\Map as CompiledMap, |
12
|
|
|
Compilation\Service\Argument as CompiledArgument |
13
|
|
|
}; |
14
|
|
|
use Innmind\Immutable\Str; |
15
|
|
|
|
16
|
|
|
final class Map implements Constructor |
17
|
|
|
{ |
18
|
|
|
private $key; |
19
|
|
|
private $value; |
20
|
|
|
|
21
|
9 |
|
private function __construct(string $key, string $value) |
22
|
|
|
{ |
23
|
9 |
|
$this->key = $key; |
24
|
9 |
|
$this->value = $value; |
25
|
9 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
13 |
|
public static function fromString(Str $value): Constructor |
31
|
|
|
{ |
32
|
13 |
|
if (!$value->matches('~^map<\S+, ?\S+>$~')) { |
33
|
9 |
|
throw new ValueNotSupported((string) $value); |
34
|
|
|
} |
35
|
|
|
|
36
|
9 |
|
$components = $value->capture('~^map<(?<key>\S+), ?(?<value>\S+)>$~'); |
37
|
|
|
|
38
|
9 |
|
return new self( |
39
|
9 |
|
(string) $components->get('key'), |
40
|
9 |
|
(string) $components->get('value') |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
3 |
|
public function __invoke(...$arguments): object |
48
|
|
|
{ |
49
|
3 |
|
return LazyMap::of($this->key, $this->value, ...$arguments); |
50
|
|
|
} |
51
|
|
|
|
52
|
3 |
|
public function compile(CompiledArgument ...$arguments): CompiledConstructor |
53
|
|
|
{ |
54
|
3 |
|
return new CompiledMap($this->key, $this->value, ...$arguments); |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
public function __toString(): string |
58
|
|
|
{ |
59
|
2 |
|
return sprintf('map<%s, %s>', $this->key, $this->value); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|