IteratableToNodeConverter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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\AbstractParentNode;
9
use Saxulum\ElasticSearchQueryBuilder\Node\ArrayNode;
10
use Saxulum\ElasticSearchQueryBuilder\Node\ObjectNode;
11
12
final class IteratableToNodeConverter implements IteratableToNodeConverterInterface
13
{
14
    /**
15
     * @var ScalarToNodeConverterInterface
16
     */
17
    private $scalarToNodeConverter;
18
19
    /**
20
     * @param ScalarToNodeConverterInterface $scalarToNodeConverter
21
     */
22 4
    public function __construct(ScalarToNodeConverterInterface $scalarToNodeConverter)
23
    {
24 4
        $this->scalarToNodeConverter = $scalarToNodeConverter;
25 4
    }
26
27
    /**
28
     * @param array|\Traversable $data
29
     * @param string             $path
30
     * @param bool               $allowSerializeEmpty
31
     *
32
     * @return AbstractParentNode
33
     *
34
     * @throws \InvalidArgumentException
35
     */
36 4
    public function convert($data, string $path = '', bool $allowSerializeEmpty = false): AbstractParentNode
37
    {
38 4
        if (!is_array($data) && !$data instanceof \Traversable) {
39 1
            throw new \InvalidArgumentException(sprintf('Params need to be array or %s', \Traversable::class));
40
        }
41
42 3
        $isArray = $this->isArray($data);
43 3
        $parentNode = $this->getParentNode($isArray, $allowSerializeEmpty);
44
45 3
        foreach ($data as $key => $value) {
46 3
            $this->addChildNode($parentNode, $key, $value, $path, $isArray, $allowSerializeEmpty);
47
        }
48
49 2
        return $parentNode;
50
    }
51
52
    /**
53
     * @param array|\Traversable $data
54
     *
55
     * @return bool
56
     */
57 3
    private function isArray($data): bool
58
    {
59 3
        $counter = 0;
60 3
        foreach ($data as $key => $value) {
61 3
            if ($key !== $counter) {
62 3
                return false;
63
            }
64
65 2
            ++$counter;
66
        }
67
68 3
        return true;
69
    }
70
71
    /**
72
     * @param bool $isArray
73
     * @param bool $allowSerializeEmpty
74
     *
75
     * @return AbstractParentNode
76
     */
77 3
    private function getParentNode(bool $isArray, bool $allowSerializeEmpty): AbstractParentNode
78
    {
79 3
        if ($isArray) {
80 3
            return ArrayNode::create($allowSerializeEmpty);
81
        }
82
83 3
        return ObjectNode::create($allowSerializeEmpty);
84
    }
85
86
    /**
87
     * @param AbstractParentNode $parentNode
88
     * @param int|string         $key
89
     * @param mixed              $value
90
     * @param string             $path
91
     * @param bool               $isArray
92
     * @param bool               $allowSerializeEmpty
93
     */
94 3
    private function addChildNode(
95
        AbstractParentNode $parentNode,
96
        $key,
97
        $value,
98
        string $path,
99
        bool $isArray,
100
        bool $allowSerializeEmpty
101
    ) {
102 3
        $subPath = $this->getSubPath($path, $key, $isArray);
103 3
        $node = $this->getNode($value, $subPath, $allowSerializeEmpty);
104
105 2
        if ($isArray) {
106 1
            $parentNode->add($node);
107
        } else {
108 2
            $parentNode->add((string) $key, $node);
109
        }
110 2
    }
111
112
    /**
113
     * @param string     $path
114
     * @param string|int $key
115
     * @param bool       $isArray
116
     *
117
     * @return string
118
     */
119 3
    private function getSubPath(string $path, $key, bool $isArray): string
120
    {
121 3
        $key = (string) $key;
122
123 3
        if ($isArray) {
124 2
            return $path.'['.$key.']';
125
        }
126
127 3
        return '' !== $path ? $path.'.'.$key : $key;
128
    }
129
130
    /**
131
     * @param mixed  $value
132
     * @param string $path
133
     * @param bool   $allowSerializeEmpty
134
     *
135
     * @return AbstractNode
136
     */
137 3
    private function getNode($value, string $path, bool $allowSerializeEmpty): AbstractNode
138
    {
139 3
        if (is_array($value) || $value instanceof \Traversable) {
140 3
            return $this->convert($value, $path, $allowSerializeEmpty);
141
        }
142
143 2
        return $this->scalarToNodeConverter->convert($value, $path, $allowSerializeEmpty);
0 ignored issues
show
Unused Code introduced by
The call to ScalarToNodeConverterInterface::convert() has too many arguments starting with $allowSerializeEmpty.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
144
    }
145
}
146