Passed
Push — hans/or-we-start-or-facets ( e1f517...266943 )
by Simon
19:30 queued 09:55
created

GetterSetterTrait::getOrFacetFields()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 $facetFields = [];
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 49
    public function getClasses(): array
101
    {
102 49
        return $this->class;
103
    }
104
105
    /**
106
     * Add a class to index or query
107
     * $options is not used anymore, added for backward compatibility
108
     *
109
     * @param $class
110
     * @param array $options unused
111
     * @return $this
112
     */
113 52
    public function addClass($class, $options = []): self
114
    {
115 52
        if (count($options)) {
116
            Deprecation::notice('5.0', 'Options are not used anymore');
117
        }
118 52
        $this->class[] = $class;
119
120 52
        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
     * @param int|null $boost
132
     * @return $this
133
     */
134 3
    public function addBoostedField($field, $options = [], $boost = null)
135
    {
136 3
        if ($boost === null && is_int($options)) {
137 1
            $boost = $options;
138
        }
139
140 3
        $this->boostedFields[$field] = $boost;
141
142 3
        return $this;
143
    }
144
145
    /**
146
     * Get the boosted fields
147
     *
148
     * @return array
149
     */
150 43
    public function getBoostedFields(): array
151
    {
152 43
        return $this->boostedFields;
153
    }
154
155
    /**
156
     * Boosted fields are used at index time, not at query time
157
     *
158
     * @param array $boostedFields
159
     * @return $this
160
     */
161 40
    public function setBoostedFields($boostedFields): self
162
    {
163 40
        $this->boostedFields = $boostedFields;
164
165 40
        return $this;
166
    }
167
168
    /**
169
     * Get the facet fields
170
     *
171
     * @return array
172
     */
173
    public function getFacetFields(): array
174
    {
175
        return $this->facetFields;
176
    }
177
178
    /**
179
     * Set the facet fields
180
     *
181
     * @param array $facetFields
182
     * @return $this
183
     */
184 39
    public function setFacetFields($facetFields): self
185
    {
186 39
        $this->facetFields = $facetFields;
187
188
        return $this;
189
    }
190
}
191