Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parent Aggregation Support #34

Open
wants to merge 1 commit into
base: 8.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <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\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 <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\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);
}
}
Loading