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

Pair::resolve()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 2
crap 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Definition\Service\Argument;
5
6
use Innmind\Compose\{
7
    Definition\Service\Argument,
8
    Definition\Service\Arguments,
9
    Definitions,
10
    Exception\ValueNotSupported,
11
    Exception\LogicException
12
};
13
use Innmind\Immutable\{
14
    Pair as ImmutablePair,
15
    StreamInterface,
16
    Str
17
};
18
19
final class Pair implements Argument
20
{
21
    private $key;
22
    private $value;
23
24 6
    private function __construct(Argument $key, Argument $value)
25
    {
26 6
        if ($key instanceof Unwind || $value instanceof Unwind) {
27 2
            throw new LogicException;
28
        }
29
30 4
        $this->key = $key;
31 4
        $this->value = $value;
32 4
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 11
    public static function fromValue($value, Arguments $arguments): Argument
38
    {
39 11
        if (!is_string($value)) {
40 5
            throw new ValueNotSupported;
41
        }
42
43 8
        $value = Str::of($value);
44
45 8
        if (!$value->matches('~^<\S+, ?\S+>$~')) {
46 7
            throw new ValueNotSupported((string) $value);
47
        }
48
49 6
        $components = $value->capture('~^<(?<key>\S+), ?(?<value>\S+)>$~');
50
51 6
        return new self(
52 6
            $arguments->load((string) $components->get('key')),
53 6
            $arguments->load((string) $components->get('value'))
54
        );
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 2
    public function resolve(
61
        StreamInterface $built,
62
        Definitions $definitions
63
    ): StreamInterface {
64
        $key = $this
65 2
            ->key
66 2
            ->resolve(
67 2
                $built->clear(),
68 2
                $definitions
69
            )
70 2
            ->current();
71
        $value = $this
72 2
            ->value
73 2
            ->resolve(
74 2
                $built->clear(),
75 2
                $definitions
76
            )
77 2
            ->current();
78
79 2
        return $built->add(new ImmutablePair($key, $value));
80
    }
81
}
82