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

Add constructor arguments to Ipv4RangeAggregation.

parent 7e097d70
No related branches found
No related tags found
No related merge requests found
......@@ -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.
*
......
......@@ -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()
);
}
}
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