Passed
Push — sheepy/elevation-configuration ( bdeab0...bcf7bc )
by Marco
18:34
created

BaseIndexTrait::addFacetField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
4
namespace Firesphere\SolrSearch\Traits;
5
6
use Firesphere\SolrSearch\Indexes\BaseIndex;
7
use SilverStripe\Dev\Deprecation;
8
use Solarium\Core\Client\Client;
9
10
/**
11
 * This is slightly cheating, but it works and also makes things more readable.
12
 *
13
 * Trait BaseIndexTrait
14
 * @package Firesphere\SolrSearch\Traits
15
 */
16
trait BaseIndexTrait
17
{
18
    /**
19
     * @var Client
20
     */
21
    protected $client;
22
    /**
23
     * @var array
24
     */
25
    protected $facetFields = [];
26
    /**
27
     * @var array
28
     */
29
    protected $fulltextFields = [];
30
    /**
31
     * @var array
32
     */
33
    protected $filterFields = [];
34
    /**
35
     * @var array
36
     */
37
    protected $sortFields = [];
38
    /**
39
     * @var string
40
     */
41
    protected $defaultField = '_text';
42
    /**
43
     * @var array
44
     */
45
    protected $storedFields = [];
46
    /**
47
     * @var array
48
     */
49
    protected $copyFields = [
50
        '_text' => [
51
            '*'
52
        ],
53
    ];
54
55
    /**
56
     * @return array
57
     */
58
    public function getCopyFields(): array
59
    {
60
        return $this->copyFields;
61
    }
62
63
    /**
64
     * @param array $copyField
65
     * @return $this
66
     */
67
    public function setCopyFields($copyField): self
68
    {
69
        $this->copyFields = $copyField;
70
71
        return $this;
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function getDefaultField(): string
78
    {
79
        return $this->defaultField;
80
    }
81
82
    /**
83
     * @param string $defaultField
84
     * @return $this
85
     */
86
    public function setDefaultField($defaultField): self
87
    {
88
        $this->defaultField = $defaultField;
89
90
        return $this;
91
    }
92
93
    /**
94
     * @param $sortField
95
     * @return $this
96
     */
97
    public function addSortField($sortField): self
98
    {
99
        if (!in_array($sortField, $this->getFulltextFields(), true) &&
100
            !in_array($sortField, $this->getFilterFields(), true)
101
        ) {
102
            $this->addFulltextField($sortField);
103
            $this->sortFields[] = $sortField;
104
        }
105
106
        $this->setSortFields(array_unique($this->getSortFields()));
107
108
        return $this;
109
    }
110
111
    /**
112
     * @return array
113
     */
114
    public function getFulltextFields(): array
115
    {
116
        return array_values(
117
            array_unique(
118
                $this->fulltextFields
119
            )
120
        );
121
    }
122
123
    /**
124
     * @param array $fulltextFields
125
     * @return $this
126
     */
127
    public function setFulltextFields($fulltextFields): self
128
    {
129
        $this->fulltextFields = $fulltextFields;
130
131
        return $this;
132
    }
133
134
    /**
135
     * @return array
136
     */
137
    public function getFilterFields(): array
138
    {
139
        return $this->filterFields;
140
    }
141
142
    /**
143
     * @param array $filterFields
144
     * @return $this
145
     */
146
    public function setFilterFields($filterFields): self
147
    {
148
        $this->filterFields = $filterFields;
149
150
        return $this;
151
    }
152
153
    /**
154
     * @param string $fulltextField
155
     * @param null|string $forceType
156
     * @param array $options
157
     * @return $this
158
     */
159
    public function addFulltextField($fulltextField, $forceType = null, $options = []): self
160
    {
161
        if ($forceType) {
162
            Deprecation::notice('5.0', 'ForceType should be handled through casting');
163
        }
164
165
        $key = array_search($fulltextField, $this->getFilterFields(), true);
166
167
        if (!$key) {
168
            $this->fulltextFields[] = $fulltextField;
169
        }
170
171
        if (isset($options['boost'])) {
172
            $this->addBoostedField($fulltextField, [], $options['boost']);
0 ignored issues
show
Bug introduced by
It seems like addBoostedField() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

172
            $this->/** @scrutinizer ignore-call */ 
173
                   addBoostedField($fulltextField, [], $options['boost']);
Loading history...
173
        }
174
175
        if (isset($options['stored'])) {
176
            $this->storedFields[] = $fulltextField;
177
        }
178
179
        return $this;
180
    }
181
182
    /**
183
     * @return array
184
     */
185
    public function getSortFields(): array
186
    {
187
        return $this->sortFields;
188
    }
189
190
    /**
191
     * @param array $sortFields
192
     * @return $this
193
     */
194
    public function setSortFields($sortFields): self
195
    {
196
        $this->sortFields = $sortFields;
197
198
        return $this;
199
    }
200
201
    /**
202
     * @param $field
203
     * @param array $options
204
     * @return $this
205
     */
206
    public function addFacetField($field, $options): self
207
    {
208
        $this->facetFields[$field] = $options;
209
210
        if (!in_array($field, $this->getFilterFields(), true)) {
211
            $this->addFilterField($field);
212
        }
213
214
        return $this;
215
    }
216
217
    /**
218
     * @param $filterField
219
     * @return $this
220
     */
221
    public function addFilterField($filterField): self
222
    {
223
        $key = array_search($filterField, $this->getFulltextFields(), true);
224
        if ($key === false) {
225
            $this->filterFields[] = $filterField;
226
        }
227
228
        return $this;
229
    }
230
231
    /**
232
     * @param string $field Name of the copyfield
233
     * @param array $options Array of all fields that should be copied to this copyfield
234
     * @return $this
235
     */
236
    public function addCopyField($field, $options): self
237
    {
238
        $this->copyFields[$field] = $options;
239
240
        if (!in_array($field, $this->getFulltextFields(), true)) {
241
            $this->addFulltextField($field);
242
        }
243
244
        return $this;
245
    }
246
247
    /**
248
     * @param string $field
249
     * @param null|string $forceType
250
     * @param array $extraOptions
251
     * @return BaseIndex
252
     */
253
    public function addStoredField($field, $forceType = null, $extraOptions = []): self
254
    {
255
        $options = array_merge($extraOptions, ['stored' => 'true']);
256
        $this->addFulltextField($field, $forceType, $options);
257
258
        return $this;
259
    }
260
261
    /**
262
     * @return Client
263
     */
264
    public function getClient()
265
    {
266
        return $this->client;
267
    }
268
269
    /**
270
     * @param Client $client
271
     * @return $this
272
     */
273
    public function setClient($client): self
274
    {
275
        $this->client = $client;
276
277
        return $this;
278
    }
279
280
    /**
281
     * @return array
282
     */
283
    public function getStoredFields(): array
284
    {
285
        return $this->storedFields;
286
    }
287
288
    /**
289
     * @param array $storedFields
290
     * @return BaseIndex
291
     */
292
    public function setStoredFields(array $storedFields): self
293
    {
294
        $this->storedFields = $storedFields;
295
296
        return $this;
297
    }
298
}
299