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\StringComparisonExpression; |
17
|
|
|
use Ynlo\GraphQLBundle\Type\StringComparisonOperatorType; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* string filter to compare strings and filter by them |
21
|
|
|
*/ |
22
|
|
|
class StringFilter implements FilterInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @inheritDoc |
26
|
|
|
*/ |
27
|
7 |
|
public function __invoke(FilterContext $context, QueryBuilder $qb, $condition) |
28
|
|
|
{ |
29
|
7 |
|
if (!$condition instanceof StringComparisonExpression) { |
30
|
1 |
|
throw new \RuntimeException('Invalid filter condition'); |
31
|
|
|
} |
32
|
|
|
|
33
|
6 |
|
if (!$context->getField() || !$context->getField()->getName()) { |
34
|
1 |
|
throw new \RuntimeException('There are not valid field related to this filter.'); |
35
|
|
|
} |
36
|
|
|
|
37
|
5 |
|
$alias = $qb->getRootAliases()[0]; |
38
|
5 |
|
$column = $context->getField()->getOriginName(); |
39
|
5 |
|
if ($context->getField()->getOriginType() === 'ReflectionMethod') { |
40
|
|
|
$column = $context->getField()->getName(); |
41
|
|
|
} |
42
|
|
|
|
43
|
5 |
|
$this->applyFilter($qb, $alias, $column, $condition); |
44
|
5 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param QueryBuilder $qb |
48
|
|
|
* @param string $alias |
49
|
|
|
* @param string $column |
50
|
|
|
* @param StringComparisonExpression $condition |
51
|
|
|
*/ |
52
|
5 |
|
protected function applyFilter(QueryBuilder $qb, $alias, $column, StringComparisonExpression $condition): void |
53
|
|
|
{ |
54
|
5 |
|
switch ($condition->getOp()) { |
55
|
|
|
case StringComparisonOperatorType::EQUAL: |
56
|
2 |
|
$qb->andWhere("{$alias}.{$column} = '{$condition->getValue()}'"); |
57
|
2 |
|
break; |
58
|
|
|
case StringComparisonOperatorType::CONTAINS: |
59
|
1 |
|
$qb->andWhere("{$alias}.{$column} LIKE '%{$condition->getValue()}%'"); |
60
|
1 |
|
break; |
61
|
|
|
case StringComparisonOperatorType::STARTS_WITH: |
62
|
1 |
|
$qb->andWhere("{$alias}.{$column} LIKE '{$condition->getValue()}%'"); |
63
|
1 |
|
break; |
64
|
|
|
case StringComparisonOperatorType::ENDS_WITH: |
65
|
1 |
|
$qb->andWhere("{$alias}.{$column} LIKE '%{$condition->getValue()}'"); |
66
|
1 |
|
break; |
67
|
|
|
} |
68
|
5 |
|
} |
69
|
|
|
} |
70
|
|
|
|