Completed
Branch master (a93247)
by Dominik
04:14 queued 01:56
created

ObjectNode::remove()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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
     * @return $this
11
     */
12
    public function add($name, AbstractNode $node)
13
    {
14
        if (null !== $this->reflectionProperty->getValue($node)) {
15
            throw new \InvalidArgumentException('Node already got a parent!');
16
        }
17
18
        if (array_key_exists($name, $this->children)) {
19
            throw new \InvalidArgumentException(sprintf('There is allready a child for name %s', $name));
20
        }
21
22
        $this->reflectionProperty->setValue($node, $this);
23
        
24
        $this->children[$name] = $node;
25
26
        return $this;
27
    }
28
29
    /**
30
     * @return \stdClass|null
31
     */
32
    public function serialize()
33
    {
34
        $serialized = new \stdClass();
35 View Code Duplication
        foreach ($this->children as $name => $child) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
            if (null !== $serializedChild = $child->serialize()) {
37
                $serialized->$name = $serializedChild;
38
            }
39
        }
40
41
        if (!$this->allowEmpty && [] === (array) $serialized) {
42
            return null;
43
        }
44
45
        return $serialized;
46
    }
47
}
48