Passed
Push — hans/logtests ( 76c086...71695d )
by Simon
06:24 queued 02:27
created

BaseIndexTrait::getFulltextFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
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
     * @return array
57
     */
58 25
    public function getCopyFields(): array
59
    {
60 25
        return $this->copyFields;
61
    }
62
63
    /**
64
     * @param array $copyField
65
     * @return $this
66
     */
67 62
    public function setCopyFields($copyField): self
68
    {
69 62
        $this->copyFields = $copyField;
70
71 62
        return $this;
72
    }
73
74
    /**
75
     * @return string
76
     */
77 26
    public function getDefaultField(): string
78
    {
79 26
        return $this->defaultField;
80
    }
81
82
    /**
83
     * @param string $defaultField
84
     * @return $this
85
     */
86 62
    public function setDefaultField($defaultField): self
87
    {
88 62
        $this->defaultField = $defaultField;
89
90 62
        return $this;
91
    }
92
93
    /**
94
     * @param $sortField
95
     * @return $this
96
     */
97 63
    public function addSortField($sortField): self
98
    {
99 63
        if (!in_array($sortField, $this->getFulltextFields(), true) &&
100 63
            !in_array($sortField, $this->getFilterFields(), true)
101
        ) {
102
            $this->addFulltextField($sortField);
103
            $this->sortFields[] = $sortField;
104
        }
105
106 63
        $this->setSortFields(array_unique($this->getSortFields()));
107
108 63
        return $this;
109
    }
110
111
    /**
112
     * @return array
113
     */
114 63
    public function getFulltextFields(): array
115
    {
116 63
        return array_values(
117 63
            array_unique(
118 63
                $this->fulltextFields
119
            )
120
        );
121
    }
122
123
    /**
124
     * @param array $fulltextFields
125
     * @return $this
126
     */
127 62
    public function setFulltextFields($fulltextFields): self
128
    {
129 62
        $this->fulltextFields = $fulltextFields;
130
131 62
        return $this;
132
    }
133
134
    /**
135
     * @return array
136
     */
137 63
    public function getFilterFields(): array
138
    {
139 63
        return $this->filterFields;
140
    }
141
142
    /**
143
     * @param array $filterFields
144
     * @return $this
145
     */
146 62
    public function setFilterFields($filterFields): self
147
    {
148 62
        $this->filterFields = $filterFields;
149
150 62
        return $this;
151
    }
152
153
    /**
154
     * @param string $fulltextField
155
     * @param null|string $forceType
156
     * @param array $options
157
     * @return $this
158
     */
159 63
    public function addFulltextField($fulltextField, $forceType = null, $options = []): self
160
    {
161 63
        if ($forceType) {
162
            Deprecation::notice('5.0', 'ForceType should be handled through casting');
163
        }
164
165 63
        $key = array_search($fulltextField, $this->getFilterFields(), true);
166
167 63
        if (!$key) {
168 63
            $this->fulltextFields[] = $fulltextField;
169
        }
170
171 63
        if (isset($options['boost'])) {
172
            $this->addBoostedField($fulltextField, [], $options['boost']);
173
        }
174
175 63
        if (isset($options['stored'])) {
176 1
            $this->storedFields[] = $fulltextField;
177
        }
178
179 63
        return $this;
180
    }
181
182
    /**
183
     * Add an abstract for the add Boosted Field to keep things consistent
184
     * @param string $field
185
     * @param array|int $options
186
     * @param null|int $boost
187
     * @return mixed
188
     */
189
    abstract public function addBoostedField($field, $options = [], $boost = null);
190
191
    /**
192
     * @return array
193
     */
194 63
    public function getSortFields(): array
195
    {
196 63
        return $this->sortFields;
197
    }
198
199
    /**
200
     * @param array $sortFields
201
     * @return $this
202
     */
203 63
    public function setSortFields($sortFields): self
204
    {
205 63
        $this->sortFields = $sortFields;
206
207 63
        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 63
    public function addFilterField($filterField): self
231
    {
232 63
        $key = array_search($filterField, $this->getFulltextFields(), true);
233 63
        if ($key === false) {
234 63
            $this->filterFields[] = $filterField;
235
        }
236
237 63
        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