1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing; |
13
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation; |
15
|
|
|
use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class representing date range aggregation. |
19
|
|
|
* |
20
|
|
|
* @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-daterange-aggregation.html |
21
|
|
|
*/ |
22
|
|
|
class DateRangeAggregation extends AbstractAggregation |
23
|
|
|
{ |
24
|
|
|
use BucketingTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $format; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function getFormat() |
35
|
|
|
{ |
36
|
|
|
return $this->format; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $name |
41
|
|
|
* @param string $field |
42
|
|
|
* @param string $format |
43
|
|
|
* @param array $ranges |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function __construct($name, $field = null, $format = null, array $ranges = []) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
parent::__construct($name); |
48
|
|
|
|
49
|
|
|
$this->setField($field); |
50
|
|
|
$this->setFormat($format); |
51
|
|
|
foreach ($ranges as $range) { |
52
|
|
|
$from = isset($range['from']) ? $range['from'] : null; |
53
|
|
|
$to = isset($range['to']) ? $range['to'] : null; |
54
|
|
|
$key = isset($range['key']) ? $range['key'] : null; |
55
|
|
|
$this->addRange($from, $to, $key); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $format |
61
|
|
|
*/ |
62
|
|
|
public function setFormat($format) |
63
|
|
|
{ |
64
|
|
|
$this->format = $format; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var array |
69
|
|
|
*/ |
70
|
|
|
private $ranges = []; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Add range to aggregation. |
74
|
|
|
* |
75
|
|
|
* @param string|null $from |
76
|
|
|
* @param string|null $to |
77
|
|
|
* |
78
|
|
|
* @return $this |
79
|
|
|
* |
80
|
|
|
* @throws \LogicException |
81
|
|
|
*/ |
82
|
|
|
public function addRange($from = null, $to = null, $key = null) |
83
|
|
|
{ |
84
|
|
|
$range = array_filter( |
85
|
|
|
[ |
86
|
|
|
'from' => $from, |
87
|
|
|
'to' => $to, |
88
|
|
|
'key' => $key, |
89
|
|
|
] |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
if (empty($range)) { |
93
|
|
|
throw new \LogicException('Either from or to must be set. Both cannot be null.'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
$this->ranges[] = $range; |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function getArray() |
105
|
|
|
{ |
106
|
|
|
if ($this->getField() && $this->getFormat() && !empty($this->ranges)) { |
107
|
|
|
$data = [ |
108
|
|
|
'format' => $this->getFormat(), |
109
|
|
|
'field' => $this->getField(), |
110
|
|
|
'ranges' => $this->ranges, |
111
|
|
|
]; |
112
|
|
|
|
113
|
|
|
return $data; |
114
|
|
|
} |
115
|
|
|
throw new \LogicException('Date range aggregation must have field, format set and range added.'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
|
|
public function getType() |
122
|
|
|
{ |
123
|
|
|
return 'date_range'; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.