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

CompositeAggregation   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 2
dl 0
loc 130
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
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 composite aggregation.
22
 *
23
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html
24
 */
25
class CompositeAggregation extends AbstractAggregation
26
{
27
    use BucketingTrait;
28
29
    private ?int $size = 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...
30
31
    private array $after = [];
32
33
    private array $sources = [];
34
35
    public function __construct(private string $name, array $sources = [])
36
    {
37
        parent::__construct($name);
38
39
        foreach ($sources as $agg) {
40
            $this->addSource($agg);
41
        }
42
    }
43
44
    public function addSource(AbstractAggregation $agg): static
45
    {
46
        $array = $agg->getArray();
47
48
        $array = is_array($array) ? array_merge($array, $agg->getParameters()) : $array;
49
50
        $this->sources[] = [
51
            $agg->getName() => [ $agg->getType() => $array ]
52
        ];
53
54
        return $this;
55
    }
56
57
    public function getArray(): array
58
    {
59
        $array = [
60
            'sources' => $this->sources,
61
        ];
62
63
        if ($this->size !== null) {
64
            $array['size'] = $this->size;
65
        }
66
67
        if (!empty($this->after)) {
68
            $array['after'] = $this->after;
69
        }
70
71
        return $array;
72
    }
73
74
    public function getType(): string
75
    {
76
        return 'composite';
77
    }
78
79
    public function setSize(?int $size): static
80
    {
81
        $this->size = $size;
82
83
        return $this;
84
    }
85
86
    public function getSize(): ?int
87
    {
88
        return $this->size;
89
    }
90
91
    public function setAfter(array $after): static
92
    {
93
        $this->after = $after;
94
95
        return $this;
96
    }
97
98
    public function getAfter(): array
99
    {
100
        return $this->after;
101
    }
102
}
103