From b164a5f5e0fcde466dd2d3cddc9bcc945ab0d713 Mon Sep 17 00:00:00 2001 From: Aivaras Gotovskis <aivaras.gotovskis@ongr.io> Date: Thu, 9 Jul 2015 15:58:49 +0300 Subject: [PATCH] Add constructor arguments to Ipv4RangeAggregation. --- src/Aggregation/Ipv4RangeAggregation.php | 23 +++++++++++++ .../Aggregation/Ipv4RangeAggregationTest.php | 32 +++++++++++++++++++ 2 files changed, 55 insertions(+) diff --git a/src/Aggregation/Ipv4RangeAggregation.php b/src/Aggregation/Ipv4RangeAggregation.php index 917c7a8..9b486b7 100644 --- a/src/Aggregation/Ipv4RangeAggregation.php +++ b/src/Aggregation/Ipv4RangeAggregation.php @@ -25,6 +25,29 @@ class Ipv4RangeAggregation extends AbstractAggregation */ private $ranges = []; + /** + * Inner aggregations container init. + * + * @param string $name + * @param string $field + * @param array $ranges + */ + public function __construct($name, $field = null, $ranges = []) + { + parent::__construct($name); + + $this->setField($field); + foreach ($ranges as $range) { + if (is_array($range)) { + $from = isset($range['from']) ? $range['from'] : null; + $to = isset($range['to']) ? $range['to'] : null; + $this->addRange($from, $to); + } else { + $this->addMask($range); + } + } + } + /** * Add range to aggregation. * diff --git a/tests/Aggregation/Ipv4RangeAggregationTest.php b/tests/Aggregation/Ipv4RangeAggregationTest.php index eb3a27e..7542584 100644 --- a/tests/Aggregation/Ipv4RangeAggregationTest.php +++ b/tests/Aggregation/Ipv4RangeAggregationTest.php @@ -25,4 +25,36 @@ class Ipv4RangeAggregationTest extends \PHPUnit_Framework_TestCase $agg = new Ipv4RangeAggregation('foo'); $agg->toArray(); } + + /** + * Tests if field and range can be passed to constructor. + */ + public function testConstructorFilter() + { + $aggregation = new Ipv4RangeAggregation('test', 'fieldName', [['from' => 'fromValue']]); + $this->assertSame( + [ + 'agg_test' => [ + 'ip_range' => [ + 'field' => 'fieldName', + 'ranges' => [['from' => 'fromValue']], + ], + ], + ], + $aggregation->toArray() + ); + + $aggregation = new Ipv4RangeAggregation('test', 'fieldName', ['maskValue']); + $this->assertSame( + [ + 'agg_test' => [ + 'ip_range' => [ + 'field' => 'fieldName', + 'ranges' => [['mask' => 'maskValue']], + ], + ], + ], + $aggregation->toArray() + ); + } } -- GitLab