Skip to content
Snippets Groups Projects
Commit c40aa1a5 authored by Mantas Jonušas's avatar Mantas Jonušas
Browse files

Removed constants

parent 39afdd38
Branches
No related tags found
No related merge requests found
<?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;
}
}
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
namespace ONGR\ElasticsearchBundle\DSL\Filter; namespace ONGR\ElasticsearchBundle\DSL\Filter;
use ONGR\ElasticsearchBundle\DSL\BuilderInterface; use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
use ONGR\ElasticsearchBundle\DSL\DslTypeAwareTrait;
use ONGR\ElasticsearchBundle\DSL\ParametersTrait; use ONGR\ElasticsearchBundle\DSL\ParametersTrait;
/** /**
...@@ -20,9 +21,7 @@ use ONGR\ElasticsearchBundle\DSL\ParametersTrait; ...@@ -20,9 +21,7 @@ use ONGR\ElasticsearchBundle\DSL\ParametersTrait;
class HasChildFilter implements BuilderInterface class HasChildFilter implements BuilderInterface
{ {
use ParametersTrait; use ParametersTrait;
use DslTypeAwareTrait;
const USE_QUERY = 'query';
const USE_FILTER = 'filter';
/** /**
* @var string * @var string
...@@ -38,16 +37,15 @@ class HasChildFilter implements BuilderInterface ...@@ -38,16 +37,15 @@ class HasChildFilter implements BuilderInterface
* @param string $type * @param string $type
* @param BuilderInterface $query * @param BuilderInterface $query
* @param array $parameters * @param array $parameters
* @param string $dslType
* *
* @throws \InvalidArgumentException * @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->type = $type;
$this->dslType = $dslType;
$this->query = $query; $this->query = $query;
$this->setParameters($parameters); $this->setParameters($parameters);
$this->setDslType('filter');
} }
/** /**
...@@ -65,7 +63,7 @@ class HasChildFilter implements BuilderInterface ...@@ -65,7 +63,7 @@ class HasChildFilter implements BuilderInterface
{ {
$query = [ $query = [
'type' => $this->type, 'type' => $this->type,
$this->dslType => [$this->query->getType() => $this->query->toArray()], $this->getDslType() => [$this->query->getType() => $this->query->toArray()],
]; ];
$output = $this->processArray($query); $output = $this->processArray($query);
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
namespace ONGR\ElasticsearchBundle\DSL\Filter; namespace ONGR\ElasticsearchBundle\DSL\Filter;
use ONGR\ElasticsearchBundle\DSL\BuilderInterface; use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
use ONGR\ElasticsearchBundle\DSL\DslTypeAwareTrait;
use ONGR\ElasticsearchBundle\DSL\ParametersTrait; use ONGR\ElasticsearchBundle\DSL\ParametersTrait;
/** /**
...@@ -20,9 +21,7 @@ use ONGR\ElasticsearchBundle\DSL\ParametersTrait; ...@@ -20,9 +21,7 @@ use ONGR\ElasticsearchBundle\DSL\ParametersTrait;
class HasParentFilter implements BuilderInterface class HasParentFilter implements BuilderInterface
{ {
use ParametersTrait; use ParametersTrait;
use DslTypeAwareTrait;
const USE_QUERY = 'query';
const USE_FILTER = 'filter';
/** /**
* @var string * @var string
...@@ -38,20 +37,15 @@ class HasParentFilter implements BuilderInterface ...@@ -38,20 +37,15 @@ class HasParentFilter implements BuilderInterface
* @param string $parentType * @param string $parentType
* @param BuilderInterface $query * @param BuilderInterface $query
* @param array $parameters * @param array $parameters
* @param string $dslType
* *
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function __construct( public function __construct($parentType, BuilderInterface $query, array $parameters = [])
$parentType, {
BuilderInterface $query,
array $parameters = [],
$dslType = self::USE_FILTER
) {
$this->parentType = $parentType; $this->parentType = $parentType;
$this->dslType = $dslType;
$this->query = $query; $this->query = $query;
$this->setParameters($parameters); $this->setParameters($parameters);
$this->setDslType('filter');
} }
/** /**
...@@ -69,7 +63,7 @@ class HasParentFilter implements BuilderInterface ...@@ -69,7 +63,7 @@ class HasParentFilter implements BuilderInterface
{ {
$query = [ $query = [
'parent_type' => $this->parentType, 'parent_type' => $this->parentType,
$this->dslType => [$this->query->getType() => $this->query->toArray()], $this->getDslType() => [$this->query->getType() => $this->query->toArray()],
]; ];
$output = $this->processArray($query); $output = $this->processArray($query);
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
namespace ONGR\ElasticsearchBundle\DSL\Query; namespace ONGR\ElasticsearchBundle\DSL\Query;
use ONGR\ElasticsearchBundle\DSL\BuilderInterface; use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
use ONGR\ElasticsearchBundle\DSL\DslTypeAwareTrait;
use ONGR\ElasticsearchBundle\DSL\ParametersTrait; use ONGR\ElasticsearchBundle\DSL\ParametersTrait;
/** /**
...@@ -20,14 +21,7 @@ use ONGR\ElasticsearchBundle\DSL\ParametersTrait; ...@@ -20,14 +21,7 @@ use ONGR\ElasticsearchBundle\DSL\ParametersTrait;
class ConstantScoreQuery implements BuilderInterface class ConstantScoreQuery implements BuilderInterface
{ {
use ParametersTrait; use ParametersTrait;
use DslTypeAwareTrait;
const USE_QUERY = 'query';
const USE_FILTER = 'filter';
/**
* @var string
*/
private $dslType;
/** /**
* @var BuilderInterface * @var BuilderInterface
...@@ -37,13 +31,12 @@ class ConstantScoreQuery implements BuilderInterface ...@@ -37,13 +31,12 @@ class ConstantScoreQuery implements BuilderInterface
/** /**
* @param BuilderInterface $query * @param BuilderInterface $query
* @param array $parameters * @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->query = $query;
$this->setParameters($parameters); $this->setParameters($parameters);
$this->setDslType('query');
} }
/** /**
...@@ -60,7 +53,7 @@ class ConstantScoreQuery implements BuilderInterface ...@@ -60,7 +53,7 @@ class ConstantScoreQuery implements BuilderInterface
public function toArray() public function toArray()
{ {
$query = [ $query = [
strtolower($this->dslType) => [ strtolower($this->getDslType()) => [
$this->query->getType() => $this->query->toArray(), $this->query->getType() => $this->query->toArray(),
], ],
]; ];
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
namespace ONGR\ElasticsearchBundle\DSL\Query; namespace ONGR\ElasticsearchBundle\DSL\Query;
use ONGR\ElasticsearchBundle\DSL\BuilderInterface; use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
use ONGR\ElasticsearchBundle\DSL\DslTypeAwareTrait;
use ONGR\ElasticsearchBundle\DSL\ParametersTrait; use ONGR\ElasticsearchBundle\DSL\ParametersTrait;
/** /**
...@@ -20,14 +21,7 @@ use ONGR\ElasticsearchBundle\DSL\ParametersTrait; ...@@ -20,14 +21,7 @@ use ONGR\ElasticsearchBundle\DSL\ParametersTrait;
class FunctionScoreQuery implements BuilderInterface class FunctionScoreQuery implements BuilderInterface
{ {
use ParametersTrait; use ParametersTrait;
use DslTypeAwareTrait;
const USE_QUERY = 'query';
const USE_FILTER = 'filter';
/**
* @var string
*/
private $dslType;
/** /**
* @var BuilderInterface * @var BuilderInterface
...@@ -43,18 +37,13 @@ class FunctionScoreQuery implements BuilderInterface ...@@ -43,18 +37,13 @@ class FunctionScoreQuery implements BuilderInterface
* @param BuilderInterface $query * @param BuilderInterface $query
* @param array $functions * @param array $functions
* @param array $parameters * @param array $parameters
* @param string $dslType
*/ */
public function __construct( public function __construct(BuilderInterface $query, array $functions, array $parameters = [])
BuilderInterface $query, {
array $functions,
array $parameters = [],
$dslType = self::USE_QUERY
) {
$this->dslType = $dslType;
$this->query = $query; $this->query = $query;
$this->functions = $functions; $this->functions = $functions;
$this->setParameters($parameters); $this->setParameters($parameters);
$this->setDslType('query');
} }
/** /**
...@@ -71,7 +60,7 @@ class FunctionScoreQuery implements BuilderInterface ...@@ -71,7 +60,7 @@ class FunctionScoreQuery implements BuilderInterface
public function toArray() public function toArray()
{ {
$query = [ $query = [
strtolower($this->dslType) => [ strtolower($this->getDslType()) => [
$this->query->getType() => $this->query->toArray(), $this->query->getType() => $this->query->toArray(),
], ],
'functions' => [$this->functions], 'functions' => [$this->functions],
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment