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

Constructors::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Definition\Service;
5
6
use Innmind\Compose\Exception\ValueNotSupported;
7
use Innmind\Immutable\{
8
    StreamInterface,
9
    Stream,
10
    Str
11
};
12
13
final class Constructors
14
{
15
    private static $defaults;
16
    private $constructors;
17
18 8
    public function __construct(string ...$constructors)
19
    {
20 8
        $constructors = Stream::of('string', ...$constructors);
21
22 8
        if ($constructors->size() === 0) {
23 7
            $constructors = self::defaults();
24
        }
25
26 8
        $this->constructors = $constructors;
27 8
    }
28
29 7
    public function load(Str $value): Constructor
30
    {
31 7
        foreach ($this->constructors as $constructor) {
32
            try {
33 7
                $build = $constructor.'::fromString';
34
35 7
                return $build($value);
36 6
            } catch (ValueNotSupported $e) {
37
                //pass
38
            }
39
        }
40
41 1
        throw new ValueNotSupported(var_export($value, true));
42
    }
43
44
    /**
45
     * @return StreamInterface<string>
46
     */
47 8
    public static function defaults(): StreamInterface
48
    {
49 8
        return self::$defaults ?? self::$defaults = Stream::of(
50 1
            'string',
51 1
            Constructor\Factory::class,
52 1
            Constructor\Set::class,
53 1
            Constructor\Stream::class,
54 1
            Constructor\Map::class,
55 8
            Constructor\Construct::class
56
        );
57
    }
58
}
59