Passed
Push — sheepy/introspection ( 69e16c...c6c7ca )
by Marco
05:28
created

BaseQueryTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 5
eloc 23
c 2
b 1
f 0
dl 0
loc 105
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addFacetFilter() 0 5 1
A addTerm() 0 10 1
A addExclude() 0 6 1
A addField() 0 6 1
A addFilter() 0 6 1
1
<?php
2
3
4
namespace Firesphere\SolrSearch\Traits;
5
6
trait BaseQueryTrait
7
{
8
    /**
9
     * @var array
10
     */
11
    protected $terms = [];
12
13
    /**
14
     * @var array
15
     */
16
    protected $filter = [];
17
18
    /**
19
     * @var array
20
     */
21
    protected $fields = [];
22
23
    /**
24
     * @var array
25
     */
26
    protected $facetFilter = [];
27
28
    /**
29
     * @var array
30
     */
31
    protected $exclude = [];
32
33
    /**
34
     * Each boosted query needs a separate addition!
35
     * e.g. $this->addTerm('test', ['MyField', 'MyOtherField'], 3)
36
     * followed by
37
     * $this->addTerm('otherTest', ['Title'], 5);
38
     *
39
     * If you want a generic boost on all terms, use addTerm only once, but boost on each field
40
     *
41
     * The fields parameter is used to boost on
42
     *
43
     * For generic boosting, use @addBoostedField($field, $boost), this will add the boost at Index time
44
     * @param string $term Term to search for
45
     * @param array $fields fields to boost on
46
     * @param int $boost Boost value
47
     * @param bool|float $fuzzy True or a value to the maximum amount of iterations
48
     * @return $this
49
     */
50
    public function addTerm($term, $fields = [], $boost = 0, $fuzzy = null): self
51
    {
52
        $this->terms[] = [
53
            'text'   => $term,
54
            'fields' => $fields,
55
            'boost'  => $boost,
56
            'fuzzy'  => $fuzzy
57
        ];
58
59
        return $this;
60
    }
61
62
    /**
63
     * @param string $field
64
     * @param string $value
65
     * @return $this
66
     */
67
    public function addFilter($field, $value): self
68
    {
69
        $field = str_replace('.', '_', $field);
70
        $this->filter[$field] = $value;
71
72
        return $this;
73
    }
74
75
    /**
76
     * Add a field to be returned
77
     * @param string $field fieldname
78
     * @return $this
79
     */
80
    public function addField($field): self
81
    {
82
        $field = str_replace('.', '_', $field);
83
        $this->fields[] = $field;
84
85
        return $this;
86
    }
87
88
    /**
89
     * @param $field
90
     * @param $value
91
     * @return $this
92
     */
93
    public function addExclude($field, $value): self
94
    {
95
        $field = str_replace('.', '_', $field);
96
        $this->exclude[$field] = $value;
97
98
        return $this;
99
    }
100
101
    /**
102
     * @param string $field
103
     * @param string $value
104
     * @return $this
105
     */
106
    public function addFacetFilter($field, $value): self
107
    {
108
        $this->facetFilter[$field][] = $value;
109
110
        return $this;
111
    }
112
}
113