ObjectNode::serializeChild()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 3
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Saxulum\ElasticSearchQueryBuilder\Node;
6
7
final class ObjectNode extends AbstractParentNode implements ObjectNodeSerializeInterface
8
{
9
    /**
10
     * @param bool $allowSerializeEmpty
11
     *
12
     * @return ObjectNode
13
     */
14 18
    public static function create(bool $allowSerializeEmpty = false): ObjectNode
15
    {
16 18
        $node = new self();
17 18
        $node->allowSerializeEmpty = $allowSerializeEmpty;
18
19 18
        return $node;
20
    }
21
22 1 View Code Duplication
    public function __clone()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
23
    {
24 1
        parent::__clone();
25
26 1
        $children = $this->children;
27
28 1
        $this->children = [];
29
30 1
        foreach ($children as $key => $node) {
31 1
            $this->add($key, clone $node);
32
        }
33 1
    }
34
35
    /**
36
     * @param string       $key
37
     * @param AbstractNode $node
38
     *
39
     * @return $this
40
     *
41
     * @throws \InvalidArgumentException
42
     */
43 18
    public function add(string $key, AbstractNode $node)
44
    {
45 18
        if (isset($this->children[$key])) {
46 1
            throw new \InvalidArgumentException(sprintf('There is already a node with key %s!', $key));
47
        }
48
49 18
        $node->setParent($this);
50
51 18
        $this->children[$key] = $node;
52
53 18
        return $this;
54
    }
55
56
    /**
57
     * @return \stdClass
58
     */
59 1
    public function serializeEmpty(): \stdClass
60
    {
61 1
        return new \stdClass();
62
    }
63
64
    /**
65
     * @return \stdClass|null
66
     */
67 16 View Code Duplication
    public function serialize()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
68
    {
69 16
        $serialized = new \stdClass();
70 16
        foreach ($this->children as $key => $child) {
71 16
            $this->serializeChild($serialized, $key, $child);
72
        }
73
74 16
        if ([] === (array) $serialized) {
75 3
            return;
76
        }
77
78 14
        return $serialized;
79
    }
80
81
    /**
82
     * @param \stdClass    $serialized
83
     * @param string       $key
84
     * @param AbstractNode $child
85
     */
86 16
    private function serializeChild(\stdClass $serialized, $key, AbstractNode $child)
87
    {
88 16
        if (null !== $serializedChild = $child->serialize()) {
89 14
            $serialized->$key = $serializedChild;
90 4
        } elseif ($child->isAllowSerializeEmpty()) {
91 2
            $serialized->$key = $child->serializeEmpty();
92
        }
93 16
    }
94
95
    /**
96
     * @param bool $beautify
97
     *
98
     * @return string
99
     */
100 16 View Code Duplication
    public function json(bool $beautify = false): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
101
    {
102 16
        if (null === $serialized = $this->serialize()) {
103 2
            return '';
104
        }
105
106 14
        if ($beautify) {
107 1
            return json_encode($serialized, JSON_PRETTY_PRINT);
108
        }
109
110 13
        return json_encode($serialized);
111
    }
112
}
113