Completed
Pull Request — develop (#284)
by
unknown
15:43
created

Facets::setFacetResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
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
                $this->cache[$name] = $this->facetResult[$definition['type']][$name];
97
            }
98
        }
99
        
100
        return $this->cache;
101
    }
102
    
103
    /**
104
     * @param ArrayAccess $facetResult
105
     * @return Facets
106
     */
107
    public function setFacetResult(ArrayAccess $facetResult)
108
    {
109
        $this->facetResult = $facetResult;
110
        $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...
111
        
112
        return $this;
113
    }
114
    /**
115
	 * @param array $params
116
	 * @return Facets
117
	 */
118
	public function setParams(array $params)
119
	{
120
		$this->params = $params;
121
		
122
		return $this;
123
	}
124
125
    /**
126
     * @param SolrDisMaxQuery $query
127
     * @return Facets
128
     */
129
    public function setupQuery(SolrDisMaxQuery $query)
130
    {
131
        $query->setFacet(true);
132
        
133
        foreach ($this->definitions as $name => $definition) {
134
            $method = $this->queryMethodMap[$definition['type']];
135
            $query->$method($name);
136
            
137
            if (isset($this->params[$name]) && is_array($this->params[$name])) {
138
                $valueList = array_filter(array_map(function ($value) {
139
                    return trim(SolrUtils::escapeQueryChars($value));
140
                }, array_keys($this->params[$name])));
141
                
142
                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...
143
                    $query->addFilterQuery(sprintf('%s:(%s)', $name, implode(' OR ', $valueList)));
144
                }
145
            }
146
        }
147
        
148
        return $this;
149
    }
150
    
151
    /**
152
     * @param string $name
153
     * @param string $value
154
     * @return boolean
155
     * @throws InvalidArgumentException
156
     */
157
    public function isValueActive($name, $value)
158
    {
159
        $this->assertValidName($name);
160
        
161
        return isset($this->params[$name])
162
            && is_array($this->params[$name])
163
            && isset($this->params[$name][$value]);
164
    }
165
    
166
    /**
167
     * @return array
168
     */
169
    public function getActiveValues()
170
    {
171
        $return = [];
172
        
173
        foreach ($this->toArray() as $name => $values) {
174
            if (isset($this->params[$name])
175
                && is_array($this->params[$name])
176
                && $this->params[$name]
177
            ) {
178
                $activeValues = [];
179
                
180
                foreach ($values as $value => $count) {
181
                    if (isset($this->params[$name][$value])) {
182
                        $activeValues[] = $value;
183
                    }
184
                }
185
                
186
                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...
187
                    $return[$name] = $activeValues;
188
                }
189
            }
190
        }
191
        
192
        return $return;
193
    }
194
    
195
    /**
196
     * @param string $name
197
     * @return string Non-translated title
198
     * @throws InvalidArgumentException
199
     */
200
    public function getTitle($name)
201
    {
202
        $this->assertValidName($name);
203
        
204
        return $this->definitions[$name]['title'];
205
    }
206
    
207
    /**
208
     * @param string $name
209
     * @throws InvalidArgumentException
210
     */
211
    protected function assertValidName($name)
212
    {
213
        if (! isset($this->definitions[$name])) {
214
            throw new InvalidArgumentException();
215
        }
216
    }
217
}