diff --git a/Aggregation/DateRangeAggregation.php b/Aggregation/DateRangeAggregation.php new file mode 100644 index 0000000000000000000000000000000000000000..034b1fd83af5bfb17fa97acf24ec6d653b3508e2 --- /dev/null +++ b/Aggregation/DateRangeAggregation.php @@ -0,0 +1,101 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\DSL\Aggregation; + +use ONGR\ElasticsearchBundle\DSL\Aggregation\Type\BucketingTrait; + +/** + * Class representing date range aggregation. + */ +class DateRangeAggregation extends AbstractAggregation +{ + use BucketingTrait; + + /** + * @var string + */ + private $format; + + /** + * @return string + */ + public function getFormat() + { + return $this->format; + } + + /** + * @param string $format + */ + public function setFormat($format) + { + $this->format = $format; + } + + /** + * @var array + */ + private $ranges = []; + + /** + * Add range to aggregation. + * + * @param string|null $from + * @param string|null $to + * + * @return RangeAggregation + * + * @throws \LogicException + */ + public function addRange($from = null, $to = null) + { + $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; + } + + /** + * {@inheritdoc} + */ + public function getArray() + { + if ($this->getField() && $this->getFormat() && $this->ranges) { + $data = [ + 'format' => $this->getFormat(), + 'field' => $this->getField(), + 'ranges' => $this->ranges, + ]; + + return $data; + } + throw new \LogicException('Date range aggregation must have field, format set and range added.'); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'date_range'; + } +} diff --git a/Aggregation/MissingAggregation.php b/Aggregation/MissingAggregation.php new file mode 100644 index 0000000000000000000000000000000000000000..8431c5e2cdceef21ad345ef2ee075d953319a1ed --- /dev/null +++ b/Aggregation/MissingAggregation.php @@ -0,0 +1,41 @@ +<?php + +/* + * This file is part of the ONGR package. + * + * (c) NFQ Technologies UAB <info@nfq.com> + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\DSL\Aggregation; + +use ONGR\ElasticsearchBundle\DSL\Aggregation\Type\BucketingTrait; + +/** + * Class representing missing aggregation. + */ +class MissingAggregation extends AbstractAggregation +{ + use BucketingTrait; + + /** + * {@inheritdoc} + */ + public function getArray() + { + if ($this->getField()) { + return ['field' => $this->getField()]; + } + throw new \LogicException('Missing aggregation must have a field set.'); + } + + /** + * {@inheritdoc} + */ + public function getType() + { + return 'missing'; + } +}