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

has_child and has_parent improvements

parent 2fb5f4df
No related branches found
No related tags found
No related merge requests found
...@@ -21,6 +21,9 @@ class HasChildFilter implements BuilderInterface ...@@ -21,6 +21,9 @@ class HasChildFilter implements BuilderInterface
{ {
use ParametersTrait; use ParametersTrait;
const INNER_QUERY = 'query';
const INNER_FILTER = 'filter';
/** /**
* @var string * @var string
*/ */
...@@ -31,15 +34,34 @@ class HasChildFilter implements BuilderInterface ...@@ -31,15 +34,34 @@ class HasChildFilter implements BuilderInterface
*/ */
private $filter; private $filter;
/**
* @var BuilderInterface
*/
private $query;
/** /**
* @param string $type * @param string $type
* @param BuilderInterface $filter * @param BuilderInterface $block
* @param array $parameters * @param array $parameters
* @param string $inner
*
* @throws \InvalidArgumentException
*/ */
public function __construct($type, BuilderInterface $filter, array $parameters = []) public function __construct($type, BuilderInterface $block, array $parameters = [], $inner = self::INNER_FILTER)
{ {
$this->type = $type; $this->type = $type;
$this->filter = $filter;
switch ($inner) {
case 'filter':
$this->filter = $block;
break;
case 'query':
$this->query = $block;
break;
default:
throw new \InvalidArgumentException('Not supported argument type');
}
$this->setParameters($parameters); $this->setParameters($parameters);
} }
...@@ -56,12 +78,17 @@ class HasChildFilter implements BuilderInterface ...@@ -56,12 +78,17 @@ class HasChildFilter implements BuilderInterface
*/ */
public function toArray() public function toArray()
{ {
$query = [ $query = [ 'type' => $this->type ];
'type' => $this->type,
'filter' => [ $queries = ['filter', 'query'];
$this->filter->getType() => $this->filter->toArray(),
], foreach ($queries as $type) {
]; if ($this->{$type}) {
$query[$type] = [
$this->{$type}->getType() => $this->{$type}->toArray(),
];
}
}
$output = $this->processArray($query); $output = $this->processArray($query);
......
...@@ -21,6 +21,9 @@ class HasParentFilter implements BuilderInterface ...@@ -21,6 +21,9 @@ class HasParentFilter implements BuilderInterface
{ {
use ParametersTrait; use ParametersTrait;
const INNER_QUERY = 'query';
const INNER_FILTER = 'filter';
/** /**
* @var string * @var string
*/ */
...@@ -31,15 +34,38 @@ class HasParentFilter implements BuilderInterface ...@@ -31,15 +34,38 @@ class HasParentFilter implements BuilderInterface
*/ */
private $filter; private $filter;
/**
* @var BuilderInterface
*/
private $query;
/** /**
* @param string $parentType * @param string $parentType
* @param BuilderInterface $filter * @param BuilderInterface $block
* @param array $parameters * @param array $parameters
* @param string $inner
*
* @throws \InvalidArgumentException
*/ */
public function __construct($parentType, BuilderInterface $filter, array $parameters = []) public function __construct(
{ $parentType,
BuilderInterface $block,
array $parameters = [],
$inner = self::INNER_FILTER
) {
$this->parentType = $parentType; $this->parentType = $parentType;
$this->filter = $filter;
switch ($inner) {
case 'filter':
$this->filter = $block;
break;
case 'query':
$this->query = $block;
break;
default:
throw new \InvalidArgumentException('Not supported argument type');
}
$this->setParameters($parameters); $this->setParameters($parameters);
} }
...@@ -56,12 +82,17 @@ class HasParentFilter implements BuilderInterface ...@@ -56,12 +82,17 @@ class HasParentFilter implements BuilderInterface
*/ */
public function toArray() public function toArray()
{ {
$query = [ $query = [ 'parent_type' => $this->parentType ];
'parent_type' => $this->parentType,
'filter' => [ $queries = ['filter', 'query'];
$this->filter->getType() => $this->filter->toArray(),
], foreach ($queries as $type) {
]; if ($this->{$type}) {
$query[$type] = [
$this->{$type}->getType() => $this->{$type}->toArray(),
];
}
}
$output = $this->processArray($query); $output = $this->processArray($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