Completed
Push — master ( bb13e5...2727f3 )
by Dominik
02:26
created

ObjectNode::serialize()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 13
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 2
b 1
f 0
cc 3
eloc 7
nc 4
nop 0
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
     * @return ObjectNode
12
     */
13 17
    public static function create(bool $allowSerializeEmpty = false): ObjectNode
14
    {
15 17
        $node = new self;
16 17
        $node->allowSerializeEmpty = $allowSerializeEmpty;
17
18 17
        return $node;
19
    }
20
21
    /**
22
     * @param string       $key
23
     * @param AbstractNode $node
24
     *
25
     * @return $this
26
     *
27
     * @throws \InvalidArgumentException
28
     */
29 17
    public function add($key, AbstractNode $node)
30
    {
31 17
        if (isset($this->children[$key])) {
32 1
            throw new \InvalidArgumentException(sprintf('There is already a node with key %s!', $key));
33
        }
34
35 17
        $node->setParent($this);
36
37 17
        $this->children[$key] = $node;
38
39 17
        return $this;
40
    }
41
42
    /**
43
     * @return \stdClass
44
     */
45 1
    public function serializeEmpty(): \stdClass
46
    {
47 1
        return new \stdClass();
48
    }
49
50
    /**
51
     * @return \stdClass|null
52
     */
53 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...
54
    {
55 16
        $serialized = new \stdClass();
56 16
        foreach ($this->children as $key => $child) {
57 16
            $this->serializeChild($serialized, $key, $child);
58
        }
59
60 16
        if ([] === (array) $serialized) {
61 3
            return;
62
        }
63
64 14
        return $serialized;
65
    }
66
67
    /**
68
     * @param \stdClass         $serialized
69
     * @param string            $key
70
     * @param AbstractNode $child
71
     */
72 16
    private function serializeChild(\stdClass $serialized, string $key, AbstractNode $child)
73
    {
74 16
        if (null !== $serializedChild = $child->serialize()) {
75 14
            $serialized->$key = $serializedChild;
76 4
        } elseif ($child->isAllowSerializeEmpty()) {
77 2
            $serialized->$key = $child->serializeEmpty();
78
        }
79 16
    }
80
81
    /**
82
     * @param boolean $beautify
83
     * @return string
84
     */
85 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...
86
    {
87 16
        if (null === $serialized = $this->serialize()) {
88 2
            return '';
89
        }
90
91 14
        if ($beautify) {
92 1
            return json_encode($serialized, JSON_PRETTY_PRINT);
93
        }
94
95 13
        return json_encode($serialized);
96
    }
97
}
98