Completed
Pull Request — master (#348)
by
unknown
10:14
created

FiltersAggregation   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 84
c 0
b 0
f 0
rs 10
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
declare(strict_types=1);
13
14
namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing;
15
16
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
17
use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait;
18
use ONGR\ElasticsearchDSL\BuilderInterface;
19
20
/**
21
 * Class representing filters aggregation.
22
 *
23
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filters-aggregation.html
24
 */
25
class FiltersAggregation extends AbstractAggregation
26
{
27
    use BucketingTrait;
28
29
    private array $filters = [];
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
30
31
    private bool $anonymous = false;
32
33
    public function __construct(
34
        string $name,
35
        ?array $filters = [],
36
         bool $anonymous = false
37
    ) {
38
        parent::__construct($name);
39
40
        $this->setAnonymous($anonymous);
41
        foreach ($filters as $name => $filter) {
42
            if ($anonymous) {
43
                $this->addFilter($filter);
44
            } else {
45
                $this->addFilter($filter, $name);
46
            }
47
        }
48
    }
49
50
    public function setAnonymous(bool $anonymous): static
51
    {
52
        $this->anonymous = $anonymous;
53
54
        return $this;
55
    }
56
57
    public function addFilter(?BuilderInterface $filter = null, string $name = ''): static
58
    {
59
        if ($this->anonymous === false && empty($name)) {
60
            throw new \LogicException('In not anonymous filters filter name must be set.');
61
        } elseif ($this->anonymous === false && !empty($name)) {
62
            $this->filters['filters'][$name] = $filter->toArray();
63
        } else {
64
            $this->filters['filters'][] = $filter->toArray();
65
        }
66
67
        return $this;
68
    }
69
70
    public function getArray(): ?array
71
    {
72
        return $this->filters;
73
    }
74
75
    public function getType(): string
76
    {
77
        return 'filters';
78
    }
79
}
80