From a4dcb834426f019209872b53147ca5c4d80c337b Mon Sep 17 00:00:00 2001 From: Haydar Kulekci Date: Fri, 15 Nov 2024 19:16:43 +0300 Subject: [PATCH] parent aggregation --- .../Bucketing/ParentAggregation.php | 83 +++++++++++++++++++ .../Bucketing/ParentAggregationTest.php | 56 +++++++++++++ 2 files changed, 139 insertions(+) create mode 100644 src/Aggregation/Bucketing/ParentAggregation.php create mode 100644 tests/Unit/Aggregation/Bucketing/ParentAggregationTest.php diff --git a/src/Aggregation/Bucketing/ParentAggregation.php b/src/Aggregation/Bucketing/ParentAggregation.php new file mode 100644 index 00000000..c92f2475 --- /dev/null +++ b/src/Aggregation/Bucketing/ParentAggregation.php @@ -0,0 +1,83 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; + +use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; +use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait; + +/** + * Class representing ChildrenAggregation. + * + * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-children-aggregation.html + */ +class ParentAggregation extends AbstractAggregation +{ + use BucketingTrait; + + /** + * @var string + */ + private $parent; + + /** + * Return children. + * + * @return string + */ + public function getParent() + { + return $this->parent; + } + + /** + * @param string $name + * @param string $parent + */ + public function __construct($name, $parent = null) + { + parent::__construct($name); + + $this->setParent($parent); + } + + /** + * @param string $parent + * + * @return $this + */ + public function setParent($parent) + { + $this->parent = $parent; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'parent'; + } + + /** + * {@inheritdoc} + */ + public function getArray() + { + if (count($this->getAggregations()) == 0) { + throw new \LogicException("Parent aggregation `{$this->getName()}` has no aggregations added"); + } + + return ['type' => $this->getParent()]; + } +} diff --git a/tests/Unit/Aggregation/Bucketing/ParentAggregationTest.php b/tests/Unit/Aggregation/Bucketing/ParentAggregationTest.php new file mode 100644 index 00000000..26abea2a --- /dev/null +++ b/tests/Unit/Aggregation/Bucketing/ParentAggregationTest.php @@ -0,0 +1,56 @@ + + * + * 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\Bucketing; + +use ONGR\ElasticsearchDSL\Aggregation\Bucketing\ParentAggregation; + +/** + * Unit test for parent aggregation. + */ +class ParentAggregationTest extends \PHPUnit\Framework\TestCase +{ + /** + * Tests if ParentAggregation#getArray throws exception when expected. + */ + public function testGetArrayException() + { + $this->expectException(\LogicException::class); + $aggregation = new ParentAggregation('foo'); + $aggregation->getArray(); + } + + /** + * Tests getType method. + */ + public function testParentAggregationGetType() + { + $aggregation = new ParentAggregation('foo'); + $result = $aggregation->getType(); + $this->assertEquals('parent', $result); + } + + /** + * Tests getArray method. + */ + public function testParentAggregationGetArray() + { + $mock = $this->getMockBuilder('ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation') + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + $aggregation = new ParentAggregation('foo'); + $aggregation->addAggregation($mock); + $aggregation->setParent('question'); + $result = $aggregation->getArray(); + $expected = ['type' => 'question']; + $this->assertEquals($expected, $result); + } +}