Sequence   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
D process() 0 29 9
A build() 0 7 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Config\Property;
5
6
use Innmind\Config\{
7
    Property,
8
    Properties,
9
    Exception\SchemaNotParseable,
10
    Exception\InvalidArgumentException,
11
};
12
use Innmind\Immutable;
13
14
final class Sequence implements Property
15
{
16
    private const PATTERN = '~^sequence\+?$~';
17
18
    private $requiresValue = false;
19
20 14
    private function __construct(bool $requiresValue)
21
    {
22 14
        $this->requiresValue = $requiresValue;
23 14
    }
24
25 17
    public static function build(Immutable\Str $schema, Properties $properties): Property
26
    {
27 17
        if (!$schema->matches(self::PATTERN)) {
28 3
            throw new SchemaNotParseable((string) $schema);
29
        }
30
31 14
        return new self((string) $schema->substring(-1) === '+');
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 11
    public function process($value)
38
    {
39 11
        if (is_null($value)) {
40 2
            if ($this->requiresValue) {
41 1
                throw new InvalidArgumentException;
42
            }
43
44 1
            return new Immutable\Sequence;
45
        }
46
47 9
        if ($value instanceof Immutable\Sequence) {
48 4
            if ($this->requiresValue && $value->size() === 0) {
49 1
                throw new InvalidArgumentException;
50
            }
51
52 3
            return $value;
53
        }
54
55 5
        if (!is_array($value)) {
56 1
            throw new InvalidArgumentException;
57
        }
58
59 4
        $sequence = Immutable\Sequence::of(...$value);
60
61 4
        if ($this->requiresValue && $sequence->size() === 0) {
62 1
            throw new InvalidArgumentException;
63
        }
64
65 3
        return $sequence;
66
    }
67
}
68