From 44ea2aad1483593384097dcb00ca3442d85816d3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simonas=20=C5=A0erlinskas?= <simonas.serlinskas@nfq.com>
Date: Fri, 16 Mar 2018 19:01:33 +0100
Subject: [PATCH] fixed top hits sort deprecated issue

---
 docs/Aggregation/TopHits.md                   |  4 +-
 src/Aggregation/Metric/TopHitsAggregation.php | 67 +++++++++++++++----
 .../Metric/TopHitsAggregationTest.php         |  2 +-
 3 files changed, 57 insertions(+), 16 deletions(-)

diff --git a/docs/Aggregation/TopHits.md b/docs/Aggregation/TopHits.md
index e9e040c..2bd20c8 100644
--- a/docs/Aggregation/TopHits.md
+++ b/docs/Aggregation/TopHits.md
@@ -44,9 +44,7 @@ And now the query via DSL:
 
 ```php
 $sort = new FieldSort('last_activity_date', FieldSort::DESC);
-$sorts = new Sorts();
-$sorts->addSort($sort);
-$topHitsAggregation = new TopHitsAggregation('top_tag_hits', 1, null, $sorts);
+$topHitsAggregation = new TopHitsAggregation('top_tag_hits', 1, null, $sort);
 $topHitsAggregation->addParameter('_source', ['include' => ['title']]);
 
 $termsAggregation = new TermsAggregation('top-tags', 'tags');
diff --git a/src/Aggregation/Metric/TopHitsAggregation.php b/src/Aggregation/Metric/TopHitsAggregation.php
index 52933ab..af10e0c 100644
--- a/src/Aggregation/Metric/TopHitsAggregation.php
+++ b/src/Aggregation/Metric/TopHitsAggregation.php
@@ -35,9 +35,9 @@ class TopHitsAggregation extends AbstractAggregation
     private $from;
 
     /**
-     * @var BuilderInterface How the top matching hits should be sorted.
+     * @var BuilderInterface[] How the top matching hits should be sorted.
      */
-    private $sort;
+    private $sorts = [];
 
     /**
      * Constructor for top hits.
@@ -52,7 +52,7 @@ class TopHitsAggregation extends AbstractAggregation
         parent::__construct($name);
         $this->setFrom($from);
         $this->setSize($size);
-        $this->setSort($sort);
+        $this->addSort($sort);
     }
 
     /**
@@ -76,23 +76,29 @@ class TopHitsAggregation extends AbstractAggregation
     }
 
     /**
-     * Return sort.
-     *
-     * @return BuilderInterface
+     * @return BuilderInterface[]
      */
-    public function getSort()
+    public function getSorts()
     {
-        return $this->sort;
+        return $this->sorts;
     }
 
     /**
-     * Set sort.
+     * @param BuilderInterface[] $sorts
+     */
+    public function setSorts(array $sorts)
+    {
+        $this->sorts = $sorts;
+    }
+
+    /**
+     * Add sort.
      *
      * @param BuilderInterface $sort
      */
-    public function setSort($sort)
+    public function addSort($sort)
     {
-        $this->sort = $sort;
+        $this->sorts[] = $sort;
     }
 
     /**
@@ -128,9 +134,19 @@ class TopHitsAggregation extends AbstractAggregation
      */
     public function getArray()
     {
+        $sortsOutput = [];
+        $addedSorts = array_filter($this->getSorts());
+        if ($addedSorts) {
+            foreach ($addedSorts as $sort) {
+                $sortsOutput[] = $sort->toArray();
+            }
+        } else {
+            $sortsOutput = null;
+        }
+
         $output = array_filter(
             [
-                'sort' => $this->getSort() ? $this->getSort()->toArray() : null,
+                'sort' => $sortsOutput,
                 'size' => $this->getSize(),
                 'from' => $this->getFrom(),
             ],
@@ -141,4 +157,31 @@ class TopHitsAggregation extends AbstractAggregation
 
         return empty($output) ? new \stdClass() : $output;
     }
+
+    /**
+     * @deprecated sorts now is a container, use `getSorts()`instead.
+     * Return sort.
+     *
+     * @return BuilderInterface
+     */
+    public function getSort()
+    {
+        if (isset($this->sorts[0])) {
+            return $this->sorts[0];
+        }
+
+        return null;
+    }
+
+    /**
+     * @deprecated sorts now is a container, use `addSort()`instead.
+     *
+     * Set sort.
+     *
+     * @param BuilderInterface $sort
+     */
+    public function setSort(BuilderInterface $sort)
+    {
+        $this->sort = $sort;
+    }
 }
diff --git a/tests/Unit/Aggregation/Metric/TopHitsAggregationTest.php b/tests/Unit/Aggregation/Metric/TopHitsAggregationTest.php
index 53f6294..935346e 100644
--- a/tests/Unit/Aggregation/Metric/TopHitsAggregationTest.php
+++ b/tests/Unit/Aggregation/Metric/TopHitsAggregationTest.php
@@ -30,7 +30,7 @@ class TopHitsAggregationTest extends \PHPUnit_Framework_TestCase
         $expected = [
             'top_hits' => [
                 'sort' => [
-                    'acme' => ['order' => 'asc'],
+                    ['acme' => ['order' => 'asc']],
                 ],
                 'size' => 1,
                 'from' => 1,
-- 
GitLab