Completed
Push — master ( 8b0a23...1e5206 )
by Dominik
01:59
created

ArrayNode::add()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Saxulum\ElasticSearchQueryBuilder\Node;
4
5
class ArrayNode extends AbstractParentNode
6
{
7
    /**
8
     * @param AbstractNode $node
9
     *
10
     * @return $this
11
     */
12
    public function add(AbstractNode $node)
13
    {
14
        $node->setParent($this);
15
16
        $this->children[] = $node;
17
18
        return $this;
19
    }
20
21
    /**
22
     * @return array
23
     */
24
    public function getAddDefault()
25
    {
26
        return [];
27
    }
28
29
    /**
30
     * @return array|null
31
     */
32
    public function serialize()
33
    {
34
        $serialized = [];
35
        foreach ($this->children as $child) {
36
            if (null !== $serializedChild = $child->serialize()) {
37
                $serialized[] = $serializedChild;
38
            } elseif ($child->allowAddDefault()) {
39
                $serialized[] = $child->getAddDefault();
40
            }
41
        }
42
43
        if ([] === $serialized) {
44
            return;
45
        }
46
47
        return $serialized;
48
    }
49
}
50