Completed
Pull Request — master (#348)
by
unknown
01:15
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
    public function __construct(
30
        private string $name,
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_PRIVATE, expecting T_VARIABLE
Loading history...
31
        private array $filters = [],
32
        private bool $anonymous = false
33
    ) {
34
        parent::__construct($name);
35
36
        $this->setAnonymous($anonymous);
37
        foreach ($filters as $name => $filter) {
38
            if ($anonymous) {
39
                $this->addFilter($filter);
40
            } else {
41
                $this->addFilter($filter, $name);
42
            }
43
        }
44
    }
45
46
    public function setAnonymous(bool $anonymous): static
47
    {
48
        $this->anonymous = $anonymous;
49
50
        return $this;
51
    }
52
53
    public function addFilter(?BuilderInterface $filter = null, string $name = ''): static
54
    {
55
        if ($this->anonymous === false && empty($name)) {
56
            throw new \LogicException('In not anonymous filters filter name must be set.');
57
        } elseif ($this->anonymous === false && !empty($name)) {
58
            $this->filters['filters'][$name] = $filter->toArray();
59
        } else {
60
            $this->filters['filters'][] = $filter->toArray();
61
        }
62
63
        return $this;
64
    }
65
66
    public function getArray(): array
67
    {
68
        return $this->filters;
69
    }
70
71
    public function getType(): string
72
    {
73
        return 'filters';
74
    }
75
}
76