Skip to content
Snippets Groups Projects
Unverified Commit e4321032 authored by Simonas Šerlinskas's avatar Simonas Šerlinskas
Browse files

Merge branch 'pmishev-2019-06-27-daterangeaggregation' into 6.x

parents 63a15e3c 5485408f
No related branches found
No related tags found
No related merge requests found
...@@ -29,25 +29,29 @@ class DateRangeAggregation extends AbstractAggregation ...@@ -29,25 +29,29 @@ class DateRangeAggregation extends AbstractAggregation
private $format; private $format;
/** /**
* @return string * @var array
*/ */
public function getFormat() private $ranges = [];
{
return $this->format; /**
} * @var bool
*/
private $keyed = false;
/** /**
* @param string $name * @param string $name
* @param string $field * @param string $field
* @param string $format * @param string $format
* @param array $ranges * @param array $ranges
* @param bool $keyed
*/ */
public function __construct($name, $field = null, $format = null, array $ranges = []) public function __construct($name, $field = null, $format = null, array $ranges = [], $keyed = false)
{ {
parent::__construct($name); parent::__construct($name);
$this->setField($field); $this->setField($field);
$this->setFormat($format); $this->setFormat($format);
$this->setKeyed($keyed);
foreach ($ranges as $range) { foreach ($ranges as $range) {
$from = isset($range['from']) ? $range['from'] : null; $from = isset($range['from']) ? $range['from'] : null;
$to = isset($range['to']) ? $range['to'] : null; $to = isset($range['to']) ? $range['to'] : null;
...@@ -57,27 +61,41 @@ class DateRangeAggregation extends AbstractAggregation ...@@ -57,27 +61,41 @@ class DateRangeAggregation extends AbstractAggregation
} }
/** /**
* @param string $format * Sets if result buckets should be keyed.
* *
* @return $this * @param bool $keyed
*
* @return DateRangeAggregation
*/ */
public function setFormat($format) public function setKeyed($keyed)
{ {
$this->format = $format; $this->keyed = $keyed;
return $this; return $this;
} }
/** /**
* @var array * @return string
*/ */
private $ranges = []; public function getFormat()
{
return $this->format;
}
/**
* @param string $format
*/
public function setFormat($format)
{
$this->format = $format;
}
/** /**
* Add range to aggregation. * Add range to aggregation.
* *
* @param string|null $from * @param string|null $from
* @param string|null $to * @param string|null $to
* @param string|null $key
* *
* @return $this * @return $this
* *
...@@ -115,6 +133,7 @@ class DateRangeAggregation extends AbstractAggregation ...@@ -115,6 +133,7 @@ class DateRangeAggregation extends AbstractAggregation
'format' => $this->getFormat(), 'format' => $this->getFormat(),
'field' => $this->getField(), 'field' => $this->getField(),
'ranges' => $this->ranges, 'ranges' => $this->ranges,
'keyed' => $this->keyed,
]; ];
return $data; return $data;
......
...@@ -47,11 +47,13 @@ class DateRangeAggregationTest extends \PHPUnit\Framework\TestCase ...@@ -47,11 +47,13 @@ class DateRangeAggregationTest extends \PHPUnit\Framework\TestCase
$agg = new DateRangeAggregation('foo', 'baz'); $agg = new DateRangeAggregation('foo', 'baz');
$agg->addRange(10, 20); $agg->addRange(10, 20);
$agg->setFormat('bar'); $agg->setFormat('bar');
$agg->setKeyed(true);
$result = $agg->getArray(); $result = $agg->getArray();
$expected = [ $expected = [
'format' => 'bar', 'format' => 'bar',
'field' => 'baz', 'field' => 'baz',
'ranges' => [['from' => 10, 'to' => 20]], 'ranges' => [['from' => 10, 'to' => 20]],
'keyed' => true,
]; ];
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
......
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