Completed
Push — master ( c0e120...9cce18 )
by Dominik
03:38 queued 01:16
created

QueryBuilder::scalarNode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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