Completed
Push — master ( 72c2fc...074e1b )
by Simonas
04:21
created

FiltersAggregation::addFilter()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 8.8571
cc 5
eloc 8
nc 3
nop 2
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing;
13
14
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
15
use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait;
16
use ONGR\ElasticsearchDSL\BuilderInterface;
17
18
/**
19
 * Class representing filters aggregation.
20
 *
21
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filters-aggregation.html
22
 */
23
class FiltersAggregation extends AbstractAggregation
24
{
25
    use BucketingTrait;
26
27
    /**
28
     * @var BuilderInterface[]
29
     */
30
    private $filters = [];
31
32
    /**
33
     * @var bool
34
     */
35
    private $anonymous = false;
36
37
    /**
38
     * Inner aggregations container init.
39
     *
40
     * @param string             $name
41
     * @param BuilderInterface[] $filters
42
     * @param bool               $anonymous
43
     */
44
    public function __construct($name, $filters = [], $anonymous = false)
45
    {
46
        parent::__construct($name);
47
48
        $this->setAnonymous($anonymous);
49
        foreach ($filters as $name => $filter) {
50
            if ($anonymous) {
51
                $this->addFilter($filter);
52
            } else {
53
                $this->addFilter($filter, $name);
54
            }
55
        }
56
    }
57
58
    /**
59
     * @param bool $anonymous
60
     *
61
     * @return FiltersAggregation
62
     */
63
    public function setAnonymous($anonymous)
64
    {
65
        $this->anonymous = $anonymous;
66
67
        return $this;
68
    }
69
70
    /**
71
     * @param BuilderInterface $filter
72
     * @param string           $name
73
     *
74
     * @throws \LogicException
75
     *
76
     * @return FiltersAggregation
77
     */
78
    public function addFilter(BuilderInterface $filter, $name = '')
79
    {
80
        if ($this->anonymous === false && empty($name)) {
81
            throw new \LogicException('In not anonymous filters filter name must be set.');
82
        } elseif ($this->anonymous === false && !empty($name)) {
83
            $this->filters['filters'][$name] = $filter->toArray();
84
        } else {
85
            $this->filters['filters'][] = $filter->toArray();
86
        }
87
88
        return $this;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function getArray()
95
    {
96
        return $this->filters;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function getType()
103
    {
104
        return 'filters';
105
    }
106
}
107