diff --git a/Aggregation/DateRangeAggregation.php b/Aggregation/DateRangeAggregation.php index 6af52d0919a76887da50e7188d613a667e210f54..034b1fd83af5bfb17fa97acf24ec6d653b3508e2 100644 --- a/Aggregation/DateRangeAggregation.php +++ b/Aggregation/DateRangeAggregation.php @@ -58,16 +58,19 @@ class DateRangeAggregation extends AbstractAggregation */ public function addRange($from = null, $to = null) { - if ($from === null && $to === null) { - throw new \LogicException('Missing range'); - } elseif ($from === null) { - $this->ranges = [['to' => $to]]; - } elseif ($to === null) { - $this->ranges = [['from' => $from]]; - } else { - $this->ranges = [['from' => $from], ['to' => $to]]; + $range = array_filter( + [ + 'from' => $from, + 'to' => $to, + ] + ); + + if (empty($range)) { + throw new \LogicException('Either from or to must be set. Both cannot be null.'); } + $this->ranges[] = $range; + return $this; } @@ -80,12 +83,12 @@ class DateRangeAggregation extends AbstractAggregation $data = [ 'format' => $this->getFormat(), 'field' => $this->getField(), - 'ranges' => array_values($this->ranges), + 'ranges' => $this->ranges, ]; return $data; } - throw new \LogicException('Date range aggregation must have field and format set.'); + throw new \LogicException('Date range aggregation must have field, format set and range added.'); } /** diff --git a/Aggregation/MissingAggregation.php b/Aggregation/MissingAggregation.php index bdd9ed853eee6cd024d464cbc55b92576ff24772..8431c5e2cdceef21ad345ef2ee075d953319a1ed 100644 --- a/Aggregation/MissingAggregation.php +++ b/Aggregation/MissingAggregation.php @@ -12,7 +12,6 @@ namespace ONGR\ElasticsearchBundle\DSL\Aggregation; use ONGR\ElasticsearchBundle\DSL\Aggregation\Type\BucketingTrait; -use Symfony\Component\Process\Exception\LogicException; /** * Class representing missing aggregation. @@ -29,7 +28,7 @@ class MissingAggregation extends AbstractAggregation if ($this->getField()) { return ['field' => $this->getField()]; } - throw new LogicException('Missing aggregation must have a field set'); + throw new \LogicException('Missing aggregation must have a field set.'); } /**