Completed
Push — develop ( 15ec3c...8b62a8 )
by Baptiste
02:19
created

Set::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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