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

Primitive::fromString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Definition\Argument\Type;
5
6
use Innmind\Compose\{
7
    Definition\Argument\Type,
8
    Exception\NotAPrimitiveType,
9
    Exception\ValueNotSupported
10
};
11
use Innmind\Immutable\Str;
12
13
final class Primitive implements Type
14
{
15
    private $function;
16
17 20
    public function __construct(string $primitive)
18
    {
19 20
        if (!function_exists('is_'.$primitive)) {
20 3
            throw new NotAPrimitiveType($primitive);
21
        }
22
23 17
        $this->function = 'is_'.$primitive;
24 17
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 14
    public function accepts($value): bool
30
    {
31 14
        return ($this->function)($value);
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 4
    public static function fromString(Str $value): Type
38
    {
39
        try {
40 4
            return new self((string) $value);
41 2
        } catch (NotAPrimitiveType $e) {
42 2
            throw new ValueNotSupported((string) $value, 0, $e);
43
        }
44
    }
45
}
46