Skip to content

Commit

Permalink
parent aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
hkulekci committed Nov 15, 2024
1 parent d61ea65 commit a4dcb83
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 0 deletions.
83 changes: 83 additions & 0 deletions src/Aggregation/Bucketing/ParentAggregation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <[email protected]>
*
* 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()];
}
}
56 changes: 56 additions & 0 deletions tests/Unit/Aggregation/Bucketing/ParentAggregationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <[email protected]>
*
* 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);
}
}

0 comments on commit a4dcb83

Please sign in to comment.