Skip to content

Commit 55e427d

Browse files
committed
Allow empty aggregation
1 parent af0bfa4 commit 55e427d

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

src/Aggregation/AbstractAggregation.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ abstract class AbstractAggregation implements BuildsArray
1414
*/
1515
public function __construct(
1616
private string $name,
17-
array $aggregations = []
17+
array $aggregations = [],
1818
) {
1919
$this->aggregations = $aggregations;
2020
}
@@ -24,8 +24,14 @@ public function __construct(
2424
*/
2525
public function build(): array
2626
{
27+
$build = $this->buildAggregation();
28+
29+
if ([] === $build) {
30+
$build = new \stdClass();
31+
}
32+
2733
$data = [
28-
$this->getType() => $this->buildAggregation(),
34+
$this->getType() => $build,
2935
];
3036

3137
$this->buildAggregationsTo($data);

src/Aggregation/Aggregation.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Aggregation
1515
public static function terms(
1616
string $name,
1717
string|Field|InlineScript $fieldOrSource,
18-
array $aggregations = []
18+
array $aggregations = [],
1919
): TermsAggregation {
2020
return new TermsAggregation($name, $fieldOrSource, $aggregations);
2121
}
@@ -27,7 +27,7 @@ public static function dateHistogram(
2727
string $nameAndField,
2828
string $calendarInterval,
2929
?string $field = null,
30-
array $aggregations = []
30+
array $aggregations = [],
3131
): DateHistogramAggregation {
3232
return new DateHistogramAggregation($nameAndField, $calendarInterval, $field, $aggregations);
3333
}
@@ -43,7 +43,7 @@ public static function nested(string $name, string $path, array $aggregations =
4343
/**
4444
* @param array<AbstractAggregation> $aggregations
4545
*/
46-
public static function reverseNested(string $name, string $path, array $aggregations = []): ReverseNestedAggregation
46+
public static function reverseNested(string $name, ?string $path = null, array $aggregations = []): ReverseNestedAggregation
4747
{
4848
return new ReverseNestedAggregation($name, $path, $aggregations);
4949
}
@@ -62,7 +62,7 @@ public static function filter(string $name, QueryInterface $query, array $aggreg
6262
public static function cardinality(
6363
string $nameAndField,
6464
string|SourceScript|Field|null $fieldOrSource = null,
65-
array $aggregations = []
65+
array $aggregations = [],
6666
): CardinalityAggregation {
6767
return new CardinalityAggregation($nameAndField, $fieldOrSource, $aggregations);
6868
}
@@ -73,7 +73,7 @@ public static function cardinality(
7373
public static function max(
7474
string $nameAndField,
7575
string|SourceScript|Field|null $fieldOrSource = null,
76-
array $aggregations = []
76+
array $aggregations = [],
7777
): MaxAggregation {
7878
return new MaxAggregation($nameAndField, $fieldOrSource, $aggregations);
7979
}
@@ -84,7 +84,7 @@ public static function max(
8484
public static function min(
8585
string $nameAndField,
8686
string|SourceScript|Field|null $fieldOrSource = null,
87-
array $aggregations = []
87+
array $aggregations = [],
8888
): MinAggregation {
8989
return new MinAggregation($nameAndField, $fieldOrSource, $aggregations);
9090
}
@@ -95,7 +95,7 @@ public static function min(
9595
public static function sum(
9696
string $nameAndField,
9797
string|SourceScript|Field|null $fieldOrSource = null,
98-
array $aggregations = []
98+
array $aggregations = [],
9999
): SumAggregation {
100100
return new SumAggregation($nameAndField, $fieldOrSource, $aggregations);
101101
}

src/Aggregation/NestedAggregation.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class NestedAggregation extends AbstractAggregation
99
*/
1010
public function __construct(
1111
string $name,
12-
private string $path,
13-
array $aggregations = []
12+
private ?string $path = null,
13+
array $aggregations = [],
1414
) {
1515
parent::__construct($name, $aggregations);
1616
}
@@ -34,6 +34,10 @@ protected function getType(): string
3434

3535
protected function buildAggregation(): array
3636
{
37+
if (null === $this->path) {
38+
return [];
39+
}
40+
3741
return [
3842
'path' => $this->path,
3943
];

0 commit comments

Comments
 (0)