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

Arguments   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 46
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 2
A load() 0 13 3
A defaults() 0 9 1
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