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

Map   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
dl 0
loc 42
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A fromString() 0 11 2
A __construct() 0 4 1
A __invoke() 0 11 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