Properties   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 3
A build() 0 11 3
A defaults() 0 10 1
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Config;
5
6
use Innmind\Config\Exception\{
7
    DomainException,
8
    SchemaNotParseable,
9
};
10
use Innmind\Immutable\{
11
    StreamInterface,
12
    Stream,
13
    Str,
14
};
15
16
final class Properties
17
{
18
    private static $defaults;
19
    private $properties;
20
21 122
    public function __construct(string ...$properties)
22
    {
23 122
        $properties = Stream::of('string', ...$properties);
24
25 122
        if ($properties->size() === 0) {
26 119
            $properties = self::defaults();
27
        }
28
29 122
        $properties->foreach(static function(string $property): void {
30 122
            $refl = new \ReflectionClass($property);
31
32 122
            if (!$refl->implementsInterface(Property::class)) {
33 1
                throw new DomainException($property);
34
            }
35 122
        });
36
37 121
        $this->properties = $properties;
38 121
    }
39
40 23
    public function build(Str $schema): Property
41
    {
42 23
        foreach ($this->properties as $property) {
43
            try {
44 23
                return [$property, 'build']($schema, $this);
45 7
            } catch (SchemaNotParseable $e) {
46
                //pass
47
            }
48
        }
49
50 3
        throw new SchemaNotParseable((string) $schema);
51
    }
52
53
    /**
54
     * @return StreamInterface<string>
55
     */
56 120
    public static function defaults(): StreamInterface
57
    {
58 120
        return self::$defaults ?? self::$defaults = Stream::of(
59 1
            'string',
60 1
            Property\Primitive::class,
61 1
            Property\Set::class,
62 1
            Property\Stream::class,
63 1
            Property\Sequence::class,
64 1
            Property\Mixed::class,
65 120
            Property\Enum::class
66
        );
67
    }
68
}
69