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