Passed
Push — sheepy/introspection ( 69e16c...c6c7ca )
by Marco
05:28
created

BaseIndexTrait::getDefaultField()   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 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
ccs 0
cts 0
cp 0
cc 1
nc 1
nop 0
crap 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 5
        ],
53
    ];
54 5
55
    /**
56
     * @return array
57
     */
58
    public function getCopyFields(): array
59
    {
60
        return $this->copyFields;
61 4
    }
62
63 4
    /**
64
     * @param array $copyField
65 4
     * @return $this
66
     */
67
    public function setCopyFields($copyField): self
68
    {
69
        $this->copyFields = $copyField;
70
71 5
        return $this;
72
    }
73 5
74
    /**
75
     * @return string
76
     */
77
    public function getDefaultField(): string
78
    {
79
        return $this->defaultField;
80 4
    }
81
82 4
    /**
83
     * @param string $defaultField
84 4
     * @return $this
85
     */
86
    public function setDefaultField($defaultField): self
87
    {
88
        $this->defaultField = $defaultField;
89
90
        return $this;
91 7
    }
92
93 7
    /**
94
     * @param $sortField
95 7
     * @return $this
96
     */
97 7
    public function addSortField($sortField): self
98
    {
99 7
        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 15
        $this->setSortFields(array_unique($this->getSortFields()));
107
108 15
        return $this;
109
    }
110 15
111 15
    /**
112
     * @return array
113
     */
114 15
    public function getFulltextFields(): array
115
    {
116
        return array_values(
117
            array_unique(
118
                $this->fulltextFields
119
            )
120 30
        );
121
    }
122 30
123
    /**
124
     * @param array $fulltextFields
125
     * @return $this
126
     */
127
    public function setFulltextFields($fulltextFields): self
128
    {
129 4
        $this->fulltextFields = $fulltextFields;
130
131 4
        return $this;
132
    }
133 4
134
    /**
135
     * @return array
136
     */
137
    public function getFilterFields(): array
138
    {
139 21
        return $this->filterFields;
140
    }
141 21
142
    /**
143
     * @param array $filterFields
144
     * @return $this
145
     */
146
    public function setFilterFields($filterFields): self
147
    {
148 7
        $this->filterFields = $filterFields;
149
150 7
        return $this;
151
    }
152 7
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 1
    {
161
        if ($forceType) {
162 1
            Deprecation::notice('5.0', 'ForceType should be handled through casting');
163
        }
164 1
165 1
        $key = array_search($fulltextField, $this->getFilterFields(), true);
166
167
        if (!$key) {
168 1
            $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 15
        if (isset($options['stored'])) {
176
            $this->storedFields[] = $fulltextField;
177 15
        }
178 15
179
        return $this;
180 7
    }
181
182 15
    /**
183
     * @return array
184 15
     */
185
    public function getSortFields(): array
186
    {
187
        return $this->sortFields;
188
    }
189
190 30
    /**
191
     * @param array $sortFields
192 30
     * @return $this
193 30
     */
194 30
    public function setSortFields($sortFields): self
195
    {
196
        $this->sortFields = $sortFields;
197
198
        return $this;
199
    }
200
201
    /**
202
     * @param $field
203 4
     * @param array $options
204
     * @return $this
205 4
     */
206
    public function addFacetField($field, $options): self
207 4
    {
208
        $this->facetFields[$field] = $options;
209
210
        if (!in_array($field, $this->getFilterFields(), true)) {
211
            $this->addFilterField($field);
212
        }
213
214
        return $this;
215 1
    }
216
217 1
    /**
218
     * @param $filterField
219 1
     * @return $this
220 1
     */
221
    public function addFilterField($filterField): self
222
    {
223 1
        $key = array_search($filterField, $this->getFulltextFields(), true);
224
        if ($key === false) {
225
            $this->filterFields[] = $filterField;
226
        }
227
228
        return $this;
229 18
    }
230
231 18
    /**
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 1
        $this->copyFields[$field] = $options;
239
240 1
        if (!in_array($field, $this->getFulltextFields(), true)) {
241
            $this->addFulltextField($field);
242 1
        }
243
244
        return $this;
245
    }
246
247
    /**
248
     * @return Client
249
     */
250
    public function getClient()
251
    {
252
        return $this->client;
253
    }
254
255
    /**
256
     * @param Client $client
257
     * @return $this
258
     */
259
    public function setClient($client): self
260
    {
261
        $this->client = $client;
262
263
        return $this;
264
    }
265
266
    /**
267
     * @return array
268
     */
269
    public function getStoredFields(): array
270
    {
271
        return $this->storedFields;
272
    }
273
274
    /**
275
     * @param array $storedFields
276
     * @return BaseIndex
277
     */
278
    public function setStoredFields(array $storedFields): self
279
    {
280
        $this->storedFields = $storedFields;
281
282
        return $this;
283
    }
284
}
285