Passed
Push — hans/searchfixes ( 0ac753 )
by Simon
05:11
created

BaseQueryTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Test Coverage

Coverage 88%

Importance

Changes 8
Bugs 1 Features 1
Metric Value
eloc 26
c 8
b 1
f 1
dl 0
loc 137
ccs 22
cts 25
cp 0.88
rs 10
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addTerm() 0 10 1
A addExclude() 0 6 1
A addField() 0 6 1
A addFilter() 0 6 1
A addFacetFilter() 0 5 1
A addSort() 0 5 1
1
<?php
2
/**
3
 * Trait BaseQueryTrait|Firesphere\SolrSearch\Traits\BaseQueryTrait Trait to clean up the
4
 * {@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 Minimalcode\Search\Criteria;
14
15
/**
16
 * Trait BaseQueryTrait Extraction from the BaseQuery class to keep things readable.
17
 *
18
 * This trait adds the support for adding the basic field/filter/term/facet options
19
 *
20
 * @package Firesphere\SolrSearch\Traits
21
 */
22
trait BaseQueryTrait
23
{
24
    /**
25
     * @var array Terms to search
26
     */
27
    protected $terms = [];
28
29
    /**
30
     * @var array Fields to filter
31
     */
32
    protected $filter = [];
33
34
    /**
35
     * @var array Fields to search
36
     */
37
    protected $fields = [];
38
39
    /**
40
     * Key => value pairs of facets to apply
41
     * [
42
     *     'FacetTitle' => [1, 2, 3]
43
     * ]
44
     *
45
     * @var array
46
     */
47
    protected $facetFilter = [];
48
49
    /**
50
     * @var array Fields to exclude
51
     */
52
    protected $exclude = [];
53
54
    /**
55
     * @var array Sorting order
56
     */
57
    protected $sort = [];
58
59
    /**
60
     * Each boosted query needs a separate addition!
61
     * e.g. $this->addTerm('test', ['MyField', 'MyOtherField'], 3)
62
     * followed by
63
     * $this->addTerm('otherTest', ['Title'], 5);
64
     *
65
     * If you want a generic boost on all terms, use addTerm only once, but boost on each field
66
     *
67
     * The fields parameter is used to boost on
68
     *
69
     * For generic boosting, use @addBoostedField($field, $boost), this will add the boost at Index time
70
     *
71
     * @param string $term Term to search for
72
     * @param array $fields fields to boost on
73
     * @param int $boost Boost value
74
     * @param bool|float $fuzzy True or a value to the maximum amount of iterations
75
     * @return $this
76
     */
77 7
    public function addTerm(string $term, array $fields = [], int $boost = 0, $fuzzy = null): self
78
    {
79 7
        $this->terms[] = [
80 7
            'text'   => $term,
81 7
            'fields' => $fields,
82 7
            'boost'  => $boost,
83 7
            'fuzzy'  => $fuzzy,
84
        ];
85
86 7
        return $this;
87
    }
88
89
    /**
90
     * Adds filters to filter on by value
91
     *
92
     * @param string $field Field to filter on
93
     * @param string|array|Criteria $value Value for this field
94
     * @return $this
95
     */
96 4
    public function addFilter($field, $value): self
97
    {
98 4
        $field = str_replace('.', '_', $field);
99 4
        $this->filter[$field] = $value;
100
101 4
        return $this;
102
    }
103
104
    /**
105
     * Add a field to be returned
106
     *
107
     * @param string $field fieldname
108
     * @return $this
109
     */
110 2
    public function addField($field): self
111
    {
112 2
        $field = str_replace('.', '_', $field);
113 2
        $this->fields[] = $field;
114
115 2
        return $this;
116
    }
117
118
    /**
119
     * Exclude fields from the search action
120
     *
121
     * @param string $field
122
     * @param string|array|Criteria $value
123
     * @return $this
124
     */
125 4
    public function addExclude($field, $value): self
126
    {
127 4
        $field = str_replace('.', '_', $field);
128 4
        $this->exclude[$field] = $value;
129
130 4
        return $this;
131
    }
132
133
    /**
134
     * Add faceting
135
     *
136
     * @param string $field Field to facet
137
     * @param string|array $value Value to facet
138
     * @return $this
139
     */
140 2
    public function addFacetFilter($field, $value): self
141
    {
142 2
        $this->facetFilter[$field][] = $value;
143
144 2
        return $this;
145
    }
146
147
    /**
148
     * Add a field to sort on
149
     *
150
     * @param string $field
151
     * @param string $direction
152
     * @return $this
153
     */
154
    public function addSort($field, $direction): self
155
    {
156
        $this->sort[$field] = $direction;
157
158
        return $this;
159
    }
160
}
161