diff --git a/src/Aggregation/Pipeline/BucketSortAggregation.php b/src/Aggregation/Pipeline/BucketSortAggregation.php new file mode 100644 index 0000000000000000000000000000000000000000..3b04ee3d87c3ca2ce1dd403c1eb88dc03ebb38af --- /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 0000000000000000000000000000000000000000..098c7d92ddc0f04dd2c9c56297632f6836d33e32 --- /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()); + } +}