From f40fd1309ea34760cb9a59e14df5de8b924c0472 Mon Sep 17 00:00:00 2001
From: Martynas Sudintas <martynas.sudintas@nfq.lt>
Date: Wed, 28 Jan 2015 11:25:00 +0200
Subject: [PATCH] Added sub aggregations setter in AbstractAggregation

---
 Aggregation/AbstractAggregation.php | 29 ++++++++++++++++++++++++-----
 Aggregation/NestedAggregation.php   |  2 +-
 2 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/Aggregation/AbstractAggregation.php b/Aggregation/AbstractAggregation.php
index 01cd487..7ff124a 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 df41315..d9d9038 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");
         }
 
-- 
GitLab