diff --git a/src/Aggregation/RangeAggregation.php b/src/Aggregation/RangeAggregation.php index 41e107569998baaf9fd7396005e294e6077ebc48..557e9c49d4a48d39dbece5f413547e65f85d5b5b 100644 --- a/src/Aggregation/RangeAggregation.php +++ b/src/Aggregation/RangeAggregation.php @@ -30,6 +30,28 @@ class RangeAggregation extends AbstractAggregation */ 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. * diff --git a/tests/Aggregation/RangeAggregationTest.php b/tests/Aggregation/RangeAggregationTest.php index 9414ea5d368bf703172415a1f78878a102e024c8..c66244d0811bdfe58b7ac11e09ad6d84b41a25c9 100644 --- a/tests/Aggregation/RangeAggregationTest.php +++ b/tests/Aggregation/RangeAggregationTest.php @@ -199,4 +199,29 @@ class RangeAggregationTest extends \PHPUnit_Framework_TestCase $result = $aggregation->removeRange(500, 700); $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() + ); + } }