1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Jared King <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @see http://jaredtking.com |
7
|
|
|
* |
8
|
|
|
* @copyright 2015 Jared King |
9
|
|
|
* @license MIT |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Pulsar; |
13
|
|
|
|
14
|
|
|
use Pulsar\Exception\ModelNotFoundException; |
15
|
|
|
use Pulsar\Relation\Relationship; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Represents a query against a model type. |
19
|
|
|
*/ |
20
|
|
|
class Query |
21
|
|
|
{ |
22
|
|
|
const DEFAULT_LIMIT = 100; |
23
|
|
|
const MAX_LIMIT = 1000; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Model|string |
27
|
|
|
*/ |
28
|
|
|
private $model; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $joins; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
private $eagerLoaded; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private $where; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var int |
47
|
|
|
*/ |
48
|
|
|
private $limit; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var int |
52
|
|
|
*/ |
53
|
|
|
private $start; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
private $sort; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param Model|string $model |
62
|
|
|
*/ |
63
|
|
|
public function __construct($model = '') |
64
|
|
|
{ |
65
|
|
|
$this->model = $model; |
66
|
|
|
$this->joins = []; |
67
|
|
|
$this->eagerLoaded = []; |
68
|
|
|
$this->where = []; |
69
|
|
|
$this->start = 0; |
70
|
|
|
$this->limit = self::DEFAULT_LIMIT; |
71
|
|
|
$this->sort = []; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Gets the model class associated with this query. |
76
|
|
|
* |
77
|
|
|
* @return Model|string |
78
|
|
|
*/ |
79
|
|
|
public function getModel() |
80
|
|
|
{ |
81
|
|
|
return $this->model; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Sets the limit for this query. |
86
|
|
|
* |
87
|
|
|
* @return $this |
88
|
|
|
*/ |
89
|
|
|
public function limit(int $limit) |
90
|
|
|
{ |
91
|
|
|
$this->limit = min($limit, self::MAX_LIMIT); |
92
|
|
|
|
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Gets the limit for this query. |
98
|
|
|
*/ |
99
|
|
|
public function getLimit(): int |
100
|
|
|
{ |
101
|
|
|
return $this->limit; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Sets the start offset. |
106
|
|
|
* |
107
|
|
|
* @return $this |
108
|
|
|
*/ |
109
|
|
|
public function start(int $start) |
110
|
|
|
{ |
111
|
|
|
$this->start = max($start, 0); |
112
|
|
|
|
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Gets the start offset. |
118
|
|
|
*/ |
119
|
|
|
public function getStart(): int |
120
|
|
|
{ |
121
|
|
|
return $this->start; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Sets the sort pattern for the query. |
126
|
|
|
* |
127
|
|
|
* @param array|string $sort |
128
|
|
|
* |
129
|
|
|
* @return $this |
130
|
|
|
*/ |
131
|
|
|
public function sort($sort) |
132
|
|
|
{ |
133
|
|
|
$columns = explode(',', $sort); |
134
|
|
|
|
135
|
|
|
$sortParams = []; |
136
|
|
|
foreach ($columns as $column) { |
137
|
|
|
$c = explode(' ', trim($column)); |
138
|
|
|
|
139
|
|
|
if (2 != count($c)) { |
140
|
|
|
continue; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// validate direction |
144
|
|
|
$direction = strtolower($c[1]); |
145
|
|
|
if (!in_array($direction, ['asc', 'desc'])) { |
146
|
|
|
continue; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$sortParams[] = [$c[0], $direction]; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$this->sort = $sortParams; |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Gets the sort parameters. |
159
|
|
|
*/ |
160
|
|
|
public function getSort(): array |
161
|
|
|
{ |
162
|
|
|
return $this->sort; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Sets the where parameters. |
167
|
|
|
* Accepts the following forms: |
168
|
|
|
* i) where(['name' => 'Bob']) |
169
|
|
|
* ii) where('name', 'Bob') |
170
|
|
|
* iii) where('balance', 100, '>') |
171
|
|
|
* iv) where('balance > 100'). |
172
|
|
|
* |
173
|
|
|
* @param array|string $where |
174
|
|
|
* @param mixed $value optional value |
175
|
|
|
* @param string|null $condition optional condition |
176
|
|
|
* |
177
|
|
|
* @return $this |
178
|
|
|
*/ |
179
|
|
|
public function where($where, $value = null, $condition = null) |
180
|
|
|
{ |
181
|
|
|
// handles i. |
182
|
|
|
if (is_array($where)) { |
183
|
|
|
$this->where = array_merge($this->where, $where); |
184
|
|
|
} else { |
185
|
|
|
// handles iii. |
186
|
|
|
$args = func_num_args(); |
187
|
|
|
if ($args > 2) { |
188
|
|
|
$this->where[] = [$where, $value, $condition]; |
189
|
|
|
// handles ii. |
190
|
|
|
} elseif (2 == $args) { |
191
|
|
|
$this->where[$where] = $value; |
192
|
|
|
// handles iv. |
193
|
|
|
} else { |
194
|
|
|
$this->where[] = $where; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Gets the where parameters. |
203
|
|
|
*/ |
204
|
|
|
public function getWhere(): array |
205
|
|
|
{ |
206
|
|
|
return $this->where; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Adds a join to the query. Matches a property on this model |
211
|
|
|
* to the ID of the model we are joining. |
212
|
|
|
* |
213
|
|
|
* @param string $model model being joined |
214
|
|
|
* @param string $column name of local property |
215
|
|
|
* |
216
|
|
|
* @return $this |
217
|
|
|
*/ |
218
|
|
|
public function join($model, string $column, string $foreignKey) |
219
|
|
|
{ |
220
|
|
|
$this->joins[] = [$model, $column, $foreignKey]; |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Gets the joins. |
227
|
|
|
*/ |
228
|
|
|
public function getJoins(): array |
229
|
|
|
{ |
230
|
|
|
return $this->joins; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Marks a relationship property on the model that should be eager loaded. |
235
|
|
|
* |
236
|
|
|
* @param string $k local property containing the relationship |
237
|
|
|
* |
238
|
|
|
* @return $this |
239
|
|
|
*/ |
240
|
|
|
public function with(string $k) |
241
|
|
|
{ |
242
|
|
|
if (!in_array($k, $this->eagerLoaded)) { |
243
|
|
|
$this->eagerLoaded[] = $k; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
return $this; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Gets the relationship properties that are going to be eager-loaded. |
251
|
|
|
*/ |
252
|
|
|
public function getWith(): array |
253
|
|
|
{ |
254
|
|
|
return $this->eagerLoaded; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Executes the query against the model's driver. |
259
|
|
|
* |
260
|
|
|
* @return Model[] results |
261
|
|
|
*/ |
262
|
|
|
public function execute(): array |
263
|
|
|
{ |
264
|
|
|
// instantiate a model so that initialize() is called and properties are filled in |
265
|
|
|
// otherwise this empty model is not used |
266
|
|
|
$modelClass = $this->model; |
267
|
|
|
$model = new $modelClass(); |
|
|
|
|
268
|
|
|
|
269
|
|
|
return Hydrator::hydrate($modelClass::getDriver()->queryModels($this), $modelClass, $this->eagerLoaded); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Creates an iterator for a search. |
274
|
|
|
* |
275
|
|
|
* @return Iterator |
276
|
|
|
*/ |
277
|
|
|
public function all() |
278
|
|
|
{ |
279
|
|
|
return new Iterator($this); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Finds exactly one model. If zero or more than one are found |
284
|
|
|
* then this function will fail. |
285
|
|
|
* |
286
|
|
|
* @throws ModelNotFoundException when the result is not exactly one model |
287
|
|
|
*/ |
288
|
|
|
public function one(): Model |
289
|
|
|
{ |
290
|
|
|
$models = $this->limit(1)->execute(); |
291
|
|
|
|
292
|
|
|
if (0 == count($models)) { |
293
|
|
|
$model = $this->model; |
294
|
|
|
throw new ModelNotFoundException('Could not find '.$model::modelName()); |
295
|
|
|
} elseif (count($models) > 1) { |
296
|
|
|
$model = $this->model; |
297
|
|
|
throw new ModelNotFoundException('Found more than one '.$model::modelName().' when only one result should have been found.'); |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
return $models[0]; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Executes the query against the model's driver and returns the first result. |
305
|
|
|
* |
306
|
|
|
* @return Model[]|Model|null when $limit = 1, returns a single model or null, otherwise returns an array |
307
|
|
|
*/ |
308
|
|
|
public function first(int $limit = 1) |
309
|
|
|
{ |
310
|
|
|
$models = $this->limit($limit)->execute(); |
311
|
|
|
|
312
|
|
|
if (1 == $limit) { |
313
|
|
|
return (1 == count($models)) ? $models[0] : null; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
return $models; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* Gets the number of models matching the query. |
321
|
|
|
*/ |
322
|
|
|
public function count(): int |
323
|
|
|
{ |
324
|
|
|
$model = $this->model; |
325
|
|
|
$driver = $model::getDriver(); |
326
|
|
|
|
327
|
|
|
return $driver->count($this); |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Gets the sum of a property matching the query. |
332
|
|
|
* |
333
|
|
|
* @return number |
334
|
|
|
*/ |
335
|
|
|
public function sum(string $property) |
336
|
|
|
{ |
337
|
|
|
$model = $this->model; |
338
|
|
|
$driver = $model::getDriver(); |
339
|
|
|
|
340
|
|
|
return $driver->sum($this, $property); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
/** |
344
|
|
|
* Gets the average of a property matching the query. |
345
|
|
|
* |
346
|
|
|
* @return number |
347
|
|
|
*/ |
348
|
|
|
public function average(string $property) |
349
|
|
|
{ |
350
|
|
|
$model = $this->model; |
351
|
|
|
$driver = $model::getDriver(); |
352
|
|
|
|
353
|
|
|
return $driver->average($this, $property); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Gets the max of a property matching the query. |
358
|
|
|
* |
359
|
|
|
* @return number |
360
|
|
|
*/ |
361
|
|
|
public function max(string $property) |
362
|
|
|
{ |
363
|
|
|
$model = $this->model; |
364
|
|
|
$driver = $model::getDriver(); |
365
|
|
|
|
366
|
|
|
return $driver->max($this, $property); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Gets the min of a property matching the query. |
371
|
|
|
* |
372
|
|
|
* @return number |
373
|
|
|
*/ |
374
|
|
|
public function min(string $property) |
375
|
|
|
{ |
376
|
|
|
$model = $this->model; |
377
|
|
|
$driver = $model::getDriver(); |
378
|
|
|
|
379
|
|
|
return $driver->min($this, $property); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Updates all of the models matched by this query. |
384
|
|
|
* |
385
|
|
|
* @todo should be optimized to be done in a single call to the data layer |
386
|
|
|
* |
387
|
|
|
* @param array $params key-value update parameters |
388
|
|
|
* |
389
|
|
|
* @return int # of models updated |
390
|
|
|
*/ |
391
|
|
|
public function set(array $params): int |
392
|
|
|
{ |
393
|
|
|
$n = 0; |
394
|
|
|
foreach ($this->all() as $model) { |
395
|
|
|
$model->set($params); |
396
|
|
|
++$n; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
return $n; |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Deletes all of the models matched by this query. |
404
|
|
|
* |
405
|
|
|
* @todo should be optimized to be done in a single call to the data layer |
406
|
|
|
* |
407
|
|
|
* @return int # of models deleted |
408
|
|
|
*/ |
409
|
|
|
public function delete(): int |
410
|
|
|
{ |
411
|
|
|
$n = 0; |
412
|
|
|
foreach ($this->all() as $model) { |
413
|
|
|
$model->delete(); |
414
|
|
|
++$n; |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
return $n; |
418
|
|
|
} |
419
|
|
|
} |
420
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.