Passed
Push — hans/code-cleanup ( 3f21ea...2d381d )
by Simon
09:28 queued 06:39
created

BaseIndexTrait::getFilterFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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
     * Add an abstract for the add Boosted Field to keep things consistent
57
     * @param string $field
58
     * @param array|int $options
59
     * @param null|int $boost
60
     * @return mixed
61
     */
62
    abstract public function addBoostedField($field, $options = [], $boost = null);
63
64
    /**
65
     * @return array
66
     */
67 25
    public function getCopyFields(): array
68
    {
69 25
        return $this->copyFields;
70
    }
71
72
    /**
73
     * @param array $copyField
74
     * @return $this
75
     */
76 59
    public function setCopyFields($copyField): self
77
    {
78 59
        $this->copyFields = $copyField;
79
80 59
        return $this;
81
    }
82
83
    /**
84
     * @return string
85
     */
86 26
    public function getDefaultField(): string
87
    {
88 26
        return $this->defaultField;
89
    }
90
91
    /**
92
     * @param string $defaultField
93
     * @return $this
94
     */
95 59
    public function setDefaultField($defaultField): self
96
    {
97 59
        $this->defaultField = $defaultField;
98
99 59
        return $this;
100
    }
101
102
    /**
103
     * @param $sortField
104
     * @return $this
105
     */
106 60
    public function addSortField($sortField): self
107
    {
108 60
        if (!in_array($sortField, $this->getFulltextFields(), true) &&
109 60
            !in_array($sortField, $this->getFilterFields(), true)
110
        ) {
111
            $this->addFulltextField($sortField);
112
            $this->sortFields[] = $sortField;
113
        }
114
115 60
        $this->setSortFields(array_unique($this->getSortFields()));
116
117 60
        return $this;
118
    }
119
120
    /**
121
     * @return array
122
     */
123 60
    public function getFulltextFields(): array
124
    {
125 60
        return array_values(
126 60
            array_unique(
127 60
                $this->fulltextFields
128
            )
129
        );
130
    }
131
132
    /**
133
     * @param array $fulltextFields
134
     * @return $this
135
     */
136 59
    public function setFulltextFields($fulltextFields): self
137
    {
138 59
        $this->fulltextFields = $fulltextFields;
139
140 59
        return $this;
141
    }
142
143
    /**
144
     * @return array
145
     */
146 60
    public function getFilterFields(): array
147
    {
148 60
        return $this->filterFields;
149
    }
150
151
    /**
152
     * @param array $filterFields
153
     * @return $this
154
     */
155 59
    public function setFilterFields($filterFields): self
156
    {
157 59
        $this->filterFields = $filterFields;
158
159 59
        return $this;
160
    }
161
162
    /**
163
     * @param string $fulltextField
164
     * @param null|string $forceType
165
     * @param array $options
166
     * @return $this
167
     */
168 60
    public function addFulltextField($fulltextField, $forceType = null, $options = []): self
169
    {
170 60
        if ($forceType) {
171
            Deprecation::notice('5.0', 'ForceType should be handled through casting');
172
        }
173
174 60
        $key = array_search($fulltextField, $this->getFilterFields(), true);
175
176 60
        if (!$key) {
177 60
            $this->fulltextFields[] = $fulltextField;
178
        }
179
180 60
        if (isset($options['boost'])) {
181
            $this->addBoostedField($fulltextField, [], $options['boost']);
182
        }
183
184 60
        if (isset($options['stored'])) {
185 1
            $this->storedFields[] = $fulltextField;
186
        }
187
188 60
        return $this;
189
    }
190
191
    /**
192
     * @return array
193
     */
194 60
    public function getSortFields(): array
195
    {
196 60
        return $this->sortFields;
197
    }
198
199
    /**
200
     * @param array $sortFields
201
     * @return $this
202
     */
203 60
    public function setSortFields($sortFields): self
204
    {
205 60
        $this->sortFields = $sortFields;
206
207 60
        return $this;
208
    }
209
210
    /**
211
     * @param $field
212
     * @param array $options
213
     * @return $this
214
     */
215 1
    public function addFacetField($field, $options): self
216
    {
217 1
        $this->facetFields[$field] = $options;
218
219 1
        if (!in_array($field, $this->getFilterFields(), true)) {
220 1
            $this->addFilterField($field);
221
        }
222
223 1
        return $this;
224
    }
225
226
    /**
227
     * @param $filterField
228
     * @return $this
229
     */
230 60
    public function addFilterField($filterField): self
231
    {
232 60
        $key = array_search($filterField, $this->getFulltextFields(), true);
233 60
        if ($key === false) {
234 60
            $this->filterFields[] = $filterField;
235
        }
236
237 60
        return $this;
238
    }
239
240
    /**
241
     * @param string $field Name of the copyfield
242
     * @param array $options Array of all fields that should be copied to this copyfield
243
     * @return $this
244
     */
245 1
    public function addCopyField($field, $options): self
246
    {
247 1
        $this->copyFields[$field] = $options;
248
249 1
        return $this;
250
    }
251
252
    /**
253
     * @param string $field
254
     * @param null|string $forceType
255
     * @param array $extraOptions
256
     * @return BaseIndex
257
     */
258 1
    public function addStoredField($field, $forceType = null, $extraOptions = []): self
259
    {
260 1
        $options = array_merge($extraOptions, ['stored' => 'true']);
261 1
        $this->addFulltextField($field, $forceType, $options);
262
263 1
        return $this;
264
    }
265
266
    /**
267
     * @return Client
268
     */
269 18
    public function getClient()
270
    {
271 18
        return $this->client;
272
    }
273
274
    /**
275
     * @param Client $client
276
     * @return $this
277
     */
278 1
    public function setClient($client): self
279
    {
280 1
        $this->client = $client;
281
282 1
        return $this;
283
    }
284
285
    /**
286
     * @return array
287
     */
288 25
    public function getStoredFields(): array
289
    {
290 25
        return $this->storedFields;
291
    }
292
293
    /**
294
     * @param array $storedFields
295
     * @return BaseIndex
296
     */
297 1
    public function setStoredFields(array $storedFields): self
298
    {
299 1
        $this->storedFields = $storedFields;
300
301 1
        return $this;
302
    }
303
}
304