Completed
Push — master ( d3ac62...0cb203 )
by Federico
02:08
created

lib/Elastica/Aggregation/Filters.php (1 issue)

Upgrade to new PHP Analysis Engine

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
    const NAMED_TYPE = 1;
16
    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
     * @param AbstractQuery $filter
29
     * @param string        $name
30
     *
31
     * @return $this
32
     */
33
    public function addFilter(AbstractQuery $filter, $name = null)
34
    {
35
        if (null !== $name && !is_string($name)) {
36
            throw new InvalidException('Name must be a string');
37
        }
38
39
        $filterArray = [];
40
41
        $type = self::NAMED_TYPE;
42
43
        if (null === $name) {
44
            $filterArray[] = $filter;
45
            $type = self::ANONYMOUS_TYPE;
46
        } else {
47
            $filterArray[$name] = $filter;
48
        }
49
50
        if ($this->hasParam('filters')
51
            && count($this->getParam('filters'))
52
            && $this->_type !== $type
53
        ) {
54
            throw new InvalidException('Mix named and anonymous keys are not allowed');
55
        }
56
57
        $this->_type = $type;
58
59
        return $this->addParam('filters', $filterArray);
60
    }
61
62
    /**
63
     * @param bool $otherBucket
64
     *
65
     * @return $this
66
     */
67
    public function setOtherBucket($otherBucket)
68
    {
69
        if (!is_bool($otherBucket)) {
70
            throw new \InvalidArgumentException('other_bucket only supports boolean values');
71
        }
72
73
        return $this->setParam('other_bucket', $otherBucket);
74
    }
75
76
    /**
77
     * @param string $otherBucketKey
78
     *
79
     * @return $this
80
     */
81
    public function setOtherBucketKey($otherBucketKey)
82
    {
83
        return $this->setParam('other_bucket_key', $otherBucketKey);
84
    }
85
86
    /**
87
     * @return array
88
     */
89
    public function toArray()
90
    {
91
        $array = [];
92
        $filters = $this->getParam('filters');
93
94
        foreach ($filters as $filter) {
95
            if (self::NAMED_TYPE === $this->_type) {
96
                $key = key($filter);
97
                $array['filters']['filters'][$key] = current($filter)->toArray();
98
            } else {
99
                $array['filters']['filters'][] = current($filter)->toArray();
100
            }
101
        }
102
103
        if ($this->hasParam('other_bucket')) {
104
            $array['filters']['other_bucket'] = $this->getParam('other_bucket');
105
        }
106
107
        if ($this->hasParam('other_bucket_key')) {
108
            $array['filters']['other_bucket_key'] = $this->getParam('other_bucket_key');
109
        }
110
111
        if ($this->_aggs) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->_aggs 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...
112
            $array['aggs'] = $this->_convertArrayable($this->_aggs);
113
        }
114
115
        return $array;
116
    }
117
}
118