Passed
Push — hans/or-we-start-or-facets ( e1f517...266943 )
by Simon
19:30 queued 09:55
created

BaseQueryTrait::getAndFacetFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * Trait BaseQueryTrait|Firesphere\SolrSearch\Traits\BaseQueryTrait Trait to clean up the
4
 * {@link \Firesphere\SolrSearch\Queries\BaseQuery}
5
 *
6
 * @package Firesphere\SolrSearch\Traits
7
 * @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo
8
 * @copyright Copyright (c) 2018 - now() Firesphere & Sheepy
9
 */
10
11
namespace Firesphere\SolrSearch\Traits;
12
13
use Minimalcode\Search\Criteria;
14
15
/**
16
 * Trait BaseQueryTrait Extraction from the BaseQuery class to keep things readable.
17
 *
18
 * This trait adds the support for adding the basic field/filter/term/facet options
19
 *
20
 * @package Firesphere\SolrSearch\Traits
21
 */
22
trait BaseQueryTrait
23
{
24
    /**
25
     * @var array Terms to search
26
     */
27
    protected $terms = [];
28
29
    /**
30
     * @var array Fields to filter
31
     */
32
    protected $filter = [];
33
34
    /**
35
     * @var array Fields to search
36
     */
37
    protected $fields = [];
38
39
    /**
40
     * Key => value pairs of facets to apply in AND fashion
41
     * [
42
     *     'FacetTitle' => [1, 2, 3],
43
     *     'FacetTitle2' => [1, 2, 3]
44
     * ]
45
     *
46
     * @var array
47
     */
48
    protected $andFacetFilter = [];
49
50
    /**
51
     * Key => value pairs of facets to apply in OR fashion
52
     * [
53
     *     'FacetTitle' => [1, 2, 3],
54
     *     'FacetTitle2' => [1, 2, 3]
55
     * ]
56
     *
57
     * @var array
58
     */
59
    protected $orFacetFilter = [];
60
61
    /**
62
     * @var array Fields to exclude
63
     */
64
    protected $exclude = [];
65
66
    /**
67
     * @var array Sorting order
68
     */
69
    protected $sort = [];
70
71
    /**
72
     * Each boosted query needs a separate addition!
73
     * e.g. $this->addTerm('test', ['MyField', 'MyOtherField'], 3)
74
     * followed by
75
     * $this->addTerm('otherTest', ['Title'], 5);
76
     *
77
     * If you want a generic boost on all terms, use addTerm only once, but boost on each field
78
     *
79
     * The fields parameter is used to boost on
80
     *
81
     * For generic boosting, use @addBoostedField($field, $boost), this will add the boost at Index time
82
     *
83
     * @param string $term Term to search for
84
     * @param array $fields fields to boost on
85
     * @param int $boost Boost value
86
     * @param bool|float $fuzzy True or a value to the maximum amount of iterations
87
     * @return $this
88
     */
89 7
    public function addTerm(string $term, array $fields = [], int $boost = 0, $fuzzy = null): self
90
    {
91 7
        $this->terms[] = [
92 7
            'text'   => $term,
93 7
            'fields' => $fields,
94 7
            'boost'  => $boost,
95 7
            'fuzzy'  => $fuzzy,
96
        ];
97
98 7
        return $this;
99
    }
100
101
    /**
102
     * Adds filters to filter on by value
103
     *
104
     * @param string $field Field to filter on
105
     * @param string|array|Criteria $value Value for this field
106
     * @return $this
107
     */
108 4
    public function addFilter($field, $value): self
109
    {
110 4
        $field = str_replace('.', '_', $field);
111 4
        $this->filter[$field] = $value;
112
113 4
        return $this;
114
    }
115
116
    /**
117
     * Add a field to be returned
118
     *
119
     * @param string $field fieldname
120
     * @return $this
121
     */
122 2
    public function addField($field): self
123
    {
124 2
        $field = str_replace('.', '_', $field);
125 2
        $this->fields[] = $field;
126
127 2
        return $this;
128
    }
129
130
    /**
131
     * Exclude fields from the search action
132
     *
133
     * @param string $field
134
     * @param string|array|Criteria $value
135
     * @return $this
136
     */
137 4
    public function addExclude($field, $value): self
138
    {
139 4
        $field = str_replace('.', '_', $field);
140 4
        $this->exclude[$field] = $value;
141
142 4
        return $this;
143
    }
144
145
    /**
146
     * Add faceting fields that need to be faceted in an AND format
147
     *
148
     * @param string $field Field to facet
149
     * @param string|array $value Value to facet
150
     * @return $this
151
     */
152 2
    public function addFacetFilter($field, $value): self
153
    {
154 2
        $this->andFacetFilter[$field][] = $value;
155
156 2
        return $this;
157
    }
158
159
    /**
160
     * Stub for addFacetFilter to add an AND filter
161
     *
162
     * @param string $field
163
     * @param string|array $value
164
     * @return $this
165
     */
166
    public function addAndFacetFilter($field, $value): self
167
    {
168
        return $this->addFacetFilter($field, $value);
169
    }
170
171
    /**
172
     * Add faceting that need to be faceted in an OR formats
173
     *
174
     * @param string $field Field to facet
175
     * @param string|array $value Value to facet
176
     * @return $this
177
     */
178
    public function addOrFacetFilter($field, $value): self
179
    {
180
        $this->orFacetFilter[$field][] = $value;
181
182
        return $this;
183
    }
184
185
    /**
186
     * Add a field to sort on
187
     *
188
     * @param string $field
189
     * @param string $direction
190
     * @return $this
191
     */
192 1
    public function addSort($field, $direction): self
193
    {
194 1
        $this->sort[$field] = $direction;
195
196 1
        return $this;
197
    }
198
199
    /**
200
     * Stub for transitioning from standard Facets to AND/OR differentiated facets
201
     *
202
     * @return array
203
     */
204
    public function getAndFacetFields(): array
205
    {
206
        return $this->getFacetFields();
0 ignored issues
show
Bug introduced by
The method getFacetFields() does not exist on Firesphere\SolrSearch\Traits\BaseQueryTrait. Did you maybe mean getOrFacetFields()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

206
        return $this->/** @scrutinizer ignore-call */ getFacetFields();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
207
    }
208
209
    /**
210
     * Stub for transitioning from standard Facets to AND/OR differentiated facets
211
     *
212
     * @param array $facetFields
213
     * @return $this
214
     */
215
    public function setAndFacetFields($facetFields): self
216
    {
217
        return $this->setFacetFields($facetFields);
0 ignored issues
show
Bug introduced by
The method setFacetFields() does not exist on Firesphere\SolrSearch\Traits\BaseQueryTrait. Did you maybe mean setOrFacetFields()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

217
        return $this->/** @scrutinizer ignore-call */ setFacetFields($facetFields);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
218
    }
219
220
    /**
221
     * Get the OR separated facets
222
     *
223
     * @return array
224
     */
225
    public function getOrFacetFields(): array
226
    {
227
        return $this->orFacetFields;
228
    }
229
230
    /**
231
     * Add a set of OR facets
232
     *
233
     * @param array $orFacetFields
234
     * @return BaseQueryTrait
235
     */
236
    public function setOrFacetFields(array $orFacetFields): self
237
    {
238
        $this->orFacetFields = $orFacetFields;
0 ignored issues
show
Bug Best Practice introduced by
The property orFacetFields does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
239
240
        return $this;
241
    }
242
}
243