From 1437a624e1a8afeaa746ef1f7aadd1b2344fe6f1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Zigmas=20Satkevi=C4=8Dius?= <zsatkevicius@nfq.lt>
Date: Fri, 28 Nov 2014 17:34:28 +0200
Subject: [PATCH] Added context suggester DSL.

---
 Suggester/Context.php                 | 107 ++++++++++++++++++++++++++
 Suggester/Context/AbstractContext.php |  87 +++++++++++++++++++++
 Suggester/Context/CategoryContext.php |  26 +++++++
 Suggester/Context/GeoContext.php      |  53 +++++++++++++
 4 files changed, 273 insertions(+)
 create mode 100644 Suggester/Context.php
 create mode 100644 Suggester/Context/AbstractContext.php
 create mode 100644 Suggester/Context/CategoryContext.php
 create mode 100644 Suggester/Context/GeoContext.php

diff --git a/Suggester/Context.php b/Suggester/Context.php
new file mode 100644
index 0000000..06c1610
--- /dev/null
+++ b/Suggester/Context.php
@@ -0,0 +1,107 @@
+<?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\Suggester;
+
+use ONGR\ElasticsearchBundle\DSL\Suggester\Context\AbstractContext;
+
+/**
+ * Context suggester.
+ */
+class Context extends AbstractSuggester
+{
+    /**
+     * @var AbstractContext[]
+     */
+    private $context;
+
+    /**
+     * Size of completion.
+     *
+     * @var int
+     */
+    private $size;
+
+    /**
+     * Returns context.
+     *
+     * @return AbstractContext[]
+     */
+    public function getContext()
+    {
+        return $this->context;
+    }
+
+    /**
+     * Sets context array.
+     *
+     * @param AbstractContext[] $context
+     */
+    public function setContext($context)
+    {
+        $this->context = $context;
+    }
+
+    /**
+     * Sets context.
+     *
+     * @param AbstractContext $context
+     */
+    public function addContext($context)
+    {
+        $this->context[] = $context;
+    }
+
+    /**
+     * @return int
+     */
+    public function getSize()
+    {
+        return $this->size;
+    }
+
+    /**
+     * @param int $size
+     */
+    public function setSize($size)
+    {
+        $this->size = $size;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function toArray()
+    {
+        $completion = ['field' => $this->getField()];
+        foreach ($this->context as $context) {
+            $completion['context'][$context->getName()] = $context->toArray();
+        }
+        if ($this->getSize() !== null) {
+            $completion['size'] = $this->getSize();
+        }
+
+        return [
+            $this->getName() => [
+                'text' => $this->getText(),
+                'completion' => $completion,
+            ]
+        ];
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function getType()
+    {
+        return 'completion';
+    }
+}
diff --git a/Suggester/Context/AbstractContext.php b/Suggester/Context/AbstractContext.php
new file mode 100644
index 0000000..f9f0a72
--- /dev/null
+++ b/Suggester/Context/AbstractContext.php
@@ -0,0 +1,87 @@
+<?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\Suggester\Context;
+
+/**
+ * Abstract context to be used by geo context and category context.
+ */
+abstract class AbstractContext
+{
+    /**
+     * Name of the context used.
+     *
+     * @var string
+     */
+    private $name;
+
+    /**
+     * Value of the context.
+     *
+     * @var string|array
+     */
+    private $value;
+
+    /**
+     * Constructor.
+     *
+     * @param string       $name  Context name.
+     * @param array|string $value Context value.
+     */
+    public function __construct($name, $value)
+    {
+        $this->name = $name;
+        $this->value = $value;
+    }
+
+    /**
+     * Converts context to an array.
+     *
+     * @return array
+     */
+    abstract public function toArray();
+
+    /**
+     * Returns name of the context.
+     *
+     * @return string
+     */
+    public function getName()
+    {
+        return $this->name;
+    }
+
+    /**
+     * Sets type of the context.
+     *
+     * @param string $name
+     */
+    public function setName($name)
+    {
+        $this->name = $name;
+    }
+
+    /**
+     * @return array|string
+     */
+    public function getValue()
+    {
+        return $this->value;
+    }
+
+    /**
+     * @param array|string $value
+     */
+    public function setValue($value)
+    {
+        $this->value = $value;
+    }
+}
diff --git a/Suggester/Context/CategoryContext.php b/Suggester/Context/CategoryContext.php
new file mode 100644
index 0000000..51429cd
--- /dev/null
+++ b/Suggester/Context/CategoryContext.php
@@ -0,0 +1,26 @@
+<?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\Suggester\Context;
+
+/**
+ * Category context to be used by context suggester.
+ */
+class CategoryContext extends AbstractContext
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function toArray()
+    {
+        return $this->getValue();
+    }
+}
diff --git a/Suggester/Context/GeoContext.php b/Suggester/Context/GeoContext.php
new file mode 100644
index 0000000..2d01eac
--- /dev/null
+++ b/Suggester/Context/GeoContext.php
@@ -0,0 +1,53 @@
+<?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\Suggester\Context;
+
+/**
+ * Geo context to be used by context suggester.
+ */
+class GeoContext extends AbstractContext
+{
+    /**
+     * @var string
+     */
+    private $precision;
+
+    /**
+     * @return mixed
+     */
+    public function getPrecision()
+    {
+        return $this->precision;
+    }
+
+    /**
+     * @param mixed $precision
+     */
+    public function setPrecision($precision)
+    {
+        $this->precision = $precision;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function toArray()
+    {
+        $out = ['value' => $this->getValue()];
+
+        if ($this->getPrecision() !== null) {
+            $out['precision'] = $this->getPrecision();
+        }
+
+        return $out;
+    }
+}
-- 
GitLab