From 1824e778be1c31a35c8489d13344dcf4224668b3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mantas=20Jonu=C5=A1as?= <mantas.jonusas@nfq.lt>
Date: Tue, 3 Mar 2015 17:28:14 +0200
Subject: [PATCH] Added missing aggregations

---
 Aggregation/DateRangeAggregation.php | 98 ++++++++++++++++++++++++++++
 Aggregation/MissingAggregation.php   | 42 ++++++++++++
 2 files changed, 140 insertions(+)
 create mode 100644 Aggregation/DateRangeAggregation.php
 create mode 100644 Aggregation/MissingAggregation.php

diff --git a/Aggregation/DateRangeAggregation.php b/Aggregation/DateRangeAggregation.php
new file mode 100644
index 0000000..6af52d0
--- /dev/null
+++ b/Aggregation/DateRangeAggregation.php
@@ -0,0 +1,98 @@
+<?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)
+    {
+        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]];
+        }
+
+        return $this;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getArray()
+    {
+        if ($this->getField() && $this->getFormat() && $this->ranges) {
+            $data = [
+                'format' => $this->getFormat(),
+                'field' => $this->getField(),
+                'ranges' => array_values($this->ranges),
+            ];
+
+            return $data;
+        }
+        throw new \LogicException('Date range aggregation must have field and format set.');
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getType()
+    {
+        return 'date_range';
+    }
+}
diff --git a/Aggregation/MissingAggregation.php b/Aggregation/MissingAggregation.php
new file mode 100644
index 0000000..bdd9ed8
--- /dev/null
+++ b/Aggregation/MissingAggregation.php
@@ -0,0 +1,42 @@
+<?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;
+use Symfony\Component\Process\Exception\LogicException;
+
+/**
+ * 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';
+    }
+}
-- 
GitLab