Passed
Push — hans/or-we-start-or-facets ( cdc0bc...b65713 )
by Simon
05:46
created

GetterSetterTrait::setBoostedFields()   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 0
Metric Value
eloc 2
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
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
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

92
    public function addClass($class, /** @scrutinizer ignore-unused */ $options = []): self

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
    {
94 52
        $this->class[] = $class;
95
96 52
        return $this;
97
    }
98
99
    /**
100
     * Add a boosted field to be boosted at query time
101
     *
102
     * This method is out of place in a way, but it's a shared method
103
     * between Index and Query, thus needs to be here.
104
     *
105
     * @param string $field
106
     * @param array|int $options
107
     * @param int|null $boost
108
     * @return $this
109
     */
110 3
    public function addBoostedField($field, $options = [], $boost = null)
111
    {
112 3
        if ($boost === null && is_int($options)) {
113 1
            $boost = $options;
114
        }
115
116 3
        $this->boostedFields[$field] = $boost;
117
118 3
        return $this;
119
    }
120
121
    /**
122
     * Get the boosted fields
123
     *
124
     * @return array
125
     */
126 43
    public function getBoostedFields(): array
127
    {
128 43
        return $this->boostedFields;
129
    }
130
131
    /**
132
     * Boosted fields are used at index time, not at query time
133
     *
134
     * @param array $boostedFields
135
     * @return $this
136
     */
137 40
    public function setBoostedFields($boostedFields): self
138
    {
139 40
        $this->boostedFields = $boostedFields;
140
141 40
        return $this;
142
    }
143
144
    /**
145
     * Get the facet fields
146
     *
147
     * @return array
148
     */
149 42
    public function getFacetFields(): array
150
    {
151 42
        return $this->facetFields;
152
    }
153
154
    /**
155
     * Set the facet fields
156
     *
157
     * @param array $facetFields
158
     * @return $this
159
     */
160 39
    public function setFacetFields($facetFields): self
161
    {
162 39
        $this->facetFields = $facetFields;
163
164 39
        return $this;
165
    }
166
}
167