Passed
Push — hans/or-we-start-or-facets ( da374f...93c8f4 )
by Simon
16:56 queued 06:56
created

GetterSetterTrait   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 211
Duplicated Lines 0 %

Test Coverage

Coverage 96.67%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 211
ccs 29
cts 30
cp 0.9667
rs 10
c 2
b 0
f 0
wmc 15

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setClasses() 0 5 1
A addClass() 0 8 2
A getOrFacetFields() 0 3 1
A setOrFacetFields() 0 5 1
A getAndFacetFields() 0 3 1
A getClasses() 0 3 1
A addBoostedField() 0 9 3
A getBoostedFields() 0 3 1
A getFacetFields() 0 3 1
A setBoostedFields() 0 5 1
A setAndFacetFields() 0 3 1
A setFacetFields() 0 5 1
1
<?php
2
/**
3
 * Trait GetterSetterTrait|Firesphere\SolrSearch\Traits\GetterSetterTrait Getters and setters that are duplicate among
4
 * classes like {@link \Firesphere\SolrSearch\Indexes\BaseIndex} and {@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 SilverStripe\Dev\Deprecation;
14
15
/**
16
 * Trait GetterSetterTrait for getting and setting data
17
 *
18
 * Getters and setters shared between the Index and Query
19
 *
20
 * @package Firesphere\SolrSearch\Traits
21
 */
22
trait GetterSetterTrait
23
{
24
    /**
25
     * @var array Classes to use
26
     */
27
    protected $class = [];
28
29
    /**
30
     * Sets boosting at _index_ time or _query_ time. Depending on the usage of this trait
31
     * [
32
     *     'FieldName' => 2,
33
     * ]
34
     *
35
     * @var array
36
     */
37
    protected $boostedFields = [];
38
39
    /**
40
     * Format:
41
     * SiteTree::class   => [
42
     *      'BaseClass' => SiteTree::class,
43
     *      'Field' => 'ChannelID',
44
     *      'Title' => 'Channel'
45
     * ],
46
     * Object::class   => [
47
     *      'BaseClass' => Object::class,
48
     *      'Field' => 'Relation.ID',
49
     *      'Title' => 'Relation'
50
     * ],
51
     *
52
     * The facets will be applied as a single "AND" query.
53
     * e.g. SiteTree_ChannelID:1 with Object_Relation_ID:5 will not be found,
54
     * if the facet filter requires the SiteTree_ChannelID to be 1 AND Object_Relation_ID to be 3 or 6
55
     *
56
     * @var array
57
     */
58
    protected $andFacetFields = [];
59
60
    /**
61
     * Format:
62
     * SiteTree::class   => [
63
     *      'BaseClass' => SiteTree::class,
64
     *      'Field' => 'ChannelID',
65
     *      'Title' => 'Channel'
66
     * ],
67
     * Object::class   => [
68
     *      'BaseClass' => Object::class,
69
     *      'Field' => 'Relation.ID',
70
     *      'Title' => 'Relation'
71
     * ],
72
     *
73
     * The facets will be applied as "OR" separated groups of filters. Compared to default
74
     * facets, that will combine all facets in to a single "AND" query.
75
     * e.g. SiteTree_ChannelID:1 with Object_Relation_ID:5 will be found,
76
     * if the facet filter requires the SiteTree_ChannelID to be 1 OR Object_Relation_ID to be 3 or 6
77
     *
78
     * @var array
79
     */
80
    protected $orFacetFields = [];
81
82
    /**
83
     * Set the classes
84
     *
85
     * @param array $class
86
     * @return $this
87
     */
88 40
    public function setClasses($class): self
89
    {
90 40
        $this->class = $class;
91
92 40
        return $this;
93
    }
94
95
    /**
96
     * Get classes
97
     *
98
     * @return array
99
     */
100
    public function getClasses(): array
101
    {
102
        return $this->class;
103 52
    }
104
105 52
    /**
106
     * Add a class to index or query
107
     * $options is not used anymore, added for backward compatibility
108 52
     *
109
     * @param $class
110 52
     * @param array $options unused
111
     * @return $this
112
     */
113
    public function addClass($class, $options = []): self
114
    {
115
        if (count($options)) {
116
            Deprecation::notice('5.0', 'Options are not used anymore');
117
        }
118 49
        $this->class[] = $class;
119
120 49
        return $this;
121
    }
122
123
    /**
124
     * Add a boosted field to be boosted at query time
125
     *
126
     * This method is out of place in a way, but it's a shared method
127
     * between Index and Query, thus needs to be here.
128
     *
129
     * @param string $field
130
     * @param array|int $options
131 3
     * @param int|null $boost
132
     * @return $this
133 3
     */
134 1
    public function addBoostedField($field, $options = [], $boost = null)
135
    {
136
        if ($boost === null && is_int($options)) {
137 3
            $boost = $options;
138
        }
139 3
140
        $this->boostedFields[$field] = $boost;
141
142
        return $this;
143
    }
144
145
    /**
146
     * Get the boosted fields
147 43
     *
148
     * @return array
149 43
     */
150
    public function getBoostedFields(): array
151
    {
152
        return $this->boostedFields;
153
    }
154
155
    /**
156
     * Boosted fields are used at index time, not at query time
157
     *
158 40
     * @param array $boostedFields
159
     * @return $this
160 40
     */
161
    public function setBoostedFields($boostedFields): self
162 40
    {
163
        $this->boostedFields = $boostedFields;
164
165
        return $this;
166
    }
167
168
    /**
169
     * Stub for transitioning from standard Facets to AND/OR differentiated facets
170 42
     *
171
     * @return array
172 42
     */
173
    public function getAndFacetFields(): array
174
    {
175
        return $this->getFacetFields();
176
    }
177
178
    /**
179
     * Stub for transitioning from standard Facets to AND/OR differentiated facets
180
     *
181 39
     * @param array $facetFields
182
     * @return $this
183 39
     */
184
    public function setAndFacetFields($facetFields): self
185 39
    {
186
        return $this->setFacetFields($facetFields);
187
    }
188
189
    /**
190
     * Get the facet fields
191
     *
192
     * @return array
193 38
     */
194
    public function getFacetFields(): array
195 38
    {
196
        return $this->andFacetFields;
197
    }
198
199
    /**
200
     * Set the facet fields
201
     *
202
     * @param array $facetFields
203
     * @return $this
204 1
     */
205
    public function setFacetFields($facetFields): self
206 1
    {
207
        $this->andFacetFields = $facetFields;
208 1
209
        return $this;
210
    }
211
212
    /**
213
     * Get the OR separated facets
214
     *
215
     * @return array
216
     */
217
    public function getOrFacetFields(): array
218
    {
219
        return $this->orFacetFields;
220
    }
221
222
    /**
223
     * Add a set of OR facets
224
     *
225
     * @param array $orFacetFields
226
     * @return GetterSetterTrait
227
     */
228
    public function setOrFacetFields(array $orFacetFields): self
229
    {
230
        $this->orFacetFields = $orFacetFields;
231
232
        return $this;
233
    }
234
}
235