/
ExpressionBuilder.php
267 lines (230 loc) · 6.89 KB
/
ExpressionBuilder.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
<?php
/*
* This file is part of the composer-write-changelogs project.
*
* (c) Dev Spiriit <dev@spiriit.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Spiriit\Bundle\FormFilterBundle\Filter\Doctrine\Expression;
use Doctrine\ORM\Query\Expr\Comparison;
use Doctrine\ORM\Query\Expr\Literal;
use Spiriit\Bundle\FormFilterBundle\Filter\FilterOperands;
abstract class ExpressionBuilder
{
public const SQL_DATE = 'Y-m-d';
public const SQL_DATE_TIME = 'Y-m-d H:i:s';
/**
* @var mixed
*/
protected $expr;
/**
* @var boolean
*/
protected $forceCaseInsensitivity;
/**
* @var string
*/
protected $encoding;
/**
* Get expression object.
*/
public function expr()
{
return $this->expr;
}
/**
* @param boolean $forceCaseInsensitivity
* @param string|null $encoding
*/
public function __construct($forceCaseInsensitivity, $encoding = null)
{
$this->forceCaseInsensitivity = $forceCaseInsensitivity;
$this->encoding = $encoding;
}
/**
* Returns between expression if min and max not null
* Returns lte expression if max is null
* Returns gte expression if min is null
*
* @param string $field field name
* @param number $min minimum value
* @param number $max maximum value
*
* @return Comparison|string
*/
public function inRange($field, $min, $max)
{
if (!$min && !$max) {
return;
}
if (null === $min) {
// $max exists
return $this->expr()->lte($field, (float) $max);
} elseif (null === $max) {
// $min exists
return $this->expr()->gte($field, (float) $min);
}
// both $min and $max exists
return $this->between($field, (float) $min, (float) $max);
}
/**
* Creates BETWEEN() function with the given argument.
*
* @param string $field field name
* @param number $min minimum value
* @param number $max maximum value
*
* @return string
*/
public function between($field, $min, $max)
{
return $field . ' BETWEEN ' . $min . ' AND ' . $max;
}
/**
* Returns between expression if min and max not null
* Returns lte expression if max is null
* Returns gte expression if min is null
*
* @param string $field field name
* @param null|\DateTime $min start date
* @param null|\DateTime $max end date
*
* @return Comparison|string
*/
public function dateInRange($field, $min = null, $max = null)
{
if (!$min && !$max) {
return;
}
$min = $this->convertToSqlDate($min);
$max = $this->convertToSqlDate($max, true);
if (null === $min) {
// $max exists
return $this->expr()->lte($field, $max);
} elseif (null === $max) {
// $min exists
return $this->expr()->gte($field, $min);
}
// both $min and $max exists
return $this->expr()->andX(
$this->expr()->lte($field, $max),
$this->expr()->gte($field, $min)
);
}
/**
* Returns between expression if min and max not null
* Returns lte expression if max is null
* Returns gte expression if min is null
*
* @param string|\DateTime $value alias.fieldName or mysql date string format or DateTime
* @param string|\DateTime $min alias.fieldName or mysql date string format or DateTime
* @param string|\DateTime $max alias.fieldName or mysql date string format or DateTime
* @return Comparison|string
*/
public function dateTimeInRange($value, $min = null, $max = null)
{
if (!$min && !$max) {
return null;
}
$value = $this->convertToSqlDateTime($value);
$min = $this->convertToSqlDateTime($min);
$max = $this->convertToSqlDateTime($max);
if (!$max && !$min) {
return null;
}
if ($min === null) {
$findExpression = $this->expr()->lte($value, $max);
} elseif ($max === null) {
$findExpression = $this->expr()->gte($value, $min);
} else {
$findExpression = $this->expr()->andX(
$this->expr()->lte($value, $max),
$this->expr()->gte($value, $min)
);
}
return $findExpression;
}
/**
* Get string like expression
*
* @param string $field field name
* @param string $value string value
* @param int $type one of FilterOperands::STRING_* constant
*
* @return Comparison|string
*/
public function stringLike($field, $value, $type = FilterOperands::STRING_CONTAINS)
{
$value = $this->convertTypeToMask($value, $type);
return $this->expr()->like(
$this->forceCaseInsensitivity ? $this->expr()->lower($field) : $field,
$this->expr()->literal($value)
);
}
/**
* Normalize DateTime boundary
*
* @param \DateTime $date
* @param bool $isMax
*
* @return Literal|string
*/
protected function convertToSqlDate($date, $isMax = false)
{
if (!$date instanceof \DateTime) {
return;
}
$copy = clone $date;
if ($isMax) {
$copy->modify('+1 day -1 second');
}
return $this->expr()->literal($copy->format(self::SQL_DATE_TIME));
}
/**
* Normalize date time boundary
*
* @param \DateTime|string $date
* @return Literal
*/
protected function convertToSqlDateTime($date)
{
if ($date instanceof \DateTime) {
$date = $this->expr()->literal($date->format(self::SQL_DATE_TIME));
}
return $date;
}
/**
* Prepare value for like operation
*
* @param string $value
* @param int $type one of FilterOperands::STRING_*
*
* @return string
*
* @throws \InvalidArgumentException
*/
protected function convertTypeToMask($value, $type)
{
if ($this->forceCaseInsensitivity) {
$value = $this->encoding ? mb_strtolower($value, $this->encoding) : mb_strtolower($value);
}
switch ($type) {
case FilterOperands::STRING_STARTS:
$value .= '%';
break;
case FilterOperands::STRING_ENDS:
$value = '%' . $value;
break;
case FilterOperands::STRING_CONTAINS:
$value = '%' . $value . '%';
break;
case FilterOperands::STRING_EQUALS:
break;
default:
throw new \InvalidArgumentException('Wrong type constant in string like expression mapper');
}
return $value;
}
}