1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Happyr Doctrine Specification package. |
5
|
|
|
* |
6
|
|
|
* (c) Tobias Nyholm <[email protected]> |
7
|
|
|
* Kacper Gunia <[email protected]> |
8
|
|
|
* Peter Gribanov <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Happyr\DoctrineSpecification\Operand; |
15
|
|
|
|
16
|
|
|
use Doctrine\ORM\QueryBuilder; |
17
|
|
|
use Happyr\DoctrineSpecification\Exception\InvalidArgumentException; |
18
|
|
|
|
19
|
|
|
class PlatformFunction implements Operand |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Doctrine internal functions. |
23
|
|
|
* |
24
|
|
|
* @see \Doctrine\ORM\Query\Parser |
25
|
|
|
* |
26
|
|
|
* @var string[] |
27
|
|
|
*/ |
28
|
|
|
private static $doctrineFunctions = [ |
29
|
|
|
// String functions |
30
|
|
|
'concat', |
31
|
|
|
'substring', |
32
|
|
|
'trim', |
33
|
|
|
'lower', |
34
|
|
|
'upper', |
35
|
|
|
'identity', |
36
|
|
|
// Numeric functions |
37
|
|
|
'length', |
38
|
|
|
'locate', |
39
|
|
|
'abs', |
40
|
|
|
'sqrt', |
41
|
|
|
'mod', |
42
|
|
|
'size', |
43
|
|
|
'date_diff', |
44
|
|
|
'bit_and', |
45
|
|
|
'bit_or', |
46
|
|
|
// Aggregate functions |
47
|
|
|
'min', |
48
|
|
|
'max', |
49
|
|
|
'avg', |
50
|
|
|
'sum', |
51
|
|
|
'count', |
52
|
|
|
// Datetime functions |
53
|
|
|
'current_date', |
54
|
|
|
'current_time', |
55
|
|
|
'current_timestamp', |
56
|
|
|
'date_add', |
57
|
|
|
'date_sub', |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
private $functionName = ''; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var array |
67
|
|
|
*/ |
68
|
|
|
private $arguments = []; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $functionName |
72
|
|
|
* @param mixed $arguments |
73
|
|
|
*/ |
74
|
|
|
public function __construct($functionName, $arguments = []) |
75
|
|
|
{ |
76
|
|
|
// NEXT_MAJOR: use variable-length argument lists ($functionName, ...$arguments) |
77
|
|
|
if (2 === func_num_args()) { |
78
|
|
|
$this->functionName = $functionName; |
79
|
|
|
$this->arguments = (array) $arguments; |
80
|
|
|
} else { |
81
|
|
|
$arguments = func_get_args(); |
82
|
|
|
$this->functionName = array_shift($arguments); |
83
|
|
|
$this->arguments = $arguments; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param QueryBuilder $qb |
89
|
|
|
* @param string $dqlAlias |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function transform(QueryBuilder $qb, $dqlAlias) |
94
|
|
|
{ |
95
|
|
|
if (!in_array(strtolower($this->functionName), self::$doctrineFunctions) && |
96
|
|
|
!$qb->getEntityManager()->getConfiguration()->getCustomStringFunction($this->functionName) && |
97
|
|
|
!$qb->getEntityManager()->getConfiguration()->getCustomNumericFunction($this->functionName) && |
98
|
|
|
!$qb->getEntityManager()->getConfiguration()->getCustomDatetimeFunction($this->functionName) |
99
|
|
|
) { |
100
|
|
|
throw new InvalidArgumentException(sprintf('"%s" is not a valid function name.', $this->functionName)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$arguments = []; |
104
|
|
|
foreach (ArgumentToOperandConverter::convert($this->arguments) as $argument) { |
105
|
|
|
$arguments[] = $argument->transform($qb, $dqlAlias); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return sprintf('%s(%s)', $this->functionName, implode(', ', $arguments)); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|