Skip to content

Commit e432103

Browse files
committed
Merge branch 'pmishev-2019-06-27-daterangeaggregation' into 6.x
2 parents 63a15e3 + 5485408 commit e432103

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

src/Aggregation/Bucketing/DateRangeAggregation.php

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,29 @@ class DateRangeAggregation extends AbstractAggregation
2929
private $format;
3030

3131
/**
32-
* @return string
32+
* @var array
3333
*/
34-
public function getFormat()
35-
{
36-
return $this->format;
37-
}
34+
private $ranges = [];
35+
36+
/**
37+
* @var bool
38+
*/
39+
private $keyed = false;
3840

3941
/**
4042
* @param string $name
4143
* @param string $field
4244
* @param string $format
4345
* @param array $ranges
46+
* @param bool $keyed
4447
*/
45-
public function __construct($name, $field = null, $format = null, array $ranges = [])
48+
public function __construct($name, $field = null, $format = null, array $ranges = [], $keyed = false)
4649
{
4750
parent::__construct($name);
4851

4952
$this->setField($field);
5053
$this->setFormat($format);
54+
$this->setKeyed($keyed);
5155
foreach ($ranges as $range) {
5256
$from = isset($range['from']) ? $range['from'] : null;
5357
$to = isset($range['to']) ? $range['to'] : null;
@@ -57,27 +61,41 @@ public function __construct($name, $field = null, $format = null, array $ranges
5761
}
5862

5963
/**
60-
* @param string $format
64+
* Sets if result buckets should be keyed.
6165
*
62-
* @return $this
66+
* @param bool $keyed
67+
*
68+
* @return DateRangeAggregation
6369
*/
64-
public function setFormat($format)
70+
public function setKeyed($keyed)
6571
{
66-
$this->format = $format;
72+
$this->keyed = $keyed;
6773

6874
return $this;
6975
}
7076

7177
/**
72-
* @var array
78+
* @return string
7379
*/
74-
private $ranges = [];
80+
public function getFormat()
81+
{
82+
return $this->format;
83+
}
84+
85+
/**
86+
* @param string $format
87+
*/
88+
public function setFormat($format)
89+
{
90+
$this->format = $format;
91+
}
7592

7693
/**
7794
* Add range to aggregation.
7895
*
7996
* @param string|null $from
8097
* @param string|null $to
98+
* @param string|null $key
8199
*
82100
* @return $this
83101
*
@@ -115,6 +133,7 @@ public function getArray()
115133
'format' => $this->getFormat(),
116134
'field' => $this->getField(),
117135
'ranges' => $this->ranges,
136+
'keyed' => $this->keyed,
118137
];
119138

120139
return $data;

tests/Unit/Aggregation/Bucketing/DateRangeAggregationTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,13 @@ public function testDateRangeAggregationGetArray()
4747
$agg = new DateRangeAggregation('foo', 'baz');
4848
$agg->addRange(10, 20);
4949
$agg->setFormat('bar');
50+
$agg->setKeyed(true);
5051
$result = $agg->getArray();
5152
$expected = [
5253
'format' => 'bar',
5354
'field' => 'baz',
5455
'ranges' => [['from' => 10, 'to' => 20]],
56+
'keyed' => true,
5557
];
5658
$this->assertEquals($expected, $result);
5759
}

0 commit comments

Comments
 (0)