Skip to content
Snippets Groups Projects
Commit 6ad5ac08 authored by Aivaras Gotovskis's avatar Aivaras Gotovskis
Browse files

Add constructor arguments to RangeAggregation.

parent c1add492
No related branches found
No related tags found
No related merge requests found
...@@ -30,6 +30,28 @@ class RangeAggregation extends AbstractAggregation ...@@ -30,6 +30,28 @@ class RangeAggregation extends AbstractAggregation
*/ */
private $keyed = false; private $keyed = false;
/**
* Inner aggregations container init.
*
* @param string $name
* @param string $field
* @param array $ranges
* @param bool $keyed
*/
public function __construct($name, $field = null, $ranges = [], $keyed = false)
{
parent::__construct($name);
$this->setField($field);
$this->setKeyed($keyed);
foreach ($ranges as $range) {
$from = isset($range['from']) ? $range['from'] : null;
$to = isset($range['to']) ? $range['to'] : null;
$key = isset($range['key']) ? $range['key'] : null;
$this->addRange($from, $to, $key);
}
}
/** /**
* Sets if result buckets should be keyed. * Sets if result buckets should be keyed.
* *
......
...@@ -199,4 +199,29 @@ class RangeAggregationTest extends \PHPUnit_Framework_TestCase ...@@ -199,4 +199,29 @@ class RangeAggregationTest extends \PHPUnit_Framework_TestCase
$result = $aggregation->removeRange(500, 700); $result = $aggregation->removeRange(500, 700);
$this->assertFalse($result, 'returns false after removing not-existing range'); $this->assertFalse($result, 'returns false after removing not-existing range');
} }
/**
* Tests if parameter can be passed to constructor.
*/
public function testConstructor()
{
$aggregation = new RangeAggregation('foo', 'fieldValue', [['from' => 'now', 'key' => 'nowkey']], true);
$this->assertSame(
[
'agg_foo' => [
'range' => [
'keyed' => true,
'ranges' => [
[
'from' => 'now',
'key' => 'nowkey',
],
],
'field' => 'fieldValue',
],
],
],
$aggregation->toArray()
);
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment