1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Filter\Common; |
12
|
|
|
|
13
|
|
|
use Doctrine\ORM\QueryBuilder; |
14
|
|
|
use Ynlo\GraphQLBundle\Filter\FilterContext; |
15
|
|
|
use Ynlo\GraphQLBundle\Filter\FilterInterface; |
16
|
|
|
use Ynlo\GraphQLBundle\Model\Filter\EnumComparisonExpression; |
17
|
|
|
use Ynlo\GraphQLBundle\Type\NodeComparisonOperatorType; |
18
|
|
|
|
19
|
|
|
class EnumFilter implements FilterInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @inheritDoc |
23
|
|
|
*/ |
24
|
4 |
|
public function __invoke(FilterContext $context, QueryBuilder $qb, $condition) |
25
|
|
|
{ |
26
|
4 |
|
if (!$condition instanceof EnumComparisonExpression) { |
27
|
1 |
|
throw new \RuntimeException('Invalid filter condition'); |
28
|
|
|
} |
29
|
|
|
|
30
|
3 |
|
if (!$context->getField() || !$context->getField()->getName()) { |
31
|
1 |
|
throw new \RuntimeException('There are not valid field related to this filter.'); |
32
|
|
|
} |
33
|
|
|
|
34
|
2 |
|
$alias = $qb->getRootAliases()[0]; |
35
|
2 |
|
$column = $context->getField()->getOriginName(); |
36
|
2 |
|
if ($context->getField()->getOriginType() === 'ReflectionMethod') { |
37
|
|
|
$column = $context->getField()->getName(); |
38
|
|
|
} |
39
|
|
|
|
40
|
2 |
|
$this->applyFilter($qb, $alias, $column, $condition); |
41
|
2 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param QueryBuilder $qb |
45
|
|
|
* @param string $alias |
46
|
|
|
* @param string $column |
47
|
|
|
* @param EnumComparisonExpression $condition |
48
|
|
|
*/ |
49
|
2 |
|
protected function applyFilter(QueryBuilder $qb, $alias, $column, EnumComparisonExpression $condition): void |
50
|
|
|
{ |
51
|
2 |
|
switch ($condition->getOp()) { |
52
|
|
|
case NodeComparisonOperatorType::IN: |
53
|
1 |
|
$qb->andWhere($qb->expr()->in("{$alias}.{$column}", $condition->getValues())); |
54
|
1 |
|
break; |
55
|
|
|
case NodeComparisonOperatorType::NIN: |
56
|
1 |
|
$qb->andWhere($qb->expr()->notIn("{$alias}.{$column}", $condition->getValues())); |
57
|
1 |
|
break; |
58
|
|
|
} |
59
|
2 |
|
} |
60
|
|
|
} |
61
|
|
|
|