Passed
Push — hans/or-we-start-or-facets ( e26c3b...fa0ee8 )
by Simon
04:52
created

GetterSetterTrait::setFacetFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 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 $facetFields = [];
59
60
61
    /**
62
     * Set the classes
63
     *
64
     * @param array $class
65
     * @return $this
66
     */
67 40
    public function setClasses($class): self
68
    {
69 40
        $this->class = $class;
70
71 40
        return $this;
72
    }
73
74
    /**
75
     * Get classes
76
     *
77
     * @return array
78
     */
79 49
    public function getClasses(): array
80
    {
81 49
        return $this->class;
82
    }
83
84
    /**
85
     * Add a class to index or query
86
     * $options is not used anymore, added for backward compatibility
87
     *
88
     * @param $class
89
     * @param array $options unused
90
     * @return $this
91
     */
92 52
    public function addClass($class, $options = []): self
93
    {
94 52
        if (count($options)) {
95
            Deprecation::notice('5.0', 'Options are not used anymore');
96
        }
97 52
        $this->class[] = $class;
98
99 52
        return $this;
100
    }
101
102
    /**
103
     * Add a boosted field to be boosted at query time
104
     *
105
     * This method is out of place in a way, but it's a shared method
106
     * between Index and Query, thus needs to be here.
107
     *
108
     * @param string $field
109
     * @param array|int $options
110
     * @param int|null $boost
111
     * @return $this
112
     */
113 3
    public function addBoostedField($field, $options = [], $boost = null)
114
    {
115 3
        if ($boost === null && is_int($options)) {
116 1
            $boost = $options;
117
        }
118
119 3
        $this->boostedFields[$field] = $boost;
120
121 3
        return $this;
122
    }
123
124
    /**
125
     * Get the boosted fields
126
     *
127
     * @return array
128
     */
129 43
    public function getBoostedFields(): array
130
    {
131 43
        return $this->boostedFields;
132
    }
133
134
    /**
135
     * Boosted fields are used at index time, not at query time
136
     *
137
     * @param array $boostedFields
138
     * @return $this
139
     */
140 40
    public function setBoostedFields($boostedFields): self
141
    {
142 40
        $this->boostedFields = $boostedFields;
143
144 40
        return $this;
145
    }
146
147
    /**
148
     * Get the facet fields
149
     *
150
     * @return array
151
     */
152 42
    public function getFacetFields(): array
153
    {
154 42
        return $this->facetFields;
155
    }
156
157
    /**
158
     * Set the facet fields
159
     *
160
     * @param array $facetFields
161
     * @return $this
162
     */
163 39
    public function setFacetFields($facetFields): self
164
    {
165 39
        $this->facetFields = $facetFields;
166
167 39
        return $this;
168
    }
169
}
170