From 41e190a23c78f30c19724f9d8b9548dd4c71e0d9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mantas=20Jonu=C5=A1as?= <mantas.jonusas@nfq.lt>
Date: Mon, 13 Apr 2015 12:48:10 +0300
Subject: [PATCH] Changed getArray method in top hits aggregation

---
 Aggregation/TopHitsAggregation.php | 38 ++++++++++++++++++++----------
 1 file changed, 25 insertions(+), 13 deletions(-)

diff --git a/Aggregation/TopHitsAggregation.php b/Aggregation/TopHitsAggregation.php
index 17ad36e..125516c 100644
--- a/Aggregation/TopHitsAggregation.php
+++ b/Aggregation/TopHitsAggregation.php
@@ -49,9 +49,7 @@ class TopHitsAggregation extends AbstractAggregation
         parent::__construct($name);
         $this->setFrom($from);
         $this->setSize($size);
-        if (!empty($sort)) {
-            $this->setSort($sort);
-        }
+        $this->setSort($sort);
     }
 
     /**
@@ -129,18 +127,32 @@ class TopHitsAggregation extends AbstractAggregation
     {
         $data = new \stdClass();
 
-        if ($this->getSort()) {
-            $data->sort = $this->getSort()->toArray();
-        }
-
-        if ($this->getSize()) {
-            $data->size = $this->getSize();
-        }
-
-        if ($this->getFrom()) {
-            $data->from = $this->getFrom();
+        $filteredData = $this->getFilteredData();
+        foreach ($filteredData as $key => $value) {
+            $data->{$key} = $value;
         }
 
         return $data;
     }
+
+    /**
+     * Filters the data.
+     *
+     * @return array
+     */
+    private function getFilteredData()
+    {
+        $fd = array_filter(
+            [
+                'sort' => $this->getSort() ? $this->getSort()->toArray() : [],
+                'size' => $this->getSize(),
+                'from' => $this->getFrom(),
+            ],
+            function ($val) {
+                return (($val || is_array($val) || ($val || is_numeric($val))));
+            }
+        );
+
+        return $fd;
+    }
 }
-- 
GitLab