Total Complexity | 9 |
Total Lines | 47 |
Duplicated Lines | 0 % |
Coverage | 94.74% |
Changes | 0 |
1 | <?php |
||
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) |
|
61 | } |
||
62 | } |
||
63 |