diff --git a/Aggregation/AbstractAggregation.php b/Aggregation/AbstractAggregation.php index 01cd487e9e721799b8b286ca2f45839a2621daf0..7ff124a91bd1a2376b184f42e7e6debe405566c9 100644 --- a/Aggregation/AbstractAggregation.php +++ b/Aggregation/AbstractAggregation.php @@ -24,17 +24,17 @@ abstract class AbstractAggregation implements NamedBuilderInterface /** * @var string */ - protected $field; + private $field; /** * @var string */ - protected $name; + private $name; /** * @var NamedBuilderBag */ - public $aggregations; + private $aggregations; /** * @return string @@ -88,6 +88,26 @@ abstract class AbstractAggregation implements NamedBuilderInterface return self::PREFIX . $this->name; } + /** + * Adds a sub-aggregation. + * + * @param AbstractAggregation $abstractAggregation + */ + public function addAggregation(AbstractAggregation $abstractAggregation) + { + $this->aggregations->add($abstractAggregation); + } + + /** + * Returns all sub aggregations. + * + * @return AbstractAggregation[] + */ + public function getAggregations() + { + return $this->aggregations->all(); + } + /** * {@inheritdoc} */ @@ -114,8 +134,7 @@ abstract class AbstractAggregation implements NamedBuilderInterface protected function collectNestedAggregations() { $result = []; - $nested = $this->aggregations->all(); - foreach ($nested as $aggregation) { + foreach ($this->aggregations->all() as $aggregation) { $result = array_merge($result, $aggregation->toArray()); } diff --git a/Aggregation/NestedAggregation.php b/Aggregation/NestedAggregation.php index df41315b59813171994dd60039351bb754be0584..d9d9038b966dba32a5a96acfad6b000220dbf683 100644 --- a/Aggregation/NestedAggregation.php +++ b/Aggregation/NestedAggregation.php @@ -58,7 +58,7 @@ class NestedAggregation extends AbstractAggregation */ public function getArray() { - if (count($this->aggregations) == 0) { + if (count($this->getAggregations()) == 0) { throw new \LogicException("Nested aggregation `{$this->getName()}` has no aggregations added"); }