From 6d9a550750c1335ec584e096adea946e57063243 Mon Sep 17 00:00:00 2001
From: Mantas <marc.mantas@gmail.com>
Date: Mon, 27 Jun 2016 15:33:33 +0300
Subject: [PATCH] added date histogram aggregation

---
 src/Aggregation/DateHistogramAggregation.php | 89 ++++++++++++++++++++
 1 file changed, 89 insertions(+)
 create mode 100644 src/Aggregation/DateHistogramAggregation.php

diff --git a/src/Aggregation/DateHistogramAggregation.php b/src/Aggregation/DateHistogramAggregation.php
new file mode 100644
index 0000000..625f3b2
--- /dev/null
+++ b/src/Aggregation/DateHistogramAggregation.php
@@ -0,0 +1,89 @@
+<?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\ElasticsearchDSL\Aggregation;
+
+use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait;
+
+/**
+ * Class representing Histogram aggregation.
+ *
+ * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html
+ */
+class DateHistogramAggregation extends AbstractAggregation
+{
+    use BucketingTrait;
+
+    /**
+     * @var string
+     */
+    protected $interval;
+
+    /**
+     * Inner aggregations container init.
+     *
+     * @param string $name
+     * @param string $field
+     * @param string $interval
+     */
+    public function __construct(
+        $name,
+        $field = null,
+        $interval = null
+    ) {
+        parent::__construct($name);
+
+        $this->setField($field);
+        $this->setInterval($interval);
+    }
+
+    /**
+     * @return int
+     */
+    public function getInterval()
+    {
+        return $this->interval;
+    }
+
+    /**
+     * @param string $interval
+     */
+    public function setInterval($interval)
+    {
+        $this->interval = $interval;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getType()
+    {
+        return 'date_histogram';
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getArray()
+    {
+        if (!$this->getField() || !$this->getInterval()) {
+            throw new \LogicException('Date histogram aggregation must have field and interval set.');
+        }
+
+        $out = [
+            'field' => $this->getField(),
+            'interval' => $this->getInterval(),
+        ];
+        $out = $this->processArray($out);
+
+        return $out;
+    }
+}
-- 
GitLab