Completed
Push — master ( 867312...c0e120 )
by Dominik
02:57
created

QueryBuilder::scalarNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Saxulum\ElasticSearchQueryBuilder;
4
5
use Saxulum\ElasticSearchQueryBuilder\Node\AbstractNode;
6
use Saxulum\ElasticSearchQueryBuilder\Node\ArrayNode;
7
use Saxulum\ElasticSearchQueryBuilder\Node\ObjectNode;
8
use Saxulum\ElasticSearchQueryBuilder\Node\ScalarNode;
9
10
class QueryBuilder
11
{
12
    /**
13
     * @var ObjectNode
14
     */
15
    protected $rootNode;
16
17
    /**
18
     * @var AbstractNode
19
     */
20
    protected $node;
21 17
22
    public function __construct()
23 17
    {
24 17
        $this->rootNode = new ObjectNode();
25 17
        $this->node = $this->rootNode;
26
    }
27
28
    /**
29
     * @param AbstractNode $node
30
     * @param bool         $allowDefault
31
     *
32
     * @return $this
33
     *
34
     * @throws \Exception
35 3
     */
36 View Code Duplication
    public function addToArrayNode(AbstractNode $node, $allowDefault = false)
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...
37 3
    {
38 1
        if (!$this->node instanceof ArrayNode) {
39
            throw new \Exception(sprintf('You cannot call %s on node type: %s', __FUNCTION__, get_class($this->node)));
40
        }
41 2
42 2
        $this->node->add($node, $allowDefault);
43
        $this->reassignParent($node);
44 2
45
        return $this;
46
    }
47
48
    /**
49
     * @param string       $key
50
     * @param AbstractNode $node
51
     * @param bool         $allowDefault
52
     *
53
     * @return $this
54
     *
55
     * @throws \Exception
56 16
     */
57 View Code Duplication
    public function addToObjectNode($key, AbstractNode $node, $allowDefault = false)
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...
58 16
    {
59 1
        if (!$this->node instanceof ObjectNode) {
60
            throw new \Exception(sprintf('You cannot call %s on node type: %s', __FUNCTION__, get_class($this->node)));
61
        }
62 16
63 16
        $this->node->add($key, $node, $allowDefault);
64
        $this->reassignParent($node);
65 16
66
        return $this;
67
    }
68
69
    /**
70
     * @param AbstractNode $node
71 16
     */
72
    protected function reassignParent(AbstractNode $node)
73 16
    {
74 16
        if ($node instanceof ArrayNode || $node instanceof ObjectNode) {
75 16
            $this->node = $node;
76 16
        }
77
    }
78
79
    /**
80
     * @return $this
81 1
     */
82
    public function end()
83 1
    {
84
        $this->node = $this->node->getParent();
85 1
86
        return $this;
87
    }
88
89
    /**
90
     * @return ArrayNode
91 15
     */
92
    public function arrayNode(): ArrayNode
93 15
    {
94
        return new ArrayNode();
95
    }
96
97
    /**
98
     * @return ObjectNode
99
     */
100 15
    public function objectNode(): ObjectNode
101
    {
102 15
        return new ObjectNode();
103 1
    }
104
105
    /**
106 14
     * @param null $value
107 1
     * @return ScalarNode
108
     */
109
    public function scalarNode($value = null): ScalarNode
110 13
    {
111
        return new ScalarNode($value);
112
    }
113
114
    /**
115
     * @return \stdClass|null
116
     */
117
    public function serialize()
118
    {
119
        return $this->rootNode->serialize();
120
    }
121
122
    /**
123
     * @param boolean $beautify
124
     * @return string
125
     */
126
    public function json($beautify = false)
127
    {
128
        if (null === $serialized = $this->serialize()) {
129
            return '';
130
        }
131
132
        if ($beautify) {
133
            return json_encode($serialized, JSON_PRETTY_PRINT);
134
        }
135
136
        return json_encode($serialized);
137
    }
138
}
139