diff --git a/Suggester/Context.php b/Suggester/Context.php new file mode 100644 index 0000000000000000000000000000000000000000..06c16108deccc5855f3108ad6deb61edebe74f04 --- /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 0000000000000000000000000000000000000000..f9f0a7246e3f429d3f857ed2ef60231c5bf0b316 --- /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 0000000000000000000000000000000000000000..51429cd3545f3c9a2ba8220e26bc1c39da49eafe --- /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 0000000000000000000000000000000000000000..2d01eac1de42c3413a43472d0c3a79a07c407b33 --- /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; + } +}