CoreQuery   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 267
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 267
rs 10
c 0
b 0
f 0
wmc 21

21 Methods

Rating   Name   Duplication   Size   Complexity  
A setOrFilters() 0 5 1
A addFilter() 0 5 1
A addSort() 0 5 1
A addBoostedField() 0 5 1
A getOrFilters() 0 3 1
A getStart() 0 3 1
A setHighlight() 0 5 1
A addTerm() 0 10 1
A setSort() 0 5 1
A getSort() 0 3 1
A getFilters() 0 3 1
A setBoostedFields() 0 3 1
A setTerms() 0 5 1
A getBoostedFields() 0 3 1
A setRows() 0 5 1
A setStart() 0 5 1
A addOrFilter() 0 5 1
A getTerms() 0 3 1
A getRows() 0 3 1
A isHighlight() 0 3 1
A setFilters() 0 5 1
1
<?php
2
3
namespace Firesphere\SearchBackend\Queries;
4
5
use Firesphere\SearchBackend\Interfaces\QueryInterface;
6
7
/**
8
 * Default querying interface,
9
 */
10
class CoreQuery implements QueryInterface
11
{
12
    /**
13
     * @var int Pagination start
14
     */
15
    protected $start = 0;
16
    /**
17
     * @var int Total rows to display
18
     */
19
    protected $rows = 10;
20
    /**
21
     * @var array Sorting settings
22
     */
23
    protected $sort = [];
24
    /**
25
     * @var array Filters to use/apply
26
     */
27
    protected $filters = [];
28
29
    /**
30
     * @var array Filters that are not exclusive
31
     */
32
    protected $orFilters = [];
33
    /**
34
     * @var array Search terms
35
     */
36
    protected $terms = [];
37
    /**
38
     * @var array
39
     */
40
    protected $boostedFields = [];
41
    /**
42
     * @var bool
43
     */
44
    protected $highlight = true;
45
    /**
46
     * @var bool Enable spellchecking?
47
     */
48
    protected $spellcheck = true;
49
50
    /**
51
     * Get the search terms
52
     *
53
     * @return array
54
     */
55
    public function getTerms(): array
56
    {
57
        return $this->terms;
58
    }
59
60
    /**
61
     * Set the search tearms
62
     *
63
     * @param array $terms
64
     * @return $this
65
     */
66
    public function setTerms($terms): self
67
    {
68
        $this->terms = $terms;
69
70
        return $this;
71
    }
72
73
    /**
74
     * Each boosted query needs a separate addition!
75
     * e.g. $this->addTerm('test', ['MyField', 'MyOtherField'], 3)
76
     * followed by
77
     * $this->addTerm('otherTest', ['Title'], 5);
78
     *
79
     * If you want a generic boost on all terms, use addTerm only once, but boost on each field
80
     *
81
     * The fields parameter is used to boost on
82
     * @param string $term Term to search for
83
     * @param array $fields fields to boost on
84
     * @param int $boost Boost value
85
     * @param float|bool $fuzzy True or a value to the maximum amount of iterations
86
     * @return $this
87
     * For generic boosting, use @addBoostedField($field, $boost), this will add the boost at Index time
88
     */
89
    public function addTerm(string $term, array $fields = [], int $boost = 0, float|bool|null $fuzzy = false): self
90
    {
91
        $this->terms[] = [
92
            'text'   => $term,
93
            'fields' => $fields,
94
            'boost'  => $boost,
95
            'fuzzy'  => $fuzzy,
96
        ];
97
98
        return $this;
99
    }
100
101
    /**
102
     * @param string $key Field to apply filter on
103
     * @param string|array $value Value(s) to filter on
104
     * @return CoreQuery
105
     */
106
    public function addFilter($key, $value): CoreQuery
107
    {
108
        $this->filters[$key] = $value;
109
110
        return $this;
111
    }
112
113
    /**
114
     * @return array
115
     */
116
    public function getFilters(): array
117
    {
118
        return $this->filters;
119
    }
120
121
    /**
122
     * @param array $filters
123
     * @return CoreQuery
124
     */
125
    public function setFilters(array $filters): CoreQuery
126
    {
127
        $this->filters = $filters;
128
129
        return $this;
130
    }
131
132
    /**
133
     * Get the OR filters for this query
134
     *
135
     * @return array
136
     */
137
    public function getOrFilters(): array
138
    {
139
        return $this->orFilters;
140
    }
141
142
    /**
143
     * Set the or filters for this query
144
     * @param array $filters
145
     * @return self
146
     */
147
    public function setOrFilters(array $filters): self
148
    {
149
        $this->orFilters = $filters;
150
151
        return $this;
152
    }
153
154
    /**
155
     * Add the or filters in a key-value pair
156
     * @param string $key
157
     * @param string $value
158
     * @return self
159
     */
160
    public function addOrFilter(string $key, string $value): self
161
    {
162
        $this->orFilters[$key] = $value;
163
164
        return $this;
165
    }
166
167
    /**
168
     * Get the offset to start
169
     *
170
     * @return int
171
     */
172
    public function getStart(): int
173
    {
174
        return $this->start;
175
    }
176
177
    /**
178
     * Set the offset to start
179
     *
180
     * @param int $start
181
     * @return self
182
     */
183
    public function setStart($start): self
184
    {
185
        $this->start = $start;
186
187
        return $this;
188
    }
189
190
    /**
191
     * Get the rows to return
192
     *
193
     * @return int
194
     */
195
    public function getRows(): int
196
    {
197
        return $this->rows;
198
    }
199
200
    /**
201
     * Set the rows to return
202
     *
203
     * @param int $rows
204
     * @return self
205
     */
206
    public function setRows($rows): self
207
    {
208
        $this->rows = $rows;
209
210
        return $this;
211
    }
212
213
    /**
214
     * Get the sort fields
215
     *
216
     * @return array
217
     */
218
    public function getSort(): array
219
    {
220
        return $this->sort;
221
    }
222
223
    /**
224
     * Set the sort fields
225
     *
226
     * @param array $sort
227
     * @return self
228
     */
229
    public function setSort($sort): self
230
    {
231
        $this->sort = $sort;
232
233
        return $this;
234
    }
235
236
    /**
237
     * Add a sort field and direction
238
     *
239
     * @param string $field
240
     * @param string $direction
241
     * @return self
242
     */
243
    public function addSort($field, $direction): self
244
    {
245
        $this->sort[$field] = $direction;
246
247
        return $this;
248
    }
249
250
    public function getBoostedFields(): array
251
    {
252
        return $this->boostedFields;
253
    }
254
255
    public function setBoostedFields(array $boostedFields): void
256
    {
257
        $this->boostedFields = $boostedFields;
258
    }
259
260
    public function addBoostedField($key, $value): self
261
    {
262
        $this->boostedFields[$key] = $value;
263
264
        return $this;
265
    }
266
267
    public function isHighlight(): bool
268
    {
269
        return $this->highlight;
270
    }
271
272
    public function setHighlight(bool $highlight): self
273
    {
274
        $this->highlight = $highlight;
275
276
        return $this;
277
    }
278
}
279