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

AbstractAggregation   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 3
dl 0
loc 151
rs 10
c 0
b 0
f 0
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;
13
14
use ONGR\ElasticsearchDSL\BuilderBag;
15
use ONGR\ElasticsearchDSL\BuilderInterface;
16
use ONGR\ElasticsearchDSL\NameAwareTrait;
17
use ONGR\ElasticsearchDSL\NamedBuilderInterface;
18
use ONGR\ElasticsearchDSL\ParametersTrait;
19
use stdClass;
20
21
/**
22
 * AbstractAggregation class.
23
 */
24
abstract class AbstractAggregation implements NamedBuilderInterface
25
{
26
    use ParametersTrait;
27
28
    use NameAwareTrait;
29
30
    private ?string $field = null;
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 '?', expecting T_FUNCTION or T_CONST
Loading history...
31
32
    private ?BuilderBag $aggregations = null;
33
34
    abstract protected function supportsNesting(): bool;
35
36
    abstract protected function getArray(): array|stdClass|null;
37
38
    public function __construct(string $name)
39
    {
40
        $this->setName($name);
41
    }
42
43
    public function setField(?string $field): static
44
    {
45
        $this->field = $field;
46
47
        return $this;
48
    }
49
50
    public function getField(): ?string
51
    {
52
        return $this->field;
53
    }
54
55
    public function addAggregation(AbstractAggregation $abstractAggregation): static
56
    {
57
        if (!$this->aggregations) {
58
            $this->aggregations = $this->createBuilderBag();
59
        }
60
61
        $this->aggregations->add($abstractAggregation);
62
63
        return $this;
64
    }
65
66
    /**
67
     * Returns all sub aggregations.
68
     *
69
     * @return BuilderBag[]|NamedBuilderInterface[]
70
     */
71
    public function getAggregations(): array
72
    {
73
        if ($this->aggregations) {
74
            return $this->aggregations->all();
75
        }
76
77
        return [];
78
    }
79
80
    public function getAggregation(string $name): AbstractAggregation|BuilderInterface|null
81
    {
82
        if ($this->aggregations && $this->aggregations->has($name)) {
83
            return $this->aggregations->get($name);
84
        }
85
86
        return null;
87
    }
88
89
    public function toArray(): array
90
    {
91
        $array = $this->getArray();
92
        $result = [
93
            $this->getType() => is_array($array) ? $this->processArray($array) : $array,
94
        ];
95
96
        if ($this->supportsNesting()) {
97
            $nestedResult = $this->collectNestedAggregations();
98
99
            if (!empty($nestedResult)) {
100
                $result['aggregations'] = $nestedResult;
101
            }
102
        }
103
104
        return $result;
105
    }
106
107
    protected function collectNestedAggregations(): array
108
    {
109
        $result = [];
110
        /** @var AbstractAggregation $aggregation */
111
        foreach ($this->getAggregations() as $aggregation) {
112
            $result[$aggregation->getName()] = $aggregation->toArray();
113
        }
114
115
        return $result;
116
    }
117
118
    private function createBuilderBag(): BuilderBag
119
    {
120
        return new BuilderBag();
121
    }
122
}
123