Skip to content
Snippets Groups Projects
Commit e092e9ac authored by Mantas Jonušas's avatar Mantas Jonušas
Browse files

Refactored percentile ranks aggregation

parent f86ec104
No related branches found
No related tags found
No related merge requests found
...@@ -77,24 +77,37 @@ class PercentileRanksAggregation extends AbstractAggregation ...@@ -77,24 +77,37 @@ class PercentileRanksAggregation extends AbstractAggregation
*/ */
public function getArray() public function getArray()
{ {
$out = []; $out = array_filter(
[
'field' => $this->getField(),
'script' => $this->getScript(),
'values' => $this->getValues(),
'compression' => $this->getCompression(),
],
function ($val) {
return ($val || is_numeric($val));
}
);
if ($this->getField() && $this->getValues()) { $this->isRequiredParametersSet($out);
$out['field'] = $this->getField();
$out['values'] = $this->getValues();
} elseif ($this->getScript() && $this->getValues()) {
$out['script'] = $this->getScript();
$out['values'] = $this->getValues();
} else {
throw new \LogicException(
'Percentile ranks aggregation must have field and values or script and values set.'
);
}
if ($this->getCompression()) {
$out['compression'] = $this->getCompression();
}
return $out; return $out;
} }
/**
* @param array $a
*
* @return bool
*
* @throws \LogicException
*/
private function isRequiredParametersSet($a)
{
if (array_key_exists('field', $a) && array_key_exists('values', $a)
|| (array_key_exists('script', $a) && array_key_exists('values', $a))
) {
return true;
}
throw new \LogicException('Percentile ranks aggregation must have field and values or script and values set.');
}
} }
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