diff --git a/DslTypeAwareTrait.php b/DslTypeAwareTrait.php new file mode 100644 index 0000000000000000000000000000000000000000..3e9412bbde203236015e4341e492abf4fef09154 --- /dev/null +++ b/DslTypeAwareTrait.php @@ -0,0 +1,50 @@ +<?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; + +use Elasticsearch\Common\Exceptions\InvalidArgumentException; + +/** + * A trait which handles dsl type. + */ +trait DslTypeAwareTrait +{ + /** + * @var string + */ + private $dslType; + + /** + * Returns a dsl type. + * + * @return string + */ + public function getDslType() + { + return $this->dslType; + } + + /** + * Sets a dsl type. + * + * @param string $dslType + * + * @throws InvalidArgumentException + */ + public function setDslType($dslType) + { + if ($dslType !== 'filter' && $dslType !== 'query') { + throw new InvalidArgumentException('Not supported dsl type'); + } + $this->dslType = $dslType; + } +} diff --git a/Filter/HasChildFilter.php b/Filter/HasChildFilter.php index 5310923a5195f3de4fbebd1f98cb231902b84899..020da73fd28a51ea4f58f48319059557536cb7ec 100644 --- a/Filter/HasChildFilter.php +++ b/Filter/HasChildFilter.php @@ -12,6 +12,7 @@ namespace ONGR\ElasticsearchBundle\DSL\Filter; use ONGR\ElasticsearchBundle\DSL\BuilderInterface; +use ONGR\ElasticsearchBundle\DSL\DslTypeAwareTrait; use ONGR\ElasticsearchBundle\DSL\ParametersTrait; /** @@ -20,9 +21,7 @@ use ONGR\ElasticsearchBundle\DSL\ParametersTrait; class HasChildFilter implements BuilderInterface { use ParametersTrait; - - const USE_QUERY = 'query'; - const USE_FILTER = 'filter'; + use DslTypeAwareTrait; /** * @var string @@ -38,16 +37,15 @@ class HasChildFilter implements BuilderInterface * @param string $type * @param BuilderInterface $query * @param array $parameters - * @param string $dslType * * @throws \InvalidArgumentException */ - public function __construct($type, BuilderInterface $query, array $parameters = [], $dslType = self::USE_FILTER) + public function __construct($type, BuilderInterface $query, array $parameters = []) { $this->type = $type; - $this->dslType = $dslType; $this->query = $query; $this->setParameters($parameters); + $this->setDslType('filter'); } /** @@ -65,7 +63,7 @@ class HasChildFilter implements BuilderInterface { $query = [ 'type' => $this->type, - $this->dslType => [$this->query->getType() => $this->query->toArray()], + $this->getDslType() => [$this->query->getType() => $this->query->toArray()], ]; $output = $this->processArray($query); diff --git a/Filter/HasParentFilter.php b/Filter/HasParentFilter.php index d151c41eeed8d6cd4facc4d5fce762ec9ad68917..a4b601e5952be91a35aac1cd74263fdc0b60071f 100644 --- a/Filter/HasParentFilter.php +++ b/Filter/HasParentFilter.php @@ -12,6 +12,7 @@ namespace ONGR\ElasticsearchBundle\DSL\Filter; use ONGR\ElasticsearchBundle\DSL\BuilderInterface; +use ONGR\ElasticsearchBundle\DSL\DslTypeAwareTrait; use ONGR\ElasticsearchBundle\DSL\ParametersTrait; /** @@ -20,9 +21,7 @@ use ONGR\ElasticsearchBundle\DSL\ParametersTrait; class HasParentFilter implements BuilderInterface { use ParametersTrait; - - const USE_QUERY = 'query'; - const USE_FILTER = 'filter'; + use DslTypeAwareTrait; /** * @var string @@ -38,20 +37,15 @@ class HasParentFilter implements BuilderInterface * @param string $parentType * @param BuilderInterface $query * @param array $parameters - * @param string $dslType * * @throws \InvalidArgumentException */ - public function __construct( - $parentType, - BuilderInterface $query, - array $parameters = [], - $dslType = self::USE_FILTER - ) { + public function __construct($parentType, BuilderInterface $query, array $parameters = []) + { $this->parentType = $parentType; - $this->dslType = $dslType; $this->query = $query; $this->setParameters($parameters); + $this->setDslType('filter'); } /** @@ -69,7 +63,7 @@ class HasParentFilter implements BuilderInterface { $query = [ 'parent_type' => $this->parentType, - $this->dslType => [$this->query->getType() => $this->query->toArray()], + $this->getDslType() => [$this->query->getType() => $this->query->toArray()], ]; $output = $this->processArray($query); diff --git a/Query/ConstantScoreQuery.php b/Query/ConstantScoreQuery.php index 711dff4a9660a6e08d60e2a65ea3dcf0c3605584..a133a52ea738655734e56bcfc28bea440fc0ab6b 100644 --- a/Query/ConstantScoreQuery.php +++ b/Query/ConstantScoreQuery.php @@ -12,6 +12,7 @@ namespace ONGR\ElasticsearchBundle\DSL\Query; use ONGR\ElasticsearchBundle\DSL\BuilderInterface; +use ONGR\ElasticsearchBundle\DSL\DslTypeAwareTrait; use ONGR\ElasticsearchBundle\DSL\ParametersTrait; /** @@ -20,14 +21,7 @@ use ONGR\ElasticsearchBundle\DSL\ParametersTrait; class ConstantScoreQuery implements BuilderInterface { use ParametersTrait; - - const USE_QUERY = 'query'; - const USE_FILTER = 'filter'; - - /** - * @var string - */ - private $dslType; + use DslTypeAwareTrait; /** * @var BuilderInterface @@ -37,13 +31,12 @@ class ConstantScoreQuery implements BuilderInterface /** * @param BuilderInterface $query * @param array $parameters - * @param string $dslType */ - public function __construct(BuilderInterface $query, array $parameters = [], $dslType = self::USE_QUERY) + public function __construct(BuilderInterface $query, array $parameters = []) { - $this->dslType = $dslType; $this->query = $query; $this->setParameters($parameters); + $this->setDslType('query'); } /** @@ -60,7 +53,7 @@ class ConstantScoreQuery implements BuilderInterface public function toArray() { $query = [ - strtolower($this->dslType) => [ + strtolower($this->getDslType()) => [ $this->query->getType() => $this->query->toArray(), ], ]; diff --git a/Query/FunctionScoreQuery.php b/Query/FunctionScoreQuery.php index 1e9f6157d23efc8e26ce5d177446de9568fa3204..ac22395c9774bc7e9226b9bfb3720cf37e60172f 100644 --- a/Query/FunctionScoreQuery.php +++ b/Query/FunctionScoreQuery.php @@ -12,6 +12,7 @@ namespace ONGR\ElasticsearchBundle\DSL\Query; use ONGR\ElasticsearchBundle\DSL\BuilderInterface; +use ONGR\ElasticsearchBundle\DSL\DslTypeAwareTrait; use ONGR\ElasticsearchBundle\DSL\ParametersTrait; /** @@ -20,14 +21,7 @@ use ONGR\ElasticsearchBundle\DSL\ParametersTrait; class FunctionScoreQuery implements BuilderInterface { use ParametersTrait; - - const USE_QUERY = 'query'; - const USE_FILTER = 'filter'; - - /** - * @var string - */ - private $dslType; + use DslTypeAwareTrait; /** * @var BuilderInterface @@ -43,18 +37,13 @@ class FunctionScoreQuery implements BuilderInterface * @param BuilderInterface $query * @param array $functions * @param array $parameters - * @param string $dslType */ - public function __construct( - BuilderInterface $query, - array $functions, - array $parameters = [], - $dslType = self::USE_QUERY - ) { - $this->dslType = $dslType; + public function __construct(BuilderInterface $query, array $functions, array $parameters = []) + { $this->query = $query; $this->functions = $functions; $this->setParameters($parameters); + $this->setDslType('query'); } /** @@ -71,7 +60,7 @@ class FunctionScoreQuery implements BuilderInterface public function toArray() { $query = [ - strtolower($this->dslType) => [ + strtolower($this->getDslType()) => [ $this->query->getType() => $this->query->toArray(), ], 'functions' => [$this->functions],