These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Elastica\Aggregation; |
||
4 | |||
5 | use Elastica\Exception\InvalidException; |
||
6 | use Elastica\Query\AbstractQuery; |
||
7 | |||
8 | /** |
||
9 | * Class Filters. |
||
10 | * |
||
11 | * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filters-aggregation.html |
||
12 | */ |
||
13 | class Filters extends AbstractAggregation |
||
14 | { |
||
15 | public const NAMED_TYPE = 1; |
||
16 | public const ANONYMOUS_TYPE = 2; |
||
17 | |||
18 | /** |
||
19 | * @var int Type of bucket keys - named, or anonymous |
||
20 | */ |
||
21 | private $_type; |
||
22 | |||
23 | /** |
||
24 | * Add a filter. |
||
25 | * |
||
26 | * If a name is given, it will be added as a key, otherwise considered as an anonymous filter |
||
27 | * |
||
28 | * @return $this |
||
29 | */ |
||
30 | public function addFilter(AbstractQuery $filter, ?string $name = null): self |
||
31 | { |
||
32 | $filterArray = []; |
||
33 | |||
34 | $type = self::NAMED_TYPE; |
||
35 | |||
36 | if (null === $name) { |
||
37 | $filterArray[] = $filter; |
||
38 | $type = self::ANONYMOUS_TYPE; |
||
39 | } else { |
||
40 | $filterArray[$name] = $filter; |
||
41 | } |
||
42 | |||
43 | if ($this->hasParam('filters') |
||
44 | && \count($this->getParam('filters')) |
||
45 | && $this->_type !== $type |
||
46 | ) { |
||
47 | throw new InvalidException('Mix named and anonymous keys are not allowed'); |
||
48 | } |
||
49 | |||
50 | $this->_type = $type; |
||
51 | |||
52 | return $this->addParam('filters', $filterArray); |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @return $this |
||
57 | */ |
||
58 | public function setOtherBucket(bool $otherBucket): self |
||
59 | { |
||
60 | return $this->setParam('other_bucket', $otherBucket); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @return $this |
||
65 | */ |
||
66 | public function setOtherBucketKey(string $otherBucketKey): self |
||
67 | { |
||
68 | return $this->setParam('other_bucket_key', $otherBucketKey); |
||
69 | } |
||
70 | |||
71 | public function toArray(): array |
||
72 | { |
||
73 | $array = []; |
||
74 | $filters = $this->getParam('filters'); |
||
75 | |||
76 | foreach ($filters as $filter) { |
||
77 | if (self::NAMED_TYPE === $this->_type) { |
||
78 | $key = \key($filter); |
||
79 | $array['filters']['filters'][$key] = \current($filter)->toArray(); |
||
80 | } else { |
||
81 | $array['filters']['filters'][] = \current($filter)->toArray(); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | if ($this->hasParam('other_bucket')) { |
||
86 | $array['filters']['other_bucket'] = $this->getParam('other_bucket'); |
||
87 | } |
||
88 | |||
89 | if ($this->hasParam('other_bucket_key')) { |
||
90 | $array['filters']['other_bucket_key'] = $this->getParam('other_bucket_key'); |
||
91 | } |
||
92 | |||
93 | if ($this->_aggs) { |
||
0 ignored issues
–
show
|
|||
94 | $array['aggs'] = $this->_convertArrayable($this->_aggs); |
||
95 | } |
||
96 | |||
97 | return $array; |
||
98 | } |
||
99 | } |
||
100 |
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.