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

lib/Elastica/Aggregation/Filter.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 Filter.
10
 *
11
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html
12
 */
13
class Filter extends AbstractAggregation
14
{
15
    /**
16
     * @param string        $name
17
     * @param AbstractQuery $filter
18
     */
19
    public function __construct($name, AbstractQuery $filter = null)
20
    {
21
        parent::__construct($name);
22
23
        if (null !== $filter) {
24
            $this->setFilter($filter);
25
        }
26
    }
27
28
    /**
29
     * Set the filter for this aggregation.
30
     *
31
     * @param AbstractQuery $filter
32
     *
33
     * @return $this
34
     */
35
    public function setFilter(AbstractQuery $filter)
36
    {
37
        return $this->setParam('filter', $filter);
38
    }
39
40
    /**
41
     * @throws \Elastica\Exception\InvalidException If filter is not set
42
     *
43
     * @return array
44
     */
45
    public function toArray()
46
    {
47
        if (!$this->hasParam('filter')) {
48
            throw new InvalidException('Filter is required');
49
        }
50
51
        $array = [
52
            'filter' => $this->getParam('filter')->toArray(),
53
        ];
54
55
        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...
56
            $array['aggs'] = $this->_convertArrayable($this->_aggs);
57
        }
58
59
        return $array;
60
    }
61
}
62