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

Arguments::load()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
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
};
11
12
final class Arguments
13
{
14
    private static $defaults;
15
    private $arguments;
16
17 55
    public function __construct(string ...$arguments)
18
    {
19 55
        $arguments = Stream::of('string', ...$arguments);
20
21 55
        if ($arguments->size() === 0) {
22 54
            $arguments = self::defaults();
23
        }
24
25 55
        $this->arguments = $arguments;
26 55
    }
27
28
    /**
29
     * @param mixed $value
30
     */
31 19
    public function load($value): Argument
32
    {
33 19
        foreach ($this->arguments as $argument) {
34
            try {
35 19
                $build = $argument.'::fromValue';
36
37 19
                return $build($value, $this);
38 17
            } catch (ValueNotSupported $e) {
39
                //pass
40
            }
41
        }
42
43 1
        throw new ValueNotSupported(var_export($value, true));
44
    }
45
46
    /**
47
     * @return StreamInterface<string>
48
     */
49 55
    public static function defaults(): StreamInterface
50
    {
51 55
        return self::$defaults ?? self::$defaults = Stream::of(
52 1
            'string',
53 1
            Argument\Decorate::class,
54 1
            Argument\Reference::class,
55 1
            Argument\Unwind::class,
56 1
            Argument\Pair::class,
57 55
            Argument\Primitive::class
58
        );
59
    }
60
}
61