|
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\FloatComparisonExpression; |
|
17
|
|
|
use Ynlo\GraphQLBundle\Type\NumberComparisonOperatorType; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Filter to compare numeric values |
|
21
|
|
|
*/ |
|
22
|
|
|
class NumberFilter implements FilterInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @inheritDoc |
|
26
|
|
|
*/ |
|
27
|
9 |
|
public function __invoke(FilterContext $context, QueryBuilder $qb, $condition) |
|
28
|
|
|
{ |
|
29
|
9 |
|
if (!$condition instanceof FloatComparisonExpression) { |
|
30
|
1 |
|
throw new \RuntimeException('Invalid filter condition'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
8 |
|
if (!$context->getField() || !$context->getField()->getName()) { |
|
34
|
1 |
|
throw new \RuntimeException('There are not valid field related to this filter.'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
7 |
|
$alias = $qb->getRootAliases()[0]; |
|
38
|
7 |
|
$column = $context->getField()->getOriginName(); |
|
39
|
7 |
|
if ($context->getField()->getOriginType() === 'ReflectionMethod') { |
|
40
|
|
|
$column = $context->getField()->getName(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
7 |
|
$this->applyFilter($qb, $alias, $column, $condition); |
|
44
|
7 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param QueryBuilder $qb |
|
48
|
|
|
* @param string $alias |
|
49
|
|
|
* @param string $column |
|
50
|
|
|
* @param FloatComparisonExpression $condition |
|
51
|
|
|
*/ |
|
52
|
7 |
|
protected function applyFilter(QueryBuilder $qb, $alias, $column, FloatComparisonExpression $condition): void |
|
53
|
|
|
{ |
|
54
|
7 |
|
switch ($condition->getOp()) { |
|
55
|
|
|
case NumberComparisonOperatorType::EQ: |
|
56
|
1 |
|
$qb->andWhere("{$alias}.{$column} = {$condition->getValue()}"); |
|
57
|
1 |
|
break; |
|
58
|
|
|
case NumberComparisonOperatorType::NEQ: |
|
59
|
1 |
|
$qb->andWhere("{$alias}.{$column} <> {$condition->getValue()}"); |
|
60
|
1 |
|
break; |
|
61
|
|
|
case NumberComparisonOperatorType::GT: |
|
62
|
1 |
|
$qb->andWhere("{$alias}.{$column} > {$condition->getValue()}"); |
|
63
|
1 |
|
break; |
|
64
|
|
|
case NumberComparisonOperatorType::GTE: |
|
65
|
1 |
|
$qb->andWhere("{$alias}.{$column} >= {$condition->getValue()}"); |
|
66
|
1 |
|
break; |
|
67
|
|
|
case NumberComparisonOperatorType::LT: |
|
68
|
1 |
|
$qb->andWhere("{$alias}.{$column} < {$condition->getValue()}"); |
|
69
|
1 |
|
break; |
|
70
|
|
|
case NumberComparisonOperatorType::LTE: |
|
71
|
1 |
|
$qb->andWhere("{$alias}.{$column} <= {$condition->getValue()}"); |
|
72
|
1 |
|
break; |
|
73
|
|
|
case NumberComparisonOperatorType::BETWEEN: |
|
74
|
1 |
|
$max = $condition->getMaxValue() ?? $condition->getValue(); |
|
75
|
1 |
|
$qb->andWhere("{$alias}.{$column} BETWEEN {$condition->getValue()} AND {$max}"); |
|
76
|
1 |
|
break; |
|
77
|
|
|
} |
|
78
|
7 |
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|