1 | <?php |
||
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; |
||
|
|||
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 |