Skip to content
Snippets Groups Projects
Commit 3f255c46 authored by Martynas Sudintas's avatar Martynas Sudintas
Browse files

Improved range aggregation

parent f3290b83
No related branches found
No related tags found
No related merge requests found
...@@ -23,64 +23,66 @@ class RangeAggregation extends AbstractAggregation ...@@ -23,64 +23,66 @@ class RangeAggregation extends AbstractAggregation
/** /**
* @var array * @var array
*/ */
protected $ranges = []; private $ranges = [];
/** /**
* @var bool * @var bool
*/ */
protected $keyed = false; private $keyed = false;
/** /**
* Sets if result buckets should be keyed. * Sets if result buckets should be keyed.
* *
* @param bool $keyed * @param bool $keyed
*
* @return RangeAggregation
*/ */
public function setKeyed($keyed) public function setKeyed($keyed)
{ {
$this->keyed = $keyed; $this->keyed = $keyed;
return $this;
} }
/** /**
* Add range to aggregation. * Add range to aggregation.
* *
* @param mixed $from * @param int|float|null $from
* @param mixed $to * @param int|float|null $to
* @param string $key * @param string $key
*
* @return RangeAggregation
*/ */
public function addRange($from = null, $to = null, $key = '') public function addRange($from = null, $to = null, $key = '')
{ {
$range = []; $range = array_filter(
[
if (!empty($from)) { 'from' => $from,
$range['from'] = $from; 'to' => $to,
} ]
);
if (!empty($to)) {
$range['to'] = $to;
}
if ($this->keyed && !empty($key)) { if ($this->keyed && !empty($key)) {
$range['key'] = $key; $range['key'] = $key;
} }
$this->ranges[] = $range; $this->ranges[] = $range;
return $this;
} }
/** /**
* Remove range from aggregation. Returns true on success. * Remove range from aggregation. Returns true on success.
* *
* @param mixed $from * @param int|float|null $from
* @param mixed $to * @param int|float|null $to
* @param string $searchKey
* *
* @return bool * @return bool
*/ */
public function removeRange($from, $to, $searchKey = '') public function removeRange($from, $to)
{ {
foreach ($this->ranges as $key => $range) { foreach ($this->ranges as $key => $range) {
if (($range['from'] == $from && $range['to'] == $to) if (array_diff_assoc(array_filter(['from' => $from, 'to' => $to]), $range) === []) {
|| (!empty($searchKey) && $range['key'] == $searchKey)
) {
unset($this->ranges[$key]); unset($this->ranges[$key]);
return true; return true;
...@@ -90,6 +92,28 @@ class RangeAggregation extends AbstractAggregation ...@@ -90,6 +92,28 @@ class RangeAggregation extends AbstractAggregation
return false; return false;
} }
/**
* Removes range by key.
*
* @param string $key Range key.
*
* @return bool
*/
public function removeRangeByKey($key)
{
if ($this->keyed) {
foreach ($this->ranges as $rangeKey => $range) {
if (array_key_exists('key', $range) && $range['key'] === $key) {
unset($this->ranges[$rangeKey]);
return true;
}
}
}
return false;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -97,7 +121,7 @@ class RangeAggregation extends AbstractAggregation ...@@ -97,7 +121,7 @@ class RangeAggregation extends AbstractAggregation
{ {
$data = [ $data = [
'keyed' => $this->keyed, 'keyed' => $this->keyed,
'ranges' => $this->ranges, 'ranges' => array_values($this->ranges),
]; ];
if ($this->getField()) { if ($this->getField()) {
......
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