1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arp\LaminasDoctrine\Query\Filter; |
6
|
|
|
|
7
|
|
|
use Arp\LaminasDoctrine\Query\Constant\WhereType; |
8
|
|
|
use Arp\LaminasDoctrine\Query\Exception\InvalidArgumentException; |
9
|
|
|
use Arp\LaminasDoctrine\Query\Metadata\MetadataInterface; |
10
|
|
|
use Arp\LaminasDoctrine\Query\QueryBuilderInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @author Alex Patterson <[email protected]> |
14
|
|
|
* @package Arp\LaminasDoctrine\Query\Filter |
15
|
|
|
*/ |
16
|
|
|
final class Between extends AbstractFilter |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param QueryBuilderInterface $queryBuilder |
20
|
|
|
* @param MetadataInterface $metadata |
21
|
|
|
* @param array $criteria |
22
|
|
|
* |
23
|
|
|
* @throws InvalidArgumentException |
24
|
|
|
*/ |
25
|
|
|
public function filter(QueryBuilderInterface $queryBuilder, MetadataInterface $metadata, array $criteria): void |
26
|
|
|
{ |
27
|
|
|
$fieldName = $this->resolveFieldName($metadata, $criteria); |
28
|
|
|
|
29
|
|
|
$queryAlias = $criteria['alias'] ?? 'entity'; |
30
|
|
|
|
31
|
|
|
$fromParamName = uniqid($queryAlias, false); |
32
|
|
|
$toParamName = uniqid($queryAlias, false); |
33
|
|
|
|
34
|
|
|
$expression = $queryBuilder->expr()->between( |
35
|
|
|
$queryAlias . '.' . $fieldName, |
36
|
|
|
':' . $fromParamName, |
37
|
|
|
':' . $toParamName |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
if (!isset($criteria['where']) || WhereType::AND === $criteria['where']) { |
41
|
|
|
$queryBuilder->andWhere($expression); |
42
|
|
|
} else { |
43
|
|
|
$queryBuilder->orWhere($expression); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$queryBuilder->setParameter( |
47
|
|
|
$fromParamName, |
48
|
|
|
$this->formatValue($metadata, $fieldName, $criteria['from'], $criteria['format'] ?? null) |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
$queryBuilder->setParameter( |
52
|
|
|
$toParamName, |
53
|
|
|
$this->formatValue($metadata, $fieldName, $criteria['to'], $criteria['format'] ?? null) |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|