From e092e9ac34958f1347a24d2b733d7412c1bf280f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mantas=20Jonu=C5=A1as?= <mantas.jonusas@nfq.lt> Date: Sat, 28 Mar 2015 11:12:09 +0200 Subject: [PATCH] Refactored percentile ranks aggregation --- Aggregation/PercentileRanksAggregation.php | 45 ++++++++++++++-------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/Aggregation/PercentileRanksAggregation.php b/Aggregation/PercentileRanksAggregation.php index a69e137..5bc5c12 100644 --- a/Aggregation/PercentileRanksAggregation.php +++ b/Aggregation/PercentileRanksAggregation.php @@ -77,24 +77,37 @@ class PercentileRanksAggregation extends AbstractAggregation */ 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()) { - $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(); - } + $this->isRequiredParametersSet($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.'); + } } -- GitLab