From 2f8e323edea6e4601b431c4294fd7598a4d1e993 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Simonas=20=C5=A0erlinskas?= <simonas.serlinskas@nfq.com>
Date: Thu, 24 Jan 2019 16:36:38 +0200
Subject: [PATCH] add bucket sort aggregation

---
 .../Pipeline/BucketSortAggregation.php        | 86 +++++++++++++++++++
 .../Pipeline/BucketSortAggregationTest.php    | 62 +++++++++++++
 2 files changed, 148 insertions(+)
 create mode 100644 src/Aggregation/Pipeline/BucketSortAggregation.php
 create mode 100644 tests/Unit/Aggregation/Pipeline/BucketSortAggregationTest.php

diff --git a/src/Aggregation/Pipeline/BucketSortAggregation.php b/src/Aggregation/Pipeline/BucketSortAggregation.php
new file mode 100644
index 0000000..3b04ee3
--- /dev/null
+++ b/src/Aggregation/Pipeline/BucketSortAggregation.php
@@ -0,0 +1,86 @@
+<?php
+
+/*
+ * This file is part of the ONGR package.
+ *
+ * (c) NFQ Technologies UAB <info@nfq.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace ONGR\ElasticsearchDSL\Aggregation\Pipeline;
+
+use ONGR\ElasticsearchDSL\BuilderInterface;
+use ONGR\ElasticsearchDSL\Sort\FieldSort;
+
+/**
+ * Class representing Bucket Script Pipeline Aggregation.
+ *
+ * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-sort-aggregation.html
+ */
+class BucketSortAggregation extends AbstractPipelineAggregation
+{
+    /**
+     * @var array
+     */
+    private $sort = [];
+
+    /**
+     * @param string $name
+     * @param string  $bucketsPath
+     */
+    public function __construct($name, $bucketsPath = null)
+    {
+        parent::__construct($name, $bucketsPath);
+    }
+
+    /**
+     * @return array
+     */
+    public function getSort()
+    {
+        return $this->sort;
+    }
+
+    /**
+     * @return self
+     */
+    public function addSort(FieldSort $sort)
+    {
+        $this->sort[] = $sort->toArray();
+    }
+
+    /**
+     * @param string $sort
+     * @return self
+     */
+    public function setSort($sort)
+    {
+        $this->sort = $sort;
+        return $this;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getType()
+    {
+        return 'bucket_sort';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getArray()
+    {
+        $out = array_filter(
+            [
+            'buckets_path' => $this->getBucketsPath(),
+            'sort' => $this->getSort(),
+            ]
+        );
+
+        return $out;
+    }
+}
diff --git a/tests/Unit/Aggregation/Pipeline/BucketSortAggregationTest.php b/tests/Unit/Aggregation/Pipeline/BucketSortAggregationTest.php
new file mode 100644
index 0000000..098c7d9
--- /dev/null
+++ b/tests/Unit/Aggregation/Pipeline/BucketSortAggregationTest.php
@@ -0,0 +1,62 @@
+<?php
+
+/*
+ * This file is part of the ONGR package.
+ *
+ * (c) NFQ Technologies UAB <info@nfq.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace ONGR\ElasticsearchDSL\Tests\Unit\Aggregation\Pipeline;
+
+use ONGR\ElasticsearchDSL\Aggregation\Pipeline\BucketSortAggregation;
+use ONGR\ElasticsearchDSL\Aggregation\Pipeline\MovingFunctionAggregation;
+use ONGR\ElasticsearchDSL\Sort\FieldSort;
+
+/**
+ * Unit test for the bucket sort aggregation.
+ */
+class BucketSortAggregationTest extends \PHPUnit\Framework\TestCase
+{
+    /**
+     * Tests toArray method.
+     */
+    public function testToArray()
+    {
+        $aggregation = new BucketSortAggregation('acme', 'test');
+
+        $expected = [
+            'bucket_sort' => [
+                'buckets_path' => 'test',
+            ],
+        ];
+
+        $this->assertEquals($expected, $aggregation->toArray());
+
+        $aggregation = new BucketSortAggregation('acme');
+
+        $expected = [
+            'bucket_sort' => [],
+        ];
+
+        $this->assertEquals($expected, $aggregation->toArray());
+
+        $aggregation = new BucketSortAggregation('acme');
+        $sort = new FieldSort('test_field', FieldSort::ASC);
+        $aggregation->addSort($sort);
+
+        $expected = [
+            'bucket_sort' => [
+                'sort' => [
+                    [
+                        'test_field' => ['order' => 'asc'],
+                    ]
+                ]
+            ],
+        ];
+
+        $this->assertEquals($expected, $aggregation->toArray());
+    }
+}
-- 
GitLab