Completed
Push — develop ( 2c61cc...b9b8dc )
by Baptiste
02:42
created

Types   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A defaults() 0 10 1
A load() 0 13 3
A __construct() 0 9 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Definition\Argument;
5
6
use Innmind\Compose\Exception\ValueNotSupported;
7
use Innmind\Immutable\{
8
    StreamInterface,
9
    Stream,
10
    Str
11
};
12
13
final class Types
14
{
15
    private static $defaults;
16
    private $types;
17
18 7
    public function __construct(string ...$types)
19
    {
20 7
        $types = Stream::of('string', ...$types);
21
22 7
        if ($types->size() === 0) {
23 6
            $types = self::defaults();
24
        }
25
26 7
        $this->types = $types;
27 7
    }
28
29 7
    public function load(Str $value): Type
30
    {
31 7
        foreach ($this->types as $type) {
32
            try {
33 7
                $build = $type.'::fromString';
34
35 7
                return $build($value);
36 6
            } catch (ValueNotSupported $e) {
37
                //pass
38
            }
39
        }
40
41 1
        throw new ValueNotSupported((string) $value);
42
    }
43
44
    /**
45
     * @return StreamInterface<string>
46
     */
47 7
    public static function defaults(): StreamInterface
48
    {
49 7
        return self::$defaults ?? self::$defaults = Stream::of(
50 1
            'string',
51 1
            Type\Map::class,
52 1
            Type\Sequence::class,
53 1
            Type\Set::class,
54 1
            Type\Stream::class,
55 1
            Type\Primitive::class,
56 7
            Type\Instance::class
57
        );
58
    }
59
}
60