|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NwLaravel\Repositories\Eloquent; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
6
|
|
|
use Prettus\Repository\Eloquent\BaseRepository; |
|
7
|
|
|
use NwLaravel\Repositories\RepositoryInterface; |
|
8
|
|
|
use NwLaravel\Repositories\Criterias\InputCriteria; |
|
9
|
|
|
use NwLaravel\Resultset\BuilderResultset; |
|
10
|
|
|
use Prettus\Validator\Contracts\ValidatorInterface; |
|
11
|
|
|
use Prettus\Repository\Events\RepositoryEntityCreated; |
|
12
|
|
|
use Prettus\Repository\Events\RepositoryEntityUpdated; |
|
13
|
|
|
use Prettus\Repository\Events\RepositoryEntityDeleted; |
|
14
|
|
|
use Illuminate\Database\Query\Expression; |
|
15
|
|
|
use Illuminate\Database\Query\Grammars; |
|
16
|
|
|
use BadMethodCallException; |
|
17
|
|
|
use RuntimeException; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class AbstractRepository |
|
21
|
|
|
* |
|
22
|
|
|
* @abstract |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract class AbstractRepository extends BaseRepository implements RepositoryInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $orderBy; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var bool |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $skipPresenter = true; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return Model |
|
38
|
|
|
* @throws RepositoryException |
|
39
|
|
|
*/ |
|
40
|
29 |
|
public function makeModel() |
|
41
|
|
|
{ |
|
42
|
29 |
|
parent::makeModel(); |
|
43
|
29 |
|
return $this->model = $this->model->newQuery(); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Reset Model |
|
48
|
|
|
* |
|
49
|
|
|
* @return AbstractRepository |
|
50
|
|
|
* @throws RepositoryException |
|
51
|
|
|
*/ |
|
52
|
9 |
|
public function resetModel() |
|
53
|
|
|
{ |
|
54
|
9 |
|
parent::resetModel(); |
|
55
|
9 |
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get Query |
|
60
|
|
|
* |
|
61
|
|
|
* @return Builder |
|
62
|
|
|
*/ |
|
63
|
2 |
|
public function getQuery() |
|
64
|
|
|
{ |
|
65
|
2 |
|
$this->applyCriteria(); |
|
66
|
2 |
|
$this->applyScope(); |
|
67
|
|
|
|
|
68
|
2 |
|
return $this->model; |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Search All |
|
73
|
|
|
* |
|
74
|
|
|
* @param array $input Array Imput |
|
75
|
|
|
* @param string $orderBy String Order By |
|
76
|
|
|
* @param int $limit Integer Limit |
|
77
|
|
|
* @param bool $skipPresenter Boolean Skip Presenter |
|
78
|
|
|
* |
|
79
|
|
|
* @return BuilderResultset |
|
80
|
|
|
*/ |
|
81
|
1 |
|
public function searchAll(array $input, $orderBy = '', $limit = null, $skipPresenter = true) |
|
82
|
|
|
{ |
|
83
|
1 |
|
$orderBy = $orderBy?:$this->orderBy; |
|
84
|
|
|
|
|
85
|
1 |
|
$query = $this |
|
86
|
1 |
|
->whereInputCriteria($input) |
|
87
|
1 |
|
->orderBy($orderBy) |
|
88
|
1 |
|
->skipPresenter($skipPresenter) |
|
89
|
1 |
|
->getQuery() |
|
90
|
1 |
|
->limit($limit); |
|
91
|
|
|
|
|
92
|
1 |
|
$this->resetModel(); |
|
93
|
1 |
|
return new BuilderResultset($query); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Search Paginator |
|
98
|
|
|
* |
|
99
|
|
|
* @param array $input Array Input |
|
100
|
|
|
* @param string $orderBy String Order By |
|
101
|
|
|
* @param int|null $limit Integer Limit |
|
102
|
|
|
* @param bool $skipPresenter Boolean Skip Presenter |
|
103
|
|
|
* |
|
104
|
|
|
* @return Paginator |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function search(array $input, $orderBy = '', $limit = null, $skipPresenter = true) |
|
107
|
|
|
{ |
|
108
|
1 |
|
$orderBy = $orderBy?:$this->orderBy; |
|
109
|
|
|
|
|
110
|
1 |
|
return $this |
|
111
|
1 |
|
->whereInputCriteria($input) |
|
112
|
1 |
|
->orderBy($orderBy) |
|
113
|
1 |
|
->skipPresenter($skipPresenter) |
|
114
|
1 |
|
->paginate($limit); |
|
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* ResultSet |
|
119
|
|
|
* |
|
120
|
|
|
* @param int $limit Integer Limit |
|
121
|
|
|
* |
|
122
|
|
|
* @return BuilderResultset |
|
123
|
|
|
*/ |
|
124
|
|
|
public function resultset($limit = null) |
|
125
|
|
|
{ |
|
126
|
|
|
$query = $this->getQuery()->limit($limit); |
|
127
|
|
|
|
|
128
|
|
|
$this->resetModel(); |
|
129
|
|
|
return new BuilderResultset($query); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Get an array with the values of a given column. |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $column String Column |
|
136
|
|
|
* @param string $key String Key |
|
137
|
|
|
* |
|
138
|
|
|
* @return \Illuminate\Support\Collection |
|
139
|
|
|
*/ |
|
140
|
1 |
|
public function pluck($column, $key = null) |
|
141
|
|
|
{ |
|
142
|
1 |
|
$this->applyCriteria(); |
|
143
|
1 |
|
$this->applyScope(); |
|
144
|
|
|
|
|
145
|
1 |
|
$lists = $this->model->pluck($column, $key); |
|
146
|
|
|
|
|
147
|
1 |
|
$this->resetModel(); |
|
148
|
1 |
|
return $lists; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Add an "order by" clause to the query. |
|
153
|
|
|
* |
|
154
|
|
|
* @param string $columns String Columns |
|
155
|
|
|
* @param string $direction String Direction |
|
156
|
|
|
* |
|
157
|
|
|
* @return RepositoryInterface |
|
158
|
|
|
*/ |
|
159
|
3 |
|
public function orderBy($columns, $direction = 'asc') |
|
160
|
|
|
{ |
|
161
|
3 |
|
if (!empty($columns)) { |
|
162
|
3 |
|
$columns = explode(',', $columns); |
|
163
|
3 |
|
foreach ($columns as $key => $column) { |
|
164
|
3 |
|
$column = explode(' ', $column); |
|
165
|
3 |
|
$column = array_filter($column); |
|
166
|
3 |
|
$column = array_pad($column, 2, ''); |
|
167
|
3 |
|
list($field, $sort) = array_values($column); |
|
168
|
3 |
|
if (!empty($sort)) { |
|
169
|
2 |
|
$direction = $sort; |
|
170
|
2 |
|
} |
|
171
|
3 |
|
$direction = strtoupper($direction); |
|
172
|
3 |
|
$direction = in_array($direction, ['ASC', 'DESC']) ? $direction : 'ASC'; |
|
173
|
3 |
|
$this->model = $this->model->orderBy($field, $direction); |
|
174
|
3 |
|
} |
|
175
|
3 |
|
} |
|
176
|
|
|
|
|
177
|
3 |
|
return $this; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Random |
|
182
|
|
|
* |
|
183
|
|
|
* @return RepositoryInterface |
|
184
|
|
|
*/ |
|
185
|
4 |
|
public function random() |
|
186
|
|
|
{ |
|
187
|
4 |
|
$grammar = $this->model->getConnection()->getQueryGrammar(); |
|
188
|
|
|
|
|
189
|
4 |
|
switch (true) { |
|
190
|
4 |
|
case $grammar instanceof Grammars\MySqlGrammar: |
|
191
|
4 |
|
case $grammar instanceof Grammars\SqlServerGrammar: |
|
192
|
2 |
|
$random = 'RAND()'; |
|
193
|
2 |
|
break; |
|
194
|
2 |
|
case $grammar instanceof Grammars\PostgresGrammar: |
|
195
|
2 |
|
case $grammar instanceof Grammars\SQLiteGrammar: |
|
196
|
2 |
|
$random = 'RANDOM()'; |
|
197
|
2 |
|
} |
|
198
|
|
|
|
|
199
|
4 |
|
$this->model = $this->model->orderBy(new Expression($random)); |
|
|
|
|
|
|
200
|
|
|
|
|
201
|
4 |
|
return $this; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Count |
|
206
|
|
|
* |
|
207
|
|
|
* @param array $input Array Input |
|
208
|
|
|
* |
|
209
|
|
|
* @return int |
|
210
|
|
|
*/ |
|
211
|
1 |
|
public function count(array $input = array()) |
|
212
|
|
|
{ |
|
213
|
1 |
|
$this->applyCriteria(); |
|
214
|
1 |
|
$this->applyScope(); |
|
215
|
|
|
|
|
216
|
1 |
|
$this->whereInputCriteria($input); |
|
217
|
|
|
|
|
218
|
1 |
|
$count = $this->model->count(); |
|
219
|
|
|
|
|
220
|
1 |
|
$this->resetModel(); |
|
221
|
1 |
|
return $count; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Max |
|
226
|
|
|
* |
|
227
|
|
|
* @param mixed $field Mixed Field |
|
228
|
|
|
* @param array $input Array Input |
|
229
|
|
|
* |
|
230
|
|
|
* @return mixed |
|
231
|
|
|
*/ |
|
232
|
1 |
|
public function max($field, array $input = array()) |
|
233
|
|
|
{ |
|
234
|
1 |
|
$this->applyCriteria(); |
|
235
|
1 |
|
$this->applyScope(); |
|
236
|
|
|
|
|
237
|
1 |
|
$this->whereInputCriteria($input); |
|
238
|
|
|
|
|
239
|
1 |
|
$max = $this->model->max($field); |
|
240
|
|
|
|
|
241
|
1 |
|
$this->resetModel(); |
|
242
|
1 |
|
return $max; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Min |
|
247
|
|
|
* |
|
248
|
|
|
* @param mixed $field Mixed Field |
|
249
|
|
|
* @param array $input Array Input |
|
250
|
|
|
* |
|
251
|
|
|
* @return mixed |
|
252
|
|
|
*/ |
|
253
|
1 |
|
public function min($field, array $input = array()) |
|
254
|
|
|
{ |
|
255
|
1 |
|
$this->applyCriteria(); |
|
256
|
1 |
|
$this->applyScope(); |
|
257
|
|
|
|
|
258
|
1 |
|
$this->whereInputCriteria($input); |
|
259
|
|
|
|
|
260
|
1 |
|
$max = $this->model->min($field); |
|
261
|
|
|
|
|
262
|
1 |
|
$this->resetModel(); |
|
263
|
1 |
|
return $max; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* Sum |
|
268
|
|
|
* |
|
269
|
|
|
* @param mixed $field Mixed Field |
|
270
|
|
|
* @param array $input Array Input |
|
271
|
|
|
* |
|
272
|
|
|
* @return float |
|
273
|
|
|
*/ |
|
274
|
1 |
|
public function sum($field, array $input = array()) |
|
275
|
|
|
{ |
|
276
|
1 |
|
$this->applyCriteria(); |
|
277
|
1 |
|
$this->applyScope(); |
|
278
|
|
|
|
|
279
|
1 |
|
$this->whereInputCriteria($input); |
|
280
|
|
|
|
|
281
|
1 |
|
$max = $this->model->sum($field); |
|
282
|
|
|
|
|
283
|
1 |
|
$this->resetModel(); |
|
284
|
1 |
|
return $max; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Average |
|
289
|
|
|
* |
|
290
|
|
|
* @param mixed $field Mixed Field |
|
291
|
|
|
* @param array $input Array Input |
|
292
|
|
|
* |
|
293
|
|
|
* @return int |
|
294
|
|
|
*/ |
|
295
|
1 |
|
public function avg($field, array $input = array()) |
|
296
|
|
|
{ |
|
297
|
1 |
|
$this->applyCriteria(); |
|
298
|
1 |
|
$this->applyScope(); |
|
299
|
|
|
|
|
300
|
1 |
|
$this->whereInputCriteria($input); |
|
301
|
|
|
|
|
302
|
1 |
|
$avg = $this->model->avg($field); |
|
303
|
|
|
|
|
304
|
1 |
|
$this->resetModel(); |
|
305
|
1 |
|
return $avg; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* Order Up |
|
310
|
|
|
* |
|
311
|
|
|
* @param Model $model |
|
312
|
|
|
* @param string $field Field Order |
|
313
|
|
|
* @param array $input Array Where |
|
314
|
|
|
* |
|
315
|
|
|
* @return boolean |
|
316
|
|
|
*/ |
|
317
|
|
|
public function orderUp($model, $field, array $input = []) |
|
318
|
|
|
{ |
|
319
|
|
|
$input["{$field} <= ?"] = $model->{$field}; |
|
320
|
|
|
$input["id != ?"] = $model->id; |
|
321
|
|
|
return $this->reorder($model, $field, $input, 'DESC'); |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* Order Top |
|
326
|
|
|
* |
|
327
|
|
|
* @param Model $model |
|
328
|
|
|
* @param string $field Field Order |
|
329
|
|
|
* @param array $input Array Where |
|
330
|
|
|
* |
|
331
|
|
|
* @return boolean |
|
332
|
|
|
*/ |
|
333
|
|
|
public function orderTop($model, $field, array $input = []) |
|
334
|
|
|
{ |
|
335
|
|
|
return $this->reorder($model, $field, $input, 'ASC'); |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* Order Down |
|
340
|
|
|
* |
|
341
|
|
|
* @param Model $model |
|
342
|
|
|
* @param string $field Field Order |
|
343
|
|
|
* @param array $input Array Where |
|
344
|
|
|
* |
|
345
|
|
|
* @return boolean |
|
346
|
|
|
*/ |
|
347
|
|
|
public function orderDown($model, $field, array $input = []) |
|
348
|
|
|
{ |
|
349
|
|
|
$input["{$field} >= ?"] = $model->{$field}; |
|
350
|
|
|
$input["id != ?"] = $model->id; |
|
351
|
|
|
return $this->reorder($model, $field, $input, 'ASC'); |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
/** |
|
355
|
|
|
* Order Bottom |
|
356
|
|
|
* |
|
357
|
|
|
* @param Model $model |
|
358
|
|
|
* @param string $field Field Order |
|
359
|
|
|
* @param array $input Array Where |
|
360
|
|
|
* |
|
361
|
|
|
* @return boolean |
|
362
|
|
|
*/ |
|
363
|
|
|
public function orderBottom($model, $field, array $input = []) |
|
364
|
|
|
{ |
|
365
|
|
|
return $this->reorder($model, $field, $input, 'DESC'); |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
/** |
|
369
|
|
|
* Reorder |
|
370
|
|
|
* |
|
371
|
|
|
* @param Model $model |
|
372
|
|
|
* @param string $field Field Order |
|
373
|
|
|
* @param array $input Array Where |
|
374
|
|
|
* @param string $sort Sort |
|
375
|
|
|
* |
|
376
|
|
|
* @return boolean |
|
377
|
|
|
*/ |
|
378
|
|
|
protected function reorder($model, $field, array $input, $sort) |
|
379
|
|
|
{ |
|
380
|
|
|
if (!$model->exists) { |
|
381
|
|
|
return false; |
|
382
|
|
|
} |
|
383
|
|
|
|
|
384
|
|
|
$order = $model->{$field}; |
|
385
|
|
|
|
|
386
|
|
|
$anterior = $this->whereInputCriteria($input)->orderBy($field, $sort)->first(); |
|
387
|
|
|
|
|
388
|
|
|
if ($anterior) { |
|
389
|
|
|
$model->{$field} = $anterior->{$field}; |
|
390
|
|
|
$model->save(); |
|
391
|
|
|
|
|
392
|
|
|
$anterior->{$field} = $order; |
|
393
|
|
|
$anterior->save(); |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
event(new RepositoryEntityUpdated($this, $model)); |
|
397
|
|
|
|
|
398
|
|
|
return true; |
|
399
|
|
|
} |
|
400
|
|
|
|
|
401
|
|
|
/** |
|
402
|
|
|
* Where InputCriteria |
|
403
|
|
|
* |
|
404
|
|
|
* @param array $input Array Input |
|
405
|
|
|
* |
|
406
|
|
|
* @return RepositoryInterface |
|
407
|
|
|
*/ |
|
408
|
7 |
|
public function whereInputCriteria(array $input = array()) |
|
409
|
|
|
{ |
|
410
|
7 |
|
if (count($input)) { |
|
411
|
6 |
|
$criteria = new InputCriteria($input); |
|
412
|
6 |
|
$this->model = $criteria->apply($this->model, $this); |
|
|
|
|
|
|
413
|
6 |
|
} |
|
414
|
|
|
|
|
415
|
7 |
|
return $this; |
|
416
|
|
|
} |
|
417
|
|
|
|
|
418
|
|
|
/** |
|
419
|
|
|
* Validar |
|
420
|
|
|
* |
|
421
|
|
|
* @param array $attributes |
|
422
|
|
|
* @param string $action |
|
423
|
|
|
* @param string $id |
|
424
|
|
|
* |
|
425
|
|
|
* @return bool |
|
426
|
|
|
*/ |
|
427
|
2 |
|
public function validar(array $attributes, $action, $id = null) |
|
428
|
|
|
{ |
|
429
|
2 |
|
$return = false; |
|
430
|
|
|
|
|
431
|
2 |
|
if (!is_null($this->validator)) { |
|
432
|
|
|
// we should pass data that has been casts by the model |
|
433
|
|
|
// to make sure data type are same because validator may need to use |
|
434
|
|
|
// this data to compare with data that fetch from database. |
|
435
|
2 |
|
$model = $this->model->newModelInstance()->fill($attributes); |
|
436
|
|
|
$attributes = array_merge($attributes, $model->toArray()); |
|
437
|
|
|
|
|
438
|
|
|
$validator = $this->validator->with($attributes); |
|
439
|
|
|
|
|
440
|
|
|
if ($id) { |
|
|
|
|
|
|
441
|
|
|
$validator->setId($id); |
|
442
|
|
|
} |
|
443
|
|
|
|
|
444
|
|
|
$return = $validator->passesOrFail($action); |
|
445
|
|
|
} |
|
446
|
|
|
|
|
447
|
|
|
return $return; |
|
448
|
|
|
} |
|
449
|
|
|
|
|
450
|
|
|
/** |
|
451
|
|
|
* Save a new model in repository |
|
452
|
|
|
* |
|
453
|
|
|
* @throws ValidatorException |
|
454
|
|
|
* @param array $attributes Array Attributes |
|
455
|
|
|
* @return mixed |
|
456
|
|
|
*/ |
|
457
|
1 |
|
public function create(array $attributes) |
|
458
|
|
|
{ |
|
459
|
1 |
|
$this->validar($attributes, ValidatorInterface::RULE_CREATE); |
|
460
|
|
|
|
|
461
|
|
|
$model = $this->model->newModelInstance($attributes); |
|
462
|
|
|
$model->save(); |
|
463
|
|
|
$this->resetModel(); |
|
464
|
|
|
|
|
465
|
|
|
event(new RepositoryEntityCreated($this, $model)); |
|
466
|
|
|
|
|
467
|
|
|
return $this->parserResult($model); |
|
468
|
|
|
} |
|
469
|
|
|
|
|
470
|
|
|
/** |
|
471
|
|
|
* Update a model in repository by id |
|
472
|
|
|
* |
|
473
|
|
|
* @throws ValidatorException |
|
474
|
|
|
* @param array $attributes Array Attributes |
|
475
|
|
|
* @param int $id Integer Id |
|
476
|
|
|
* @return mixed |
|
477
|
|
|
*/ |
|
478
|
1 |
|
public function update(array $attributes, $id) |
|
479
|
|
|
{ |
|
480
|
1 |
|
$this->applyScope(); |
|
481
|
|
|
|
|
482
|
1 |
|
$this->validar($attributes, ValidatorInterface::RULE_UPDATE, $id); |
|
483
|
|
|
|
|
484
|
|
|
$temporarySkipPresenter = $this->skipPresenter; |
|
485
|
|
|
|
|
486
|
|
|
$this->skipPresenter(true); |
|
487
|
|
|
|
|
488
|
|
|
$model = $this->model->findOrFail($id); |
|
489
|
|
|
$model->fill($attributes); |
|
490
|
|
|
$model->save(); |
|
491
|
|
|
|
|
492
|
|
|
$this->skipPresenter($temporarySkipPresenter); |
|
493
|
|
|
$this->resetModel(); |
|
494
|
|
|
|
|
495
|
|
|
event(new RepositoryEntityUpdated($this, $model)); |
|
496
|
|
|
|
|
497
|
|
|
return $this->parserResult($model); |
|
498
|
|
|
} |
|
499
|
|
|
|
|
500
|
|
|
/** |
|
501
|
|
|
* Delete multiple entities by given criteria. |
|
502
|
|
|
* |
|
503
|
|
|
* @param array $where |
|
504
|
|
|
* |
|
505
|
|
|
* @return boolean|null |
|
506
|
|
|
*/ |
|
507
|
1 |
|
public function deleteWhere(array $where) |
|
508
|
|
|
{ |
|
509
|
1 |
|
$this->applyCriteria(); |
|
510
|
1 |
|
$this->applyScope(); |
|
511
|
|
|
|
|
512
|
1 |
|
$temporarySkipPresenter = $this->skipPresenter; |
|
513
|
1 |
|
$this->skipPresenter(true); |
|
514
|
|
|
|
|
515
|
1 |
|
$this->whereInputCriteria($where); |
|
516
|
|
|
|
|
517
|
1 |
|
$deleted = $this->model->delete(); |
|
518
|
|
|
|
|
519
|
1 |
|
$model = $this->model instanceof Builder ? $this->model->getModel() : $this->model; |
|
520
|
1 |
|
event(new RepositoryEntityDeleted($this, $model)); |
|
521
|
|
|
|
|
522
|
1 |
|
$this->skipPresenter($temporarySkipPresenter); |
|
523
|
1 |
|
$this->resetModel(); |
|
524
|
|
|
|
|
525
|
1 |
|
return $deleted; |
|
526
|
|
|
} |
|
527
|
|
|
|
|
528
|
|
|
/** |
|
529
|
|
|
* Update multiple entities by given criteria. |
|
530
|
|
|
* |
|
531
|
|
|
* @param array $where |
|
532
|
|
|
* |
|
533
|
|
|
* @return boolean|null |
|
534
|
|
|
*/ |
|
535
|
1 |
|
public function updateWhere(array $attributes, array $where) |
|
536
|
|
|
{ |
|
537
|
1 |
|
$this->applyCriteria(); |
|
538
|
1 |
|
$this->applyScope(); |
|
539
|
|
|
|
|
540
|
1 |
|
$temporarySkipPresenter = $this->skipPresenter; |
|
541
|
1 |
|
$this->skipPresenter(true); |
|
542
|
|
|
|
|
543
|
1 |
|
$this->whereInputCriteria($where); |
|
544
|
|
|
|
|
545
|
1 |
|
$updated = $this->model->update($attributes); |
|
546
|
|
|
|
|
547
|
1 |
|
$this->skipPresenter($temporarySkipPresenter); |
|
548
|
1 |
|
$this->resetModel(); |
|
549
|
|
|
|
|
550
|
1 |
|
$model = $this->model instanceof Builder ? $this->model->getModel() : $this->model; |
|
551
|
1 |
|
event(new RepositoryEntityUpdated($this, $model)); |
|
552
|
|
|
|
|
553
|
1 |
|
return $updated; |
|
554
|
|
|
} |
|
555
|
|
|
|
|
556
|
|
|
/** |
|
557
|
|
|
* Handle dynamic method calls into the method. |
|
558
|
|
|
* |
|
559
|
|
|
* @param string $method |
|
560
|
|
|
* @param array $parameters |
|
561
|
|
|
* |
|
562
|
|
|
* @return AbstractRepository |
|
563
|
|
|
* |
|
564
|
|
|
* @throws BadMethodCallException |
|
565
|
|
|
*/ |
|
566
|
7 |
|
public function __call($method, $parameters) |
|
567
|
|
|
{ |
|
568
|
7 |
|
$pattern = '/^(((where|orWhere).*)|select|limit|groupBy|join|leftJoin|rightJoin|crossJoin)$/'; |
|
569
|
7 |
|
if (preg_match($pattern, $method)) { |
|
570
|
6 |
|
$this->model = call_user_func_array([$this->model, $method], $parameters); |
|
571
|
6 |
|
return $this; |
|
572
|
|
|
} |
|
573
|
|
|
|
|
574
|
1 |
|
$pattern = '/^(toSql|getBindings)$/'; |
|
575
|
1 |
|
if (preg_match($pattern, $method)) { |
|
576
|
|
|
return call_user_func_array([$this->model, $method], $parameters); |
|
577
|
|
|
} |
|
578
|
|
|
|
|
579
|
1 |
|
$className = static::class; |
|
580
|
1 |
|
throw new BadMethodCallException("Call to undefined method {$className}::{$method}()"); |
|
581
|
|
|
} |
|
582
|
|
|
} |
|
583
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..