Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <info@nfq.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\ElasticsearchBundle\DSL\Aggregation;
use ONGR\ElasticsearchBundle\DSL\Aggregation\Type\BucketingTrait;
/**
* Class representing date range aggregation.
*/
class DateRangeAggregation extends AbstractAggregation
{
use BucketingTrait;
/**
* @var string
*/
private $format;
/**
* @return string
*/
public function getFormat()
{
return $this->format;
}
/**
* @param string $format
*/
public function setFormat($format)
{
$this->format = $format;
}
/**
* @var array
*/
private $ranges = [];
/**
* Add range to aggregation.
*
* @param string|null $from
* @param string|null $to
*
* @return RangeAggregation
*
* @throws \LogicException
*/
public function addRange($from = null, $to = null)
{
$range = array_filter(
[
'from' => $from,
'to' => $to,
]
);
if (empty($range)) {
throw new \LogicException('Either from or to must be set. Both cannot be null.');
$this->ranges[] = $range;
return $this;
}
/**
* {@inheritdoc}
*/
public function getArray()
{
if ($this->getField() && $this->getFormat() && $this->ranges) {
$data = [
'format' => $this->getFormat(),
'field' => $this->getField(),
'ranges' => $this->ranges,
throw new \LogicException('Date range aggregation must have field, format set and range added.');