Set::__construct()   A
last analyzed

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\Argument\Type;
5
6
use Innmind\Compose\{
7
    Definition\Argument\Type,
8
    Exception\ValueNotSupported
9
};
10
use Innmind\Immutable\{
11
    SetInterface,
12
    Str
13
};
14
15
final class Set implements Type
16
{
17
    private const PATTERN = '~^set<(?<type>.+)>$~';
18
    private $type;
19
20 4
    public function __construct(string $type)
21
    {
22 4
        $this->type = $type;
23 4
    }
24
25
    /**
26
     * {@inheritdoc}
27
     */
28 2
    public function accepts($value): bool
29
    {
30 2
        if (!$value instanceof SetInterface) {
31 1
            return false;
32
        }
33
34 2
        return (string) $value->type() === $this->type;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 13
    public static function fromString(Str $value): Type
41
    {
42 13
        if (!$value->matches(self::PATTERN)) {
43 11
            throw new ValueNotSupported((string) $value);
44
        }
45
46 2
        $components = $value->capture(self::PATTERN);
47
48 2
        return new self((string) $components->get('type'));
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 1
    public function __toString(): string
55
    {
56 1
        return sprintf('set<%s>', $this->type);
57
    }
58
}
59