Completed
Push — master ( 2bc3d9...4d44a5 )
by Sergey
15:01
created

QueryBuilder::where()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
3
namespace Isswp101\Persimmon\QueryBuilder;
4
5
class QueryBuilder
6
{
7
    /**
8
     * Query.
9
     *
10
     * @var array
11
     */
12
    protected $query = [];
13
14
    /**
15
     * Constructor.
16
     *
17
     * @param array $body
18
     */
19
    public function __construct(array $body = [])
20
    {
21
        $this->query['body'] = $body;
22
    }
23
24
    /**
25
     * @param int $from
26
     * @return $this
27
     */
28
    public function from($from)
29
    {
30
        $this->query['from'] = $from;
31
        return $this;
32
    }
33
34
    /**
35
     * @param int $size
36
     * @return $this
37
     */
38
    public function size($size)
39
    {
40
        $this->query['size'] = $size;
41
        return $this;
42
    }
43
44
    /**
45
     * @return bool
46
     */
47
    public function hasSort()
48
    {
49
        return !empty($this->query['body']['sort']);
50
    }
51
52
    /**
53
     * @param array|string $sort
54
     * @return $this
55
     */
56
    public function sort($sort)
57
    {
58
        $this->query['body']['sort'] = $sort;
59
        return $this;
60
    }
61
62
    /**
63
     * @param array|Filter $filter
64
     * @param string $mode
65
     * @return $this
66
     */
67
    public function filter($filter = [], $mode = 'include')
68
    {
69
        $map = [
70
            'include' => 'must',
71
            'exclude' => 'must_not',
72
            'should' => 'should'
73
        ];
74
        $mode = $map[$mode];
75
76
        if (!$filter) {
77
            unset($this->query['body']['filter']);
78
            return $this;
79
        }
80
81
        if ($filter instanceof Filter) {
82
            $filter = $filter->makeQuery();
83
        }
84
85
        $this->merge($filter, $mode);
86
87
        return $this;
88
    }
89
90
    /**
91
     * Specify the fields that will be retrieved when searching.
92
     *
93
     * @param mixed $fields
94
     * @return $this
95
     */
96
    public function fields($fields = false)
97
    {
98
        if ($fields === false) {
99
            $this->query['body']['_source'] = false;
100
        } elseif ((array)$fields == ['*']) {
101
            unset($this->query['body']['_source']);
102
        } else {
103
            $this->query['body']['_source'] = $fields;
104
        }
105
        return $this;
106
    }
107
108
    /**
109
     * Return only ids.
110
     *
111
     * @return $this
112
     */
113
    public function getOnlyIds()
114
    {
115
        return $this->fields(false);
116
    }
117
118
    /**
119
     * Return query.
120
     *
121
     * @return array
122
     */
123
    protected function getQuery()
124
    {
125
        return $this->query;
126
    }
127
128
    /**
129
     * Build query.
130
     *
131
     * @return array
132
     */
133
    public function build()
134
    {
135
        return $this->getQuery();
136
    }
137
138
    /**
139
     * @return string
140
     */
141
    public function __toString()
142
    {
143
        return json_encode($this->getQuery(), JSON_PRETTY_PRINT);
144
    }
145
146
    /**
147
     * @return string
148
     */
149
    public function toJson()
150
    {
151
        return json_encode($this->getQuery());
152
    }
153
154
    protected function merge(array $query, $mode = 'must')
155
    {
156
        if (!array_key_exists('filter', $this->query['body'])) {
157
            $this->query['body']['filter']['bool'][$mode] = [];
158
        }
159
160
        $this->query['body']['filter']['bool'][$mode][] = $query;
161
        return $this;
162
    }
163
164
    public function aggregation(Aggregation $aggregation)
165
    {
166
        $this->query['body']['aggs'][$aggregation->getName()] = $aggregation->make();
167
        return $this;
168
    }
169
170
    public function where($field, $value)
171
    {
172
        $query = ['term' => [$field => $value]];
173
        $this->merge($query);
174
        return $this;
175
    }
176
177
    public function notWhere($field, $value)
178
    {
179
        $query = ['term' => [$field => $value]];
180
        $this->merge($query, 'must_not');
181
        return $this;
182
    }
183
184
    public function orWhere($field, $value)
185
    {
186
        $query = ['term' => [$field => $value]];
187
        $this->merge($query, 'should');
188
        return $this;
189
    }
190
191
    public function between($field, $start, $end)
192
    {
193
        $query = ['range' => [$field => ['gt' => $start, 'lt' => $end]]];
194
        $this->merge($query);
195
        return $this;
196
    }
197
198
    public function betweenOrEquals($field, $start, $end)
199
    {
200
        $query = ['range' => [$field => ['gte' => $start, 'lte' => $end]]];
201
        $this->merge($query);
202
        return $this;
203
    }
204
205
    public function range($field, $start, $end)
206
    {
207
        return $this->betweenOrEquals($field, $start, $end);
208
    }
209
210
    public function greaterThan($field, $start)
211
    {
212
        $query = ['range' => [$field => ['gt' => $start]]];
213
        $this->merge($query);
214
        return $this;
215
    }
216
217
    public function gt($field, $start)
218
    {
219
        return $this->greaterThan($field, $start);
220
    }
221
222
    public function greaterThanOrEquals($field, $start)
223
    {
224
        $query = ['range' => [$field => ['gte' => $start]]];
225
        $this->merge($query);
226
        return $this;
227
    }
228
229
    public function gte($field, $start)
230
    {
231
        return $this->greaterThanOrEquals($field, $start);
232
    }
233
234
    public function lessThan($field, $end)
235
    {
236
        $query = ['range' => [$field => ['lt' => $end]]];
237
        $this->merge($query);
238
        return $this;
239
    }
240
241
    public function lt($field, $start)
242
    {
243
        return $this->lessThan($field, $start);
244
    }
245
246
    public function lessThanOrEquals($field, $end)
247
    {
248
        $query = ['range' => [$field => ['lte' => $end]]];
249
        $this->merge($query);
250
        return $this;
251
    }
252
253
    public function lte($field, $start)
254
    {
255
        return $this->lessThanOrEquals($field, $start);
256
    }
257
258
    public function match($field, $value)
259
    {
260
        $query = ['query' => ['match' => [$field => $value]]];
261
        $this->merge($query);
262
        return $this;
263
    }
264
265
    public function notMatch($field, $value)
266
    {
267
        $query = ['query' => ['match' => [$field => $value]]];
268
        $this->merge($query, 'must_not');
269
        return $this;
270
    }
271
272
    public function orMatch($field, $value)
273
    {
274
        $query = ['query' => ['match' => [$field => $value]]];
275
        $this->merge($query, 'should');
276
        return $this;
277
    }
278
}