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\DateComparisonExpression; |
17
|
|
|
use Ynlo\GraphQLBundle\Type\DateComparisonOperatorType; |
18
|
|
|
|
19
|
|
|
class DateFilter implements FilterInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @inheritDoc |
23
|
|
|
*/ |
24
|
8 |
|
public function __invoke(FilterContext $context, QueryBuilder $qb, $condition) |
25
|
|
|
{ |
26
|
8 |
|
if (!$condition instanceof DateComparisonExpression) { |
27
|
1 |
|
throw new \RuntimeException('Invalid filter condition'); |
28
|
|
|
} |
29
|
|
|
|
30
|
7 |
|
if (!$context->getField() || !$context->getField()->getName()) { |
31
|
1 |
|
throw new \RuntimeException('There are not valid field related to this filter.'); |
32
|
|
|
} |
33
|
|
|
|
34
|
6 |
|
$alias = $qb->getRootAliases()[0]; |
35
|
6 |
|
$column = $context->getField()->getOriginName(); |
36
|
6 |
|
if ($context->getField()->getOriginType() === 'ReflectionMethod') { |
37
|
|
|
$column = $context->getField()->getName(); |
38
|
|
|
} |
39
|
|
|
|
40
|
6 |
|
$this->applyFilter($qb, $alias, $column, $condition); |
41
|
6 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param QueryBuilder $qb |
45
|
|
|
* @param string $alias |
46
|
|
|
* @param string $column |
47
|
|
|
* @param DateComparisonExpression $condition |
48
|
|
|
*/ |
49
|
6 |
|
protected function applyFilter(QueryBuilder $qb, $alias, $column, DateComparisonExpression $condition): void |
50
|
|
|
{ |
51
|
6 |
|
$date = $condition->getDate()->format('Y-m-d H:i:s'); |
52
|
6 |
|
$maxDate = $date; |
53
|
6 |
|
if ($condition->getMaxDate()) { |
54
|
2 |
|
$maxDate = $condition->getMaxDate()->format('Y-m-d H:i:s'); |
55
|
|
|
} |
56
|
|
|
|
57
|
6 |
|
switch ($condition->getOp()) { |
58
|
6 |
|
case DateComparisonOperatorType::AFTER: |
59
|
2 |
|
if ($condition->isStrict()) { |
60
|
1 |
|
$qb->andWhere($qb->expr()->gt("{$alias}.{$column}", "'$date'")); |
61
|
|
|
} else { |
62
|
1 |
|
$qb->andWhere($qb->expr()->gte("{$alias}.{$column}", "'$date'")); |
63
|
|
|
} |
64
|
2 |
|
break; |
65
|
4 |
|
case DateComparisonOperatorType::BEFORE: |
66
|
2 |
|
if ($condition->isStrict()) { |
67
|
1 |
|
$qb->andWhere($qb->expr()->lt("{$alias}.{$column}", "'$date'")); |
68
|
|
|
} else { |
69
|
1 |
|
$qb->andWhere($qb->expr()->lte("{$alias}.{$column}", "'$date'")); |
70
|
|
|
} |
71
|
2 |
|
break; |
72
|
2 |
|
case DateComparisonOperatorType::BETWEEN: |
73
|
2 |
|
if ($condition->isStrict()) { |
74
|
1 |
|
$qb->andWhere($qb->expr()->gt("{$alias}.{$column}", "'$date'")); |
75
|
1 |
|
$qb->andWhere($qb->expr()->lt("{$alias}.{$column}", "'$maxDate'")); |
76
|
|
|
} else { |
77
|
1 |
|
$qb->andWhere($qb->expr()->between("{$alias}.{$column}", "'$date'", "'$maxDate'")); |
78
|
|
|
} |
79
|
2 |
|
break; |
80
|
|
|
} |
81
|
6 |
|
} |
82
|
|
|
} |
83
|
|
|
|