Completed
Branch master (9de2b1)
by Dominik
03:28 queued 01:13
created

ObjectNode   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 17
Bugs 3 Features 3
Metric Value
wmc 8
c 17
b 3
f 3
lcom 1
cbo 2
dl 0
loc 51
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 13 2
A getDefault() 0 4 1
B serialize() 0 17 5
1
<?php
2
3
namespace Saxulum\ElasticSearchQueryBuilder\Node;
4
5
class ObjectNode extends AbstractParentNode
6
{
7
    /**
8
     * @param string       $name
9
     * @param AbstractNode $node
10
     *
11
     * @return $this
12
     */
13
    public function add($name, AbstractNode $node, $allowDefault = false)
14
    {
15
        if (isset($this->children[$name])) {
16
            throw new \InvalidArgumentException(sprintf('There is already a node with name %s!', $name));
17
        }
18
19
        $node->setParent($this);
20
21
        $this->children[$name] = $node;
22
        $this->allowDefault[$name] = $allowDefault;
23
24
        return $this;
25
    }
26
27
    /**
28
     * @return array
29
     */
30
    public function getDefault()
31
    {
32
        return new \stdClass();
33
    }
34
35
    /**
36
     * @return \stdClass|null
37
     */
38
    public function serialize()
39
    {
40
        $serialized = new \stdClass();
41
        foreach ($this->children as $name => $child) {
42
            if (null !== $serializedChild = $child->serialize()) {
43
                $serialized->$name = $serializedChild;
44
            } elseif ($this->allowDefault[$name]) {
45
                $serialized->$name = $child->getDefault();
46
            }
47
        }
48
49
        if ([] === (array) $serialized) {
50
            return;
51
        }
52
53
        return $serialized;
54
    }
55
}
56