From f86ec104b14cb626640d7f78685cdeb208af1ac9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mantas=20Jonu=C5=A1as?= <mantas.jonusas@nfq.lt> Date: Sat, 28 Mar 2015 08:32:43 +0200 Subject: [PATCH] Refactored percentiles aggregation --- Aggregation/PercentilesAggregation.php | 40 +++++++++++++++----------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/Aggregation/PercentilesAggregation.php b/Aggregation/PercentilesAggregation.php index e73496a..8aa9e1e 100644 --- a/Aggregation/PercentilesAggregation.php +++ b/Aggregation/PercentilesAggregation.php @@ -77,24 +77,32 @@ class PercentilesAggregation extends AbstractAggregation */ public function getArray() { - $out = []; + $out = array_filter( + [ + 'compression' => $this->getCompression(), + 'percents' => $this->getPercents(), + 'field' => $this->getField(), + 'script' => $this->getScript(), + ], + function ($val) { + return ($val || is_numeric($val)); + } + ); + + $this->isRequiredParametersSet($out); - if ($this->getField()) { - $out['field'] = $this->getField(); - } elseif ($this->getScript()) { - $out['script'] = $this->getScript(); - } else { - throw new \LogicException('Percentiles aggregation must have field or script set.'); - } - - if ($this->getCompression()) { - $out['compression'] = $this->getCompression(); - } + return $out; + } - if ($this->getPercents()) { - $out['percents'] = $this->getPercents(); + /** + * @param array $a + * + * @throws \LogicException + */ + private function isRequiredParametersSet($a) + { + if (!array_key_exists('field', $a) && !array_key_exists('script', $a)) { + throw new \LogicException('Percentiles aggregation must have field or script set.'); } - - return $out; } } -- GitLab