Completed
Branch master (d78aad)
by Dominik
04:36 queued 02:12
created

ScalarToNodeConverter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 39
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A convert() 0 18 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Saxulum\ElasticSearchQueryBuilder\Converter;
6
7
use Saxulum\ElasticSearchQueryBuilder\Node\AbstractNode;
8
use Saxulum\ElasticSearchQueryBuilder\Node\BoolNode;
9
use Saxulum\ElasticSearchQueryBuilder\Node\FloatNode;
10
use Saxulum\ElasticSearchQueryBuilder\Node\IntNode;
11
use Saxulum\ElasticSearchQueryBuilder\Node\NullNode;
12
use Saxulum\ElasticSearchQueryBuilder\Node\StringNode;
13
14
final class ScalarToNodeConverter implements ScalarToNodeConverterInterface
15
{
16
    /**
17
     * @var array
18
     */
19
    private $typeMapping = [
20
        'boolean' => BoolNode::class,
21
        'double' => FloatNode::class,
22
        'integer' => IntNode::class,
23
        'string' => StringNode::class,
24
    ];
25
26
    /**
27
     * @param bool|float|int|null|string $value
28
     * @param string                     $path
29
     *
30
     * @return AbstractNode
31
     *
32
     * @throws \InvalidArgumentException
33
     */
34 6
    public function convert($value, string $path = ''): AbstractNode
35
    {
36 6
        if (null === $value) {
37 1
            return NullNode::create();
38
        }
39
40 5
        $type = gettype($value);
41
42 5
        if (!isset($this->typeMapping[$type])) {
43 1
            throw new \InvalidArgumentException(
44 1
                sprintf('Type %s is not supported, at path %s', is_object($value) ? get_class($value) : $type, $path)
45
            );
46
        }
47
48 4
        $node = $this->typeMapping[$type];
49
50 4
        return $node::create($value);
51
    }
52
}
53