Completed
Pull Request — develop (#284)
by
unknown
08:46
created

Facets::setupQuery()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 12
nc 4
nop 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 * @author Miroslav Fedeleš <[email protected]>
9
 * @since 0.27
10
 */
11
12
namespace Solr;
13
14
use IteratorAggregate;
15
use Countable;
16
use ArrayIterator;
17
use ArrayAccess;
18
use InvalidArgumentException;
19
use SolrDisMaxQuery;
20
use SolrUtils;
21
22
class Facets implements IteratorAggregate, Countable
23
{
24
    
25
    /**
26
     * @var ArrayAccess
27
     */
28
    protected $facetResult;
29
    
30
    /**
31
     * @var array
32
     */
33
    protected $params;
34
    
35
    /**
36
     * @var array
37
     */
38
    protected $definitions;
39
    
40
    /**
41
     * @var array
42
     */
43
    protected $queryMethodMap = [
44
        'facet_fields' => 'addFacetField'
45
    ];
46
    
47
    /**
48
     * @var array
49
     */
50
    protected $cache;
51
52
    public function __construct()
53
    {
54
        $this->params = [];
55
        $this->definitions = [
56
            'regionList' => [
57
                'type' => 'facet_fields',
58
                'title' => /*@translate*/ 'Regions'
59
            ]
60
        ];
61
    }
62
63
    /**
64
	 * @see IteratorAggregate::getIterator()
65
	 */
66
	public function getIterator()
67
	{
68
		return new ArrayIterator($this->toArray());
69
		
70
	}
71
72
    /**
73
	 * @see Countable::count()
74
	 */
75
	public function count()
76
	{
77
		return count($this->toArray());
78
	}
79
80
    /**
81
     * @return array
82
     */
83
    public function toArray()
84
    {
85
        if (!isset($this->cache)) {
86
            $this->cache = [];
87
            
88
            foreach ($this->definitions as $name => $definition) {
89
                // check if the facet definition exists in the facet result
90
                if (!isset($this->facetResult[$definition['type']])
91
                    || !isset($this->facetResult[$definition['type']][$name])
92
                ) {
93
                    continue;
94
                }
95
                
96
                // cast to array to workaround the 'Notice: Illegal member variable name' for PHP <= 5.6.20
97
                $result = (array)$this->facetResult[$definition['type']][$name];
98
                // remove empty value
99
                unset($result['']);
100
                
101
                $this->cache[$name] = $result;
102
            }
103
        }
104
        
105
        return $this->cache;
106
    }
107
    
108
    /**
109
     * @param ArrayAccess $facetResult
110
     * @return Facets
111
     */
112
    public function setFacetResult(ArrayAccess $facetResult)
113
    {
114
        $this->facetResult = $facetResult;
115
        $this->cache = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $cache.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
116
        
117
        return $this;
118
    }
119
    /**
120
	 * @param array $params
121
	 * @return Facets
122
	 */
123
	public function setParams(array $params)
124
	{
125
		$this->params = $params;
126
		
127
		return $this;
128
	}
129
130
    /**
131
     * @param SolrDisMaxQuery $query
132
     * @return Facets
133
     */
134
    public function setupQuery(SolrDisMaxQuery $query)
135
    {
136
        $query->setFacet(true);
137
        
138
        foreach ($this->definitions as $name => $definition) {
139
            $method = $this->queryMethodMap[$definition['type']];
140
            $query->$method($name);
141
            
142
            if (isset($this->params[$name]) && is_array($this->params[$name])) {
143
                $valueList = array_filter(array_map(function ($value) {
144
                    return trim(SolrUtils::escapeQueryChars($value));
145
                }, array_keys($this->params[$name])));
146
                
147
                if ($valueList) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $valueList of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
148
                    $query->addFilterQuery(sprintf('%s:(%s)', $name, implode(' OR ', $valueList)));
149
                }
150
            }
151
        }
152
        
153
        return $this;
154
    }
155
    
156
    /**
157
     * @param string $name
158
     * @param string $value
159
     * @return boolean
160
     * @throws InvalidArgumentException
161
     */
162
    public function isValueActive($name, $value)
163
    {
164
        $this->assertValidName($name);
165
        
166
        return isset($this->params[$name])
167
            && is_array($this->params[$name])
168
            && isset($this->params[$name][$value]);
169
    }
170
    
171
    /**
172
     * @return array
173
     */
174
    public function getActiveValues()
175
    {
176
        $return = [];
177
        
178
        foreach ($this->toArray() as $name => $values) {
179
            if (isset($this->params[$name])
180
                && is_array($this->params[$name])
181
                && $this->params[$name]
182
            ) {
183
                $activeValues = [];
184
                
185
                foreach ($values as $value => $count) {
186
                    if (isset($this->params[$name][$value])) {
187
                        $activeValues[] = $value;
188
                    }
189
                }
190
                
191
                if ($activeValues) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $activeValues of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
192
                    $return[$name] = $activeValues;
193
                }
194
            }
195
        }
196
        
197
        return $return;
198
    }
199
    
200
    /**
201
     * @param string $name
202
     * @return string Non-translated title
203
     * @throws InvalidArgumentException
204
     */
205
    public function getTitle($name)
206
    {
207
        $this->assertValidName($name);
208
        
209
        return $this->definitions[$name]['title'];
210
    }
211
    
212
    /**
213
     * @param string $name
214
     * @throws InvalidArgumentException
215
     */
216
    protected function assertValidName($name)
217
    {
218
        if (! isset($this->definitions[$name])) {
219
            throw new InvalidArgumentException();
220
        }
221
    }
222
}