Completed
Pull Request — master (#157)
by
unknown
02:58
created

FilterAggregation::setField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
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 FilterAggregation.
20
 *
21
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html
22
 */
23
class FilterAggregation extends AbstractAggregation
24
{
25
    use BucketingTrait;
26
27
    /**
28
     * @var BuilderInterface
29
     */
30
    protected $filter;
31
32
    /**
33
     * Inner aggregations container init.
34
     *
35
     * @param string           $name
36
     * @param BuilderInterface $filter
37
     */
38
    public function __construct($name, BuilderInterface $filter = null)
39
    {
40
        parent::__construct($name);
41
42
        if ($filter !== null) {
43
            $this->setFilter($filter);
44
        }
45
    }
46
47
    /**
48
     * Sets a filter.
49
     *
50
     * @param BuilderInterface $filter
51
     */
52
    public function setFilter(BuilderInterface $filter)
53
    {
54
        $this->filter = $filter;
55
    }
56
57
    /**
58
     * Returns a filter.
59
     *
60
     * @return BuilderInterface
61
     */
62
    public function getFilter()
63
    {
64
        return $this->filter;
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70
    public function setField($field)
71
    {
72
        throw new \LogicException("Filter aggregation, doesn't support `field` parameter");
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function getArray()
79
    {
80
        if (!$this->filter) {
81
            throw new \LogicException("Filter aggregation `{$this->getName()}` has no filter added");
82
        }
83
84
        return $this->getFilter()->toArray();
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function getType()
91
    {
92
        return 'filter';
93
    }
94
}
95