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

Refactored code

parent 67e1a417
No related branches found
No related tags found
No related merge requests found
......@@ -21,19 +21,14 @@ class HasChildFilter implements BuilderInterface
{
use ParametersTrait;
const INNER_QUERY = 'query';
const INNER_FILTER = 'filter';
const USE_QUERY = 'query';
const USE_FILTER = 'filter';
/**
* @var string
*/
private $type;
/**
* @var BuilderInterface
*/
private $filter;
/**
* @var BuilderInterface
*/
......@@ -41,27 +36,17 @@ class HasChildFilter implements BuilderInterface
/**
* @param string $type
* @param BuilderInterface $block
* @param BuilderInterface $query
* @param array $parameters
* @param string $inner
* @param string $dslType
*
* @throws \InvalidArgumentException
*/
public function __construct($type, BuilderInterface $block, array $parameters = [], $inner = self::INNER_FILTER)
public function __construct($type, BuilderInterface $query, array $parameters = [], $dslType = self::USE_FILTER)
{
$this->type = $type;
switch ($inner) {
case 'filter':
$this->filter = $block;
break;
case 'query':
$this->query = $block;
break;
default:
throw new \InvalidArgumentException('Not supported argument type');
}
$this->dslType = $dslType;
$this->query = $query;
$this->setParameters($parameters);
}
......@@ -78,17 +63,10 @@ class HasChildFilter implements BuilderInterface
*/
public function toArray()
{
$query = [ 'type' => $this->type ];
$queries = ['filter', 'query'];
foreach ($queries as $type) {
if ($this->{$type}) {
$query[$type] = [
$this->{$type}->getType() => $this->{$type}->toArray(),
];
}
}
$query = [
'type' => $this->type,
$this->dslType => [$this->query->getType() => $this->query->toArray()],
];
$output = $this->processArray($query);
......
......@@ -21,19 +21,14 @@ class HasParentFilter implements BuilderInterface
{
use ParametersTrait;
const INNER_QUERY = 'query';
const INNER_FILTER = 'filter';
const USE_QUERY = 'query';
const USE_FILTER = 'filter';
/**
* @var string
*/
private $parentType;
/**
* @var BuilderInterface
*/
private $filter;
/**
* @var BuilderInterface
*/
......@@ -41,31 +36,21 @@ class HasParentFilter implements BuilderInterface
/**
* @param string $parentType
* @param BuilderInterface $block
* @param BuilderInterface $query
* @param array $parameters
* @param string $inner
* @param string $dslType
*
* @throws \InvalidArgumentException
*/
public function __construct(
$parentType,
BuilderInterface $block,
BuilderInterface $query,
array $parameters = [],
$inner = self::INNER_FILTER
$dslType = self::USE_FILTER
) {
$this->parentType = $parentType;
switch ($inner) {
case 'filter':
$this->filter = $block;
break;
case 'query':
$this->query = $block;
break;
default:
throw new \InvalidArgumentException('Not supported argument type');
}
$this->dslType = $dslType;
$this->query = $query;
$this->setParameters($parameters);
}
......@@ -82,17 +67,10 @@ class HasParentFilter implements BuilderInterface
*/
public function toArray()
{
$query = [ 'parent_type' => $this->parentType ];
$queries = ['filter', 'query'];
foreach ($queries as $type) {
if ($this->{$type}) {
$query[$type] = [
$this->{$type}->getType() => $this->{$type}->toArray(),
];
}
}
$query = [
'parent_type' => $this->parentType,
$this->dslType => [$this->query->getType() => $this->query->toArray()],
];
$output = $this->processArray($query);
......
......@@ -21,6 +21,9 @@ class ConstantScoreQuery implements BuilderInterface
{
use ParametersTrait;
const USE_QUERY = 'query';
const USE_FILTER = 'filter';
/**
* @var string
*/
......@@ -29,16 +32,17 @@ class ConstantScoreQuery implements BuilderInterface
/**
* @var BuilderInterface
*/
private $filterOrQuery;
private $query;
/**
* @param BuilderInterface $filterOrQuery
* @param BuilderInterface $query
* @param array $parameters
* @param string $dslType
*/
public function __construct(BuilderInterface $filterOrQuery, array $parameters = [])
public function __construct(BuilderInterface $query, array $parameters = [], $dslType = self::USE_FILTER)
{
$this->dslType = array_slice(explode('\\', get_class($filterOrQuery)), -2, 1)[0];
$this->filterOrQuery = $filterOrQuery;
$this->dslType = $dslType;
$this->query = $query;
$this->setParameters($parameters);
}
......@@ -57,7 +61,7 @@ class ConstantScoreQuery implements BuilderInterface
{
$query = [
strtolower($this->dslType) => [
$this->filterOrQuery->getType() => $this->filterOrQuery->toArray(),
$this->query->getType() => $this->query->toArray(),
],
];
......
......@@ -21,6 +21,9 @@ class FunctionScoreQuery implements BuilderInterface
{
use ParametersTrait;
const USE_QUERY = 'query';
const USE_FILTER = 'filter';
/**
* @var string
*/
......@@ -29,7 +32,7 @@ class FunctionScoreQuery implements BuilderInterface
/**
* @var BuilderInterface
*/
private $filterOrQuery;
private $query;
/**
* @var array[]
......@@ -37,14 +40,19 @@ class FunctionScoreQuery implements BuilderInterface
private $functions;
/**
* @param BuilderInterface $filterOrQuery
* @param BuilderInterface $query
* @param array $functions
* @param array $parameters
* @param string $dslType
*/
public function __construct(BuilderInterface $filterOrQuery, array $functions, array $parameters = [])
{
$this->dslType = array_slice(explode('\\', get_class($filterOrQuery)), -2, 1)[0];
$this->filterOrQuery = $filterOrQuery;
public function __construct(
BuilderInterface $query,
array $functions,
array $parameters = [],
$dslType = self::USE_FILTER
) {
$this->dslType = $dslType;
$this->query = $query;
$this->functions = $functions;
$this->setParameters($parameters);
}
......@@ -64,7 +72,7 @@ class FunctionScoreQuery implements BuilderInterface
{
$query = [
strtolower($this->dslType) => [
$this->filterOrQuery->getType() => $this->filterOrQuery->toArray(),
$this->query->getType() => $this->query->toArray(),
],
'functions' => [$this->functions],
];
......
......@@ -24,22 +24,22 @@ class MatchQuery implements BuilderInterface
/**
* @var string
*/
private $query;
private $field;
/**
* @var string
*/
private $field;
private $query;
/**
* @param string $query
* @param string $field
* @param string $query
* @param array $parameters
*/
public function __construct($query, $field, array $parameters = [])
public function __construct($field, $query, array $parameters = [])
{
$this->query = $query;
$this->field = $field;
$this->query = $query;
$this->setParameters($parameters);
}
......
......@@ -19,9 +19,14 @@ use ONGR\ElasticsearchBundle\DSL\BuilderInterface;
class MultiMatchQuery implements BuilderInterface
{
/**
* @var string
* @param array $fields
* @param string $query
*/
private $query;
public function __construct(array $fields, $query)
{
$this->fields = $fields;
$this->query = $query;
}
/**
* @var array
......@@ -29,14 +34,9 @@ class MultiMatchQuery implements BuilderInterface
private $fields = [];
/**
* @param string $query
* @param array $fields
* @var string
*/
public function __construct($query, array $fields)
{
$this->query = $query;
$this->fields = $fields;
}
private $query;
/**
* {@inheritdoc}
......@@ -52,8 +52,8 @@ class MultiMatchQuery implements BuilderInterface
public function toArray()
{
return [
'query' => $this->query,
'fields' => $this->fields,
'query' => $this->query,
];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment