Primitive   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

Changes 0
Metric Value
dl 0
loc 47
ccs 18
cts 19
cp 0.9474
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 11 4
A build() 0 19 4
A __construct() 0 3 1
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\Str;
13
14
final class Primitive implements Property
15
{
16
    private const PATTERN = '~^\??(?<type>.+)$~';
17
18
    private $function;
19
    private $optional = false;
20
21 43
    private function __construct(string $type)
22
    {
23 43
        $this->function = 'is_'.$type;
24 43
    }
25
26 49
    public static function build(Str $schema, Properties $properties): Property
27
    {
28 49
        if (!$schema->matches(self::PATTERN)) {
29
            throw new SchemaNotParseable((string) $schema);
30
        }
31
32 49
        $type = (string) $schema->capture(self::PATTERN)->get('type');
33
34 49
        if (!function_exists('is_'.$type)) {
35 8
            throw new SchemaNotParseable((string) $schema);
36
        }
37
38 43
        $self = new self($type);
39
40 43
        if ((string) $schema->substring(0, 1) === '?') {
41 14
            $self->optional = true;
42
        }
43
44 43
        return $self;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 20
    public function process($value)
51
    {
52 20
        if (is_null($value) && $this->optional) {
53 6
            return null;
54
        }
55
56 14
        if (!($this->function)($value)) {
57 4
            throw new InvalidArgumentException;
58
        }
59
60 12
        return $value;
61
    }
62
}
63