From 552d8518cf60c834ed2003622782f192d3dec2cc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mantas=20Jonu=C5=A1as?= <mantas.jonusas@nfq.lt>
Date: Thu, 12 Mar 2015 11:03:06 +0200
Subject: [PATCH] Added geo distance aggregation

---
 Aggregation/GeoDistanceAggregation.php | 158 +++++++++++++++++++++++++
 1 file changed, 158 insertions(+)
 create mode 100644 Aggregation/GeoDistanceAggregation.php

diff --git a/Aggregation/GeoDistanceAggregation.php b/Aggregation/GeoDistanceAggregation.php
new file mode 100644
index 0000000..c1e4906
--- /dev/null
+++ b/Aggregation/GeoDistanceAggregation.php
@@ -0,0 +1,158 @@
+<?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 geo distance aggregation.
+ */
+class GeoDistanceAggregation extends AbstractAggregation
+{
+    use BucketingTrait;
+
+    /**
+     * @var mixed
+     */
+    private $origin;
+
+    /**
+     * @var string
+     */
+    private $distanceType;
+
+    /**
+     * @var string
+     */
+    private $unit;
+
+    /**
+     * @var array
+     */
+    private $ranges = [];
+
+    /**
+     * @return string
+     */
+    public function getOrigin()
+    {
+        return $this->origin;
+    }
+
+    /**
+     * @param mixed $origin
+     */
+    public function setOrigin($origin)
+    {
+        $this->origin = $origin;
+    }
+
+    /**
+     * @return string
+     */
+    public function getDistanceType()
+    {
+        return $this->distanceType;
+    }
+
+    /**
+     * @param string $distanceType
+     */
+    public function setDistanceType($distanceType)
+    {
+        $this->distanceType = $distanceType;
+    }
+
+    /**
+     * @return string
+     */
+    public function getUnit()
+    {
+        return $this->unit;
+    }
+
+    /**
+     * @param string $unit
+     */
+    public function setUnit($unit)
+    {
+        $this->unit = $unit;
+    }
+
+    /**
+     * Add range to aggregation.
+     *
+     * @param int|float|null $from
+     * @param int|float|null $to
+     *
+     * @throws \LogicException
+     *
+     * @return GeoDistanceAggregation
+     */
+    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()
+    {
+        $data = [];
+
+        if ($this->getField()) {
+            $data['field'] = $this->getField();
+        } else {
+            throw new \LogicException('Geo distance aggregation must have a field set.');
+        }
+
+        if ($this->getOrigin()) {
+            $data['origin'] = $this->getOrigin();
+        } else {
+            throw new \LogicException('Geo distance aggregation must have an origin set.');
+        }
+
+        if ($this->getUnit()) {
+            $data['unit'] = $this->getUnit();
+        }
+
+        if ($this->getDistanceType()) {
+            $data['distance_type'] = $this->getDistanceType();
+        }
+
+        $data['ranges'] = $this->ranges;
+
+        return $data;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getType()
+    {
+        return 'geo_distance';
+    }
+}
-- 
GitLab