Completed
Push — develop ( f8d1c0...c6d734 )
by Baptiste
02:31
created

Map::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
crap 1
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
};
10
use Innmind\Immutable\{
11
    Str,
12
    Map as ImmutableMap,
13
    Pair
14
};
15
16
final class Map implements Constructor
17
{
18
    private $key;
19
    private $value;
20
21 3
    private function __construct(string $key, string $value)
22
    {
23 3
        $this->key = $key;
24 3
        $this->value = $value;
25 3
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 5
    public static function fromString(Str $value): Constructor
31
    {
32 5
        if (!$value->matches('~^map<\S+, ?\S+>$~')) {
33 4
            throw new ValueNotSupported((string) $value);
34
        }
35
36 3
        $components = $value->capture('~^map<(?<key>\S+), ?(?<value>\S+)>$~');
37
38 3
        return new self(
39 3
            (string) $components->get('key'),
40 3
            (string) $components->get('value')
41
        );
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 2
    public function __invoke(...$arguments): object
48
    {
49 2
        return ImmutableMap::of(
50 2
            $this->key,
51 2
            $this->value,
52 2
            array_map(static function(Pair $pair) {
53 2
                return $pair->key();
54 2
            }, $arguments),
55 2
            array_map(static function(Pair $pair) {
56 2
                return $pair->value();
57 2
            }, $arguments)
58
        );
59
    }
60
}
61