1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace T4webInfrastructure; |
4
|
|
|
|
5
|
|
|
use T4webDomainInterface\Infrastructure\CriteriaInterface; |
6
|
|
|
use Zend\Db\Sql\Select; |
7
|
|
|
|
8
|
|
|
class Criteria implements CriteriaInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $entityName; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $relations = []; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Config |
22
|
|
|
*/ |
23
|
|
|
protected $config; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Select |
27
|
|
|
*/ |
28
|
|
|
protected $select; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $entityName |
32
|
|
|
* @param Config $config |
33
|
|
|
* @param Select $select |
34
|
|
|
*/ |
35
|
|
|
public function __construct($entityName, Config $config, Select $select = null) |
36
|
|
|
{ |
37
|
|
|
$this->entityName = $entityName; |
38
|
|
|
$this->config = $config; |
39
|
|
|
if ($select === null) { |
40
|
|
|
$this->select = new Select(); |
41
|
|
|
$this->select->from($this->config->getTable($this->entityName)); |
42
|
|
|
} else { |
43
|
|
|
$this->select = $select; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return Select |
49
|
|
|
*/ |
50
|
|
|
public function getQuery() |
51
|
|
|
{ |
52
|
|
|
return $this->select; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function getEntityName() |
59
|
|
|
{ |
60
|
|
|
return $this->entityName; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $entityName |
65
|
|
|
* @return CriteriaInterface |
66
|
|
|
*/ |
67
|
|
|
public function relation($entityName) |
68
|
|
|
{ |
69
|
|
|
if (isset($this->relations[$entityName])) { |
70
|
|
|
return $this->relations[$entityName]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ($this->config->isRelationManyToMany($this->entityName, $entityName)) { |
74
|
|
|
|
75
|
|
|
list($linkTable, |
76
|
|
|
$mainField, |
77
|
|
|
$joinedField) = $this->config->getRelationManyToMany($this->entityName, $entityName); |
78
|
|
|
|
79
|
|
|
$mainTable = $this->config->getTable($this->entityName); |
80
|
|
|
$joinedTable = $this->config->getTable($entityName); |
81
|
|
|
|
82
|
|
|
$this->select->join( |
83
|
|
|
$linkTable, |
84
|
|
|
"$linkTable.$mainField = $mainTable.id", |
85
|
|
|
[] |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$this->select->join( |
89
|
|
|
$joinedTable, |
90
|
|
|
"$linkTable.$joinedField = $joinedTable.id", |
91
|
|
|
[] |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
} else { |
95
|
|
|
$table = $this->config->getTable($entityName); |
96
|
|
|
|
97
|
|
|
$this->select->join( |
98
|
|
|
$table, |
99
|
|
|
$this->config->getRelationExpression($this->entityName, $entityName), |
100
|
|
|
[] |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$relationCriteria = new self($entityName, $this->config, $this->select); |
105
|
|
|
|
106
|
|
|
$this->relations[$entityName] = $relationCriteria; |
107
|
|
|
|
108
|
|
|
return $relationCriteria; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param string $attribute |
113
|
|
|
* @param bool|int|float|string $value |
114
|
|
|
* @return $this |
115
|
|
|
*/ |
116
|
|
|
public function equalTo($attribute, $value) |
117
|
|
|
{ |
118
|
|
|
$this->select->where->equalTo($this->getField($attribute), $value); |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $attribute |
125
|
|
|
* @param bool|int|float|string $value |
126
|
|
|
* @return $this |
127
|
|
|
*/ |
128
|
|
|
public function notEqualTo($attribute, $value) |
129
|
|
|
{ |
130
|
|
|
$this->select->where->notEqualTo($this->getField($attribute), $value); |
131
|
|
|
|
132
|
|
|
return $this; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string $attribute |
137
|
|
|
* @param int|float $value |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
|
|
public function lessThan($attribute, $value) |
141
|
|
|
{ |
142
|
|
|
$this->select->where->lessThan($this->getField($attribute), $value); |
143
|
|
|
|
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $attribute |
149
|
|
|
* @param int|float $value |
150
|
|
|
* @return $this |
151
|
|
|
*/ |
152
|
|
|
public function greaterThan($attribute, $value) |
153
|
|
|
{ |
154
|
|
|
$this->select->where->greaterThan($this->getField($attribute), $value); |
155
|
|
|
|
156
|
|
|
return $this; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string $attribute |
161
|
|
|
* @param int|float $value |
162
|
|
|
* @return $this |
163
|
|
|
*/ |
164
|
|
|
public function greaterThanOrEqualTo($attribute, $value) |
165
|
|
|
{ |
166
|
|
|
$this->select->where->greaterThanOrEqualTo($this->getField($attribute), $value); |
167
|
|
|
|
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param string $attribute |
173
|
|
|
* @param int|float $value |
174
|
|
|
* @return $this |
175
|
|
|
*/ |
176
|
|
|
public function lessThanOrEqualTo($attribute, $value) |
177
|
|
|
{ |
178
|
|
|
$this->select->where->lessThanOrEqualTo($this->getField($attribute), $value); |
179
|
|
|
|
180
|
|
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @param string $attribute |
185
|
|
|
* @param int|float $value |
186
|
|
|
* @return $this |
187
|
|
|
*/ |
188
|
|
|
public function like($attribute, $value) |
189
|
|
|
{ |
190
|
|
|
$this->select->where->like($this->getField($attribute), $value); |
191
|
|
|
|
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param string $attribute |
197
|
|
|
* @return $this |
198
|
|
|
*/ |
199
|
|
|
public function isNull($attribute) |
200
|
|
|
{ |
201
|
|
|
$this->select->where->isNull($this->getField($attribute)); |
202
|
|
|
|
203
|
|
|
return $this; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param string $attribute |
208
|
|
|
* @return $this |
209
|
|
|
*/ |
210
|
|
|
public function isNotNull($attribute) |
211
|
|
|
{ |
212
|
|
|
$this->select->where->isNotNull($this->getField($attribute)); |
213
|
|
|
|
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param string $attribute |
219
|
|
|
* @param array $values |
220
|
|
|
* @return $this |
221
|
|
|
*/ |
222
|
|
|
public function in($attribute, array $values) |
223
|
|
|
{ |
224
|
|
|
$this->select->where->in($this->getField($attribute), $values); |
225
|
|
|
|
226
|
|
|
return $this; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param string $attribute |
231
|
|
|
* @param array $values |
232
|
|
|
* @return $this |
233
|
|
|
*/ |
234
|
|
|
public function notIn($attribute, array $values) |
235
|
|
|
{ |
236
|
|
|
$this->select->where->notIn($this->getField($attribute), $values); |
237
|
|
|
|
238
|
|
|
return $this; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @param string $attribute |
243
|
|
|
* @param int|float|string $minValue |
244
|
|
|
* @param int|float|string $maxValue |
245
|
|
|
* @return $this |
246
|
|
|
*/ |
247
|
|
|
public function between($attribute, $minValue, $maxValue) |
248
|
|
|
{ |
249
|
|
|
$this->select->where->between($this->getField($attribute), $minValue, $maxValue); |
250
|
|
|
|
251
|
|
|
return $this; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param string $attribute |
256
|
|
|
* @return $this |
257
|
|
|
*/ |
258
|
|
|
public function order($attribute) |
259
|
|
|
{ |
260
|
|
|
$orders = explode(',', $attribute); |
261
|
|
|
$sqlOrders = []; |
262
|
|
|
|
263
|
|
|
foreach ($orders as $item) { |
264
|
|
|
$exploded = explode(' ', trim($item)); |
265
|
|
|
if (count($exploded) == 2) { |
266
|
|
|
$order = $this->getField($exploded[0]) . ' ' . $exploded[1]; |
267
|
|
|
} else { |
268
|
|
|
$order = $this->getField($attribute); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
$sqlOrders[] = $order; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
$this->select->order($sqlOrders); |
275
|
|
|
|
276
|
|
|
return $this; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @param int $limit |
281
|
|
|
* @return $this |
282
|
|
|
*/ |
283
|
|
|
public function limit($limit) |
284
|
|
|
{ |
285
|
|
|
$this->select->limit($limit); |
286
|
|
|
|
287
|
|
|
return $this; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @param int $offset |
292
|
|
|
* @return $this |
293
|
|
|
*/ |
294
|
|
|
public function offset($offset) |
295
|
|
|
{ |
296
|
|
|
$this->select->offset($offset); |
297
|
|
|
|
298
|
|
|
return $this; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* @param $attribute |
303
|
|
|
* @return string |
304
|
|
|
*/ |
305
|
|
|
public function getField($attribute) |
306
|
|
|
{ |
307
|
|
|
$table = $this->config->getTable($this->entityName); |
308
|
|
|
$field = $this->config->getFiled($this->entityName, $attribute); |
309
|
|
|
|
310
|
|
|
return $table.".".$field; |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|