From c0b75cf1c79d554614db883a801c19f1b80dc310 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mantas=20Jonu=C5=A1as?= <mantas.jonusas@nfq.lt> Date: Wed, 18 Mar 2015 11:45:43 +0200 Subject: [PATCH] Implemented addQuery method in SpanOr and SpanNear queries --- Query/Span/SpanNearQuery.php | 30 +++++++++++------------------- Query/Span/SpanOrQuery.php | 33 +++++++++++++++++++++++++-------- 2 files changed, 36 insertions(+), 27 deletions(-) diff --git a/Query/Span/SpanNearQuery.php b/Query/Span/SpanNearQuery.php index 4804764..564a2ca 100644 --- a/Query/Span/SpanNearQuery.php +++ b/Query/Span/SpanNearQuery.php @@ -11,37 +11,30 @@ namespace ONGR\ElasticsearchBundle\DSL\Query\Span; -use ONGR\ElasticsearchBundle\DSL\ParametersTrait; - /** * Elasticsearch span near query. */ -class SpanNearQuery implements SpanQueryInterface +class SpanNearQuery extends SpanOrQuery implements SpanQueryInterface { - use ParametersTrait; - /** * @var int */ private $slop; /** - * @var SpanQueryInterface[] + * @return int */ - private $queries = []; + public function getSlop() + { + return $this->slop; + } /** - * @param int $slop - * @param SpanQueryInterface[] $queries - * @param array $parameters - * - * @throws \LogicException + * @param int $slop */ - public function __construct($slop, array $queries = [], array $parameters = []) + public function setSlop($slop) { $this->slop = $slop; - $this->queries = $queries; - $this->setParameters($parameters); } /** @@ -58,11 +51,10 @@ class SpanNearQuery implements SpanQueryInterface public function toArray() { $query = []; - foreach ($this->queries as $type) { - $data = [$type->getType() => $type->toArray()]; - $query['clauses'][] = $data; + foreach ($this->getQueries() as $type) { + $query['clauses'][] = [$type->getType() => $type->toArray()]; } - $query['slop'] = $this->slop; + $query['slop'] = $this->getSlop(); $output = $this->processArray($query); return $output; diff --git a/Query/Span/SpanOrQuery.php b/Query/Span/SpanOrQuery.php index a18df02..5d351c4 100644 --- a/Query/Span/SpanOrQuery.php +++ b/Query/Span/SpanOrQuery.php @@ -26,17 +26,35 @@ class SpanOrQuery implements SpanQueryInterface private $queries = []; /** - * @param SpanQueryInterface[] $queries - * @param array $parameters + * @param array $parameters */ - public function __construct(array $queries = [], array $parameters = []) + public function __construct(array $parameters = []) { - foreach ($queries as $query) { - $this->queries[] = $query; - } $this->setParameters($parameters); } + /** + * Add span query. + * + * @param SpanQueryInterface $query + * + * @return $this + */ + public function addQuery(SpanQueryInterface $query) + { + $this->queries[] = $query; + + return $this; + } + + /** + * @return SpanQueryInterface[] + */ + public function getQueries() + { + return $this->queries; + } + /** * {@inheritdoc} */ @@ -52,8 +70,7 @@ class SpanOrQuery implements SpanQueryInterface { $query = []; foreach ($this->queries as $type) { - $data = [$type->getType() => $type->toArray()]; - $query['clauses'][] = $data; + $query['clauses'][] = [$type->getType() => $type->toArray()]; } $output = $this->processArray($query); -- GitLab