Skip to content

Commit b7a0d52

Browse files
author
Martynas Sudintas
committed
Added sub aggregations setter in AbstractAggregation
1 parent 2e085f0 commit b7a0d52

13 files changed

+70
-18
lines changed

DSL/Aggregation/AbstractAggregation.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ abstract class AbstractAggregation implements NamedBuilderInterface
2424
/**
2525
* @var string
2626
*/
27-
protected $field;
27+
private $field;
2828

2929
/**
3030
* @var string
3131
*/
32-
protected $name;
32+
private $name;
3333

3434
/**
3535
* @var NamedBuilderBag
3636
*/
37-
public $aggregations;
37+
private $aggregations;
3838

3939
/**
4040
* @return string
@@ -88,6 +88,26 @@ public function getName()
8888
return self::PREFIX . $this->name;
8989
}
9090

91+
/**
92+
* Adds a sub-aggregation.
93+
*
94+
* @param AbstractAggregation $abstractAggregation
95+
*/
96+
public function addAggregation(AbstractAggregation $abstractAggregation)
97+
{
98+
$this->aggregations->add($abstractAggregation);
99+
}
100+
101+
/**
102+
* Returns all sub aggregations.
103+
*
104+
* @return AbstractAggregation[]
105+
*/
106+
public function getAggregations()
107+
{
108+
return $this->aggregations->all();
109+
}
110+
91111
/**
92112
* {@inheritdoc}
93113
*/
@@ -114,8 +134,7 @@ public function toArray()
114134
protected function collectNestedAggregations()
115135
{
116136
$result = [];
117-
$nested = $this->aggregations->all();
118-
foreach ($nested as $aggregation) {
137+
foreach ($this->aggregations->all() as $aggregation) {
119138
$result = array_merge($result, $aggregation->toArray());
120139
}
121140

DSL/Aggregation/NestedAggregation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function getType()
5858
*/
5959
public function getArray()
6060
{
61-
if (count($this->aggregations) == 0) {
61+
if (count($this->getAggregations()) == 0) {
6262
throw new \LogicException("Nested aggregation `{$this->getName()}` has no aggregations added");
6363
}
6464

Tests/Functional/DSL/Aggregation/FilterAggregationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function getFilterAggregationData()
7676

7777
$aggregation2 = new StatsAggregation('test_agg_2');
7878
$aggregation2->setField('price');
79-
$aggregation->aggregations->add($aggregation2);
79+
$aggregation->addAggregation($aggregation2);
8080

8181
$result = [
8282
'agg_test_agg' => [

Tests/Functional/DSL/Aggregation/GlobalAggregationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function getGlobalAggregationData()
7979
$aggregation2->setField('price');
8080
$aggregation2->addRange(null, 40);
8181

82-
$aggregation->aggregations->add($aggregation2);
82+
$aggregation->addAggregation($aggregation2);
8383

8484
// Case #0 global aggregation without query.
8585
$out[] = [

Tests/Functional/DSL/Aggregation/NestedAggregationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public function testNestedAggregation($aggregation, $expectedResult, $mapping)
169169
$nestedAggregation = new NestedAggregation('test_nested_agg');
170170
$nestedAggregation->setPath('sub_products');
171171

172-
$nestedAggregation->aggregations->add($aggregation);
172+
$nestedAggregation->addAggregation($aggregation);
173173

174174
$search = $repo->createSearch()->addAggregation($nestedAggregation);
175175
$results = $repo->execute($search, Repository::RESULTS_RAW);

Tests/Functional/DSL/Aggregation/RangeAggregationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public function testRangeAggregation($aggregation, $expectedResult)
159159
$aggregation2->setKeyed($childAgg['keyed']);
160160
$aggregation2->addRange($childAgg['range']['from'], $childAgg['range']['to'], $childAgg['key']);
161161

162-
$rangeAggregation->aggregations->add($aggregation2);
162+
$rangeAggregation->addAggregation($aggregation2);
163163
}
164164

165165
$search = $repo->createSearch()->addAggregation($rangeAggregation);

Tests/Functional/DSL/Aggregation/TopHitsAggregationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public function testTopHitsAggregationNested()
189189
$topAggregation = new TopHitsAggregation('test-top_hits');
190190
$termAggregation = new TermsAggregation('test_term');
191191
$termAggregation->setField('description');
192-
$termAggregation->aggregations->add($topAggregation);
192+
$termAggregation->addAggregation($topAggregation);
193193

194194
/** @var Repository $repo */
195195
$repo = $this->getManager()->getRepository('AcmeTestBundle:Product');

Tests/Functional/DSL/Complex/PostFilterAndAggregationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function testBoolWithFuzzyQueryAndSortFilter()
8989
$filters = $search->getPostFilters();
9090
$filterAgg->setFilter($filters);
9191

92-
$filterAgg->aggregations->add($TermsAgg);
92+
$filterAgg->addAggregation($TermsAgg);
9393

9494
$search->addAggregation($filterAgg);
9595

Tests/Functional/Result/AggregationIteratorFindTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ private function buildAggregation()
139139
$aggregation->setField('description');
140140
$aggregation2 = new StatsAggregation('test_agg_2');
141141
$aggregation2->setField('price');
142-
$aggregation->aggregations->add($aggregation2);
142+
$aggregation->addAggregation($aggregation2);
143143

144144
return $aggregation;
145145
}

Tests/Unit/DSL/Aggregation/FilterAggregationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function getToArrayData()
6969
->method('getName')
7070
->willReturn('agg_test_agg2');
7171

72-
$aggregation->aggregations->add($aggregation2);
72+
$aggregation->addAggregation($aggregation2);
7373

7474
$result = [
7575
'agg_test_agg' => [

0 commit comments

Comments
 (0)