Passed
Push — hans/code-cleanup ( 7f6349...96a9e1 )
by Simon
06:38
created

GetterSetterTrait::addClass()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.032
1
<?php
2
3
4
namespace Firesphere\SolrSearch\Traits;
5
6
use SilverStripe\Dev\Deprecation;
7
8
/**
9
 * Trait GetterSetterTrait for getting and setting data
10
 *
11
 * Getters and setters shared between the Index and Query
12
 *
13
 * @package Firesphere\SolrSearch\Traits
14
 */
15
trait GetterSetterTrait
16
{
17
    /**
18
     * @var array Classes to use
19
     */
20
    protected $class = [];
21
22
    /**
23
     * Sets boosting at _index_ time or _query_ time. Depending on the usage of this trait
24
     * [
25
     *     'FieldName' => 2,
26
     * ]
27
     *
28
     * @var array
29
     */
30
    protected $boostedFields = [];
31
32
    /**
33
     * Format:
34
     * SiteTree::class   => [
35
     *      'BaseClass' => SiteTree::class,
36
     *      'Field' => 'ChannelID',
37
     *      'Title' => 'Channel'
38
     * ],
39
     *
40
     * @var array
41
     */
42
    protected $facetFields = [];
43
44
    /**
45
     * Set the classes
46
     *
47
     * @param array $class
48
     * @return $this
49
     */
50 74
    public function setClasses($class): self
51
    {
52 74
        $this->class = $class;
53
54 74
        return $this;
55
    }
56
57
    /**
58
     * Add a class to index or query
59
     * $options is not used anymore, added for backward compatibility
60
     *
61
     * @param $class
62
     * @param array $options unused
63
     * @return $this
64
     */
65 75
    public function addClass($class, $options = []): self
66
    {
67 75
        if (count($options)) {
68
            Deprecation::notice('5.0', 'Options are not used anymore');
69
        }
70 75
        $this->class[] = $class;
71
72 75
        return $this;
73
    }
74
75
    /**
76
     * Get classes
77
     *
78
     * @return array
79
     */
80 79
    public function getClasses(): array
81
    {
82 79
        return $this->class;
83
    }
84
85
    /**
86
     * Add a boosted field to be boosted at query time
87
     *
88
     * @param string $field
89
     * @param array|int $options
90
     * @param int|null $boost
91
     * @return $this
92
     */
93 3
    public function addBoostedField($field, $options = [], $boost = null)
94
    {
95 3
        if ($boost === null && is_int($options)) {
96 1
            $boost = $options;
97
        }
98
99 3
        $this->boostedFields[$field] = $boost;
100
101 3
        return $this;
102
    }
103
104
    /**
105
     * Get the boosted fields
106
     *
107
     * @return array
108
     */
109 39
    public function getBoostedFields(): array
110
    {
111 39
        return $this->boostedFields;
112
    }
113
114
    /**
115
     * Boosted fields are used at index time, not at query time
116
     *
117
     * @param array $boostedFields
118
     * @return $this
119
     */
120 74
    public function setBoostedFields($boostedFields): self
121
    {
122 74
        $this->boostedFields = $boostedFields;
123
124 74
        return $this;
125
    }
126
127
    /**
128
     * Get the facet fields
129
     *
130
     * @return array
131
     */
132 38
    public function getFacetFields(): array
133
    {
134 38
        return $this->facetFields;
135
    }
136
137
    /**
138
     * Set the facet fields
139
     *
140
     * @param array $facetFields
141
     * @return $this
142
     */
143 74
    public function setFacetFields($facetFields): self
144
    {
145 74
        $this->facetFields = $facetFields;
146
147 74
        return $this;
148
    }
149
}
150