forked from zfcampus/zf-doctrine-querybuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Between.php
52 lines (43 loc) · 1.73 KB
/
Between.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
/**
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @copyright Copyright (c) 2014 Zend Technologies USA Inc. (http://www.zend.com)
*/
namespace ZF\Doctrine\QueryBuilder\Filter\ORM;
class Between extends AbstractFilter
{
public function filter($queryBuilder, $metadata, $option)
{
if (isset($option['where'])) {
if ($option['where'] === 'and') {
$queryType = 'andWhere';
} elseif ($option['where'] === 'or') {
$queryType = 'orWhere';
}
}
if (! isset($queryType)) {
$queryType = 'andWhere';
}
if (! isset($option['alias'])) {
$option['alias'] = 'row';
}
[$typeCastMetaData, $typeCastFieldName] = $this->getTypeCastParams($queryBuilder, $metadata, $option['field'], $option['alias']);
$fieldType = $typeCastMetaData->getTypeOfField($typeCastFieldName);
$format = isset($option['format']) ? $option['format'] : null;
$from = $this->typeCastField($typeCastMetaData, $typeCastFieldName, $option['from'], $format);
$to = $this->typeCastField($typeCastMetaData, $typeCastFieldName, $option['to'], $format);
$fromParameter = uniqid('a1');
$toParameter = uniqid('a2');
$queryBuilder->$queryType(
$queryBuilder
->expr()
->between(
sprintf('%s.%s', $option['alias'], $option['field']),
sprintf(':%s', $fromParameter),
sprintf(':%s', $toParameter)
)
);
$queryBuilder->setParameter($fromParameter, $from, $fieldType);
$queryBuilder->setParameter($toParameter, $to, $fieldType);
}
}