Skip to content

Commit 548cc71

Browse files
committed
Merge pull request #158 from martiis/agg_setter
Added sub aggregations setter in AbstractAggregation
2 parents 2e085f0 + 3bc7ddd commit 548cc71

14 files changed

+74
-22
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->getAggregations() 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

DSL/NamedBuilderBag.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,18 @@ public function get($name)
9696
/**
9797
* Returns all builders contained.
9898
*
99-
* @param string|null $name Builder name.
99+
* @param string|null $type Builder type.
100100
*
101101
* @return NamedBuilderInterface[]
102102
*/
103-
public function all($name = null)
103+
public function all($type = null)
104104
{
105105
return array_filter(
106106
$this->bag,
107-
function ($builder) use ($name) {
107+
function ($builder) use ($type) {
108108
/** @var NamedBuilderInterface $builder */
109109

110-
return $name === null || $builder->getName() == $name;
110+
return $type === null || $builder->getType() == $type;
111111
}
112112
);
113113
}

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
}

0 commit comments

Comments
 (0)