1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\Repository; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Recca0120\Repository\Contracts\EloquentRepository as EloquentRepositoryContract; |
7
|
|
|
|
8
|
|
|
abstract class EloquentRepository implements EloquentRepositoryContract |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* $model. |
12
|
|
|
* |
13
|
|
|
* @var \Illuminate\Database\Eloquent\Model |
14
|
|
|
*/ |
15
|
|
|
protected $model; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* __construct. |
19
|
|
|
* |
20
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
21
|
|
|
*/ |
22
|
30 |
|
public function __construct(Model $model) |
23
|
|
|
{ |
24
|
30 |
|
$this->model = $model; |
25
|
30 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Find a model by its primary key. |
29
|
|
|
* |
30
|
|
|
* @param mixed $id |
31
|
|
|
* @param array $columns |
32
|
|
|
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|static[]|static|null |
33
|
|
|
*/ |
34
|
4 |
|
public function find($id, $columns = ['*']) |
35
|
|
|
{ |
36
|
4 |
|
return $this->newQuery()->find($id, $columns); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Find multiple models by their primary keys. |
41
|
|
|
* |
42
|
|
|
* @param \Illuminate\Contracts\Support\Arrayable|array $ids |
43
|
|
|
* @param array $columns |
44
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
45
|
|
|
*/ |
46
|
1 |
|
public function findMany($ids, $columns = ['*']) |
47
|
|
|
{ |
48
|
1 |
|
return $this->newQuery()->findMany($ids, $columns); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Find a model by its primary key or throw an exception. |
53
|
|
|
* |
54
|
|
|
* @param mixed $id |
55
|
|
|
* @param array $columns |
56
|
|
|
* @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection |
57
|
|
|
* |
58
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
59
|
|
|
*/ |
60
|
4 |
|
public function findOrFail($id, $columns = ['*']) |
61
|
|
|
{ |
62
|
4 |
|
return $this->newQuery()->findOrFail($id, $columns); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Find a model by its primary key or return fresh model instance. |
67
|
|
|
* |
68
|
|
|
* @param mixed $id |
69
|
|
|
* @param array $columns |
70
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
71
|
|
|
*/ |
72
|
1 |
|
public function findOrNew($id, $columns = ['*']) |
73
|
|
|
{ |
74
|
1 |
|
return $this->newQuery()->findOrNew($id, $columns); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get the first record matching the attributes or instantiate it. |
79
|
|
|
* |
80
|
|
|
* @param array $attributes |
81
|
|
|
* @param array $values |
82
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
83
|
|
|
*/ |
84
|
1 |
|
public function firstOrNew(array $attributes, array $values = []) |
85
|
|
|
{ |
86
|
1 |
|
return $this->newQuery()->firstOrNew($attributes, $values); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get the first record matching the attributes or create it. |
91
|
|
|
* |
92
|
|
|
* @param array $attributes |
93
|
|
|
* @param array $values |
94
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
95
|
|
|
*/ |
96
|
1 |
|
public function firstOrCreate(array $attributes, array $values = []) |
97
|
|
|
{ |
98
|
1 |
|
return $this->newQuery()->firstOrCreate($attributes, $values); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Create or update a record matching the attributes, and fill it with values. |
103
|
|
|
* |
104
|
|
|
* @param array $attributes |
105
|
|
|
* @param array $values |
106
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
107
|
|
|
*/ |
108
|
1 |
|
public function updateOrCreate(array $attributes, array $values = []) |
109
|
|
|
{ |
110
|
1 |
|
return $this->newQuery()->updateOrCreate($attributes, $values); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Execute the query and get the first result or throw an exception. |
115
|
|
|
* |
116
|
|
|
* @param array $columns |
117
|
|
|
* @param \Recca0120\Repository\Criteria[] $criteria |
118
|
|
|
* @return \Illuminate\Database\Eloquent\Model|static |
119
|
|
|
* |
120
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
121
|
|
|
*/ |
122
|
1 |
|
public function firstOrFail($criteria = [], $columns = ['*']) |
123
|
|
|
{ |
124
|
1 |
|
return $this->matching($criteria)->firstOrFail($columns); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* create. |
129
|
|
|
* |
130
|
|
|
* @param array $attributes |
131
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
132
|
|
|
* |
133
|
|
|
* @throws \Throwable |
134
|
|
|
*/ |
135
|
1 |
|
public function create($attributes) |
136
|
|
|
{ |
137
|
1 |
|
return $this->newQuery()->create($attributes); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Save a new model and return the instance. |
142
|
|
|
* |
143
|
|
|
* @param array $attributes |
144
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
145
|
|
|
* |
146
|
|
|
* @throws \Throwable |
147
|
|
|
*/ |
148
|
1 |
|
public function forceCreate($attributes) |
149
|
|
|
{ |
150
|
1 |
|
return $this->newQuery()->forceCreate($attributes); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* update. |
155
|
|
|
* |
156
|
|
|
* @param array $attributes |
157
|
|
|
* @param mixed $id |
158
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
159
|
|
|
* |
160
|
|
|
* @throws \Throwable |
161
|
|
|
*/ |
162
|
|
|
public function update($id, $attributes) |
163
|
|
|
{ |
164
|
1 |
|
return tap($this->findOrFail($id), function ($instance) use ($attributes) { |
165
|
1 |
|
$instance->fill($attributes)->saveOrFail(); |
166
|
1 |
|
}); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* forceCreate. |
171
|
|
|
* |
172
|
|
|
* @param array $attributes |
173
|
|
|
* @param mixed $id |
174
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
175
|
|
|
* |
176
|
|
|
* @throws \Throwable |
177
|
|
|
*/ |
178
|
|
|
public function forceUpdate($id, $attributes) |
179
|
|
|
{ |
180
|
1 |
|
return tap($this->findOrFail($id), function ($instance) use ($attributes) { |
181
|
1 |
|
$instance->forceFill($attributes)->saveOrFail(); |
182
|
1 |
|
}); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* delete. |
187
|
|
|
* |
188
|
|
|
* @param mixed $id |
189
|
|
|
*/ |
190
|
2 |
|
public function delete($id) |
191
|
|
|
{ |
192
|
2 |
|
return $this->find($id)->delete(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Restore a soft-deleted model instance. |
197
|
|
|
* |
198
|
|
|
* @param mixed $id |
199
|
|
|
* @return bool|null |
200
|
|
|
*/ |
201
|
1 |
|
public function restore($id) |
202
|
|
|
{ |
203
|
1 |
|
return $this->newQuery()->restore($id); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Force a hard delete on a soft deleted model. |
208
|
|
|
* |
209
|
|
|
* This method protects developers from running forceDelete when trait is missing. |
210
|
|
|
* |
211
|
|
|
* @param mixed $id |
212
|
|
|
* @return bool|null |
213
|
|
|
*/ |
214
|
1 |
|
public function forceDelete($id) |
215
|
|
|
{ |
216
|
1 |
|
return $this->findOrFail($id)->forceDelete(); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Create a new model instance that is existing. |
221
|
|
|
* |
222
|
|
|
* @param array $attributes |
223
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
224
|
|
|
*/ |
225
|
1 |
|
public function newInstance($attributes = [], $exists = false) |
226
|
|
|
{ |
227
|
1 |
|
return $this->getModel()->newInstance($attributes, $exists); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Execute the query as a "select" statement. |
232
|
|
|
* |
233
|
|
|
* @param \Recca0120\Repository\Criteria[] $criteria |
234
|
|
|
* @param array $columns |
235
|
|
|
* @return \Illuminate\Support\Collection |
236
|
|
|
*/ |
237
|
2 |
|
public function get($criteria = [], $columns = ['*']) |
238
|
|
|
{ |
239
|
2 |
|
return $this->matching($criteria)->get($columns); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Chunk the results of the query. |
244
|
|
|
* |
245
|
|
|
* @param \Recca0120\Repository\Criteria[] $criteria |
246
|
|
|
* @param int $count |
247
|
|
|
* @param callable $callback |
248
|
|
|
* @return bool |
249
|
|
|
*/ |
250
|
1 |
|
public function chunk($criteria, $count, callable $callback) |
251
|
|
|
{ |
252
|
1 |
|
return $this->matching($criteria)->chunk($count, $callback); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Execute a callback over each item while chunking. |
257
|
|
|
* |
258
|
|
|
* @param \Recca0120\Repository\Criteria[] $criteria |
259
|
|
|
* @param callable $callback |
260
|
|
|
* @param int $count |
261
|
|
|
* @return bool |
262
|
|
|
*/ |
263
|
1 |
|
public function each($criteria, callable $callback, $count = 1000) |
264
|
|
|
{ |
265
|
1 |
|
return $this->matching($criteria)->each($callback, $count); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Execute the query and get the first result. |
270
|
|
|
* |
271
|
|
|
* @param \Recca0120\Repository\Criteria[] $criteria |
272
|
|
|
* @param array $columns |
273
|
|
|
* @return \Illuminate\Database\Eloquent\Model|static|null |
274
|
|
|
*/ |
275
|
1 |
|
public function first($criteria = [], $columns = ['*']) |
276
|
|
|
{ |
277
|
1 |
|
return $this->matching($criteria)->first($columns); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Paginate the given query. |
282
|
|
|
* |
283
|
|
|
* @param \Recca0120\Repository\Criteria[] $criteria |
284
|
|
|
* @param int $perPage |
285
|
|
|
* @param array $columns |
286
|
|
|
* @param string $pageName |
287
|
|
|
* @param int|null $page |
288
|
|
|
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator |
289
|
|
|
* |
290
|
|
|
* @throws \InvalidArgumentException |
291
|
|
|
*/ |
292
|
1 |
|
public function paginate($criteria = [], $perPage = null, $columns = ['*'], $pageName = 'page', $page = null) |
293
|
|
|
{ |
294
|
1 |
|
return $this->matching($criteria)->paginate($perPage, $columns, $pageName, $page); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Paginate the given query into a simple paginator. |
299
|
|
|
* |
300
|
|
|
* @param \Recca0120\Repository\Criteria[] $criteria |
301
|
|
|
* @param int $perPage |
302
|
|
|
* @param array $columns |
303
|
|
|
* @param string $pageName |
304
|
|
|
* @param int|null $page |
305
|
|
|
* @return \Illuminate\Contracts\Pagination\Paginator |
306
|
|
|
*/ |
307
|
1 |
|
public function simplePaginate($criteria = [], $perPage = null, $columns = ['*'], $pageName = 'page', $page = null) |
308
|
|
|
{ |
309
|
1 |
|
return $this->matching($criteria)->simplePaginate($perPage, $columns, $pageName, $page); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Retrieve the "count" result of the query. |
314
|
|
|
* |
315
|
|
|
* @param \Recca0120\Repository\Criteria[] $criteria |
316
|
|
|
* @param string $columns |
317
|
|
|
* @return int |
318
|
|
|
*/ |
319
|
1 |
|
public function count($criteria = [], $columns = '*') |
320
|
|
|
{ |
321
|
1 |
|
return (int) $this->matching($criteria)->count($columns); |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* Retrieve the minimum value of a given column. |
326
|
|
|
* |
327
|
|
|
* @param \Recca0120\Repository\Criteria[] $criteria |
328
|
|
|
* @param string $column |
329
|
|
|
* @return mixed |
330
|
|
|
*/ |
331
|
1 |
|
public function min($criteria, $column) |
332
|
|
|
{ |
333
|
1 |
|
return $this->matching($criteria)->min($column); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Retrieve the maximum value of a given column. |
338
|
|
|
* |
339
|
|
|
* @param \Recca0120\Repository\Criteria[] $criteria |
340
|
|
|
* @param string $column |
341
|
|
|
* @return mixed |
342
|
|
|
*/ |
343
|
1 |
|
public function max($criteria, $column) |
344
|
|
|
{ |
345
|
1 |
|
return $this->matching($criteria)->max($column); |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Retrieve the sum of the values of a given column. |
350
|
|
|
* |
351
|
|
|
* @param \Recca0120\Repository\Criteria[] $criteria |
352
|
|
|
* @param string $column |
353
|
|
|
* @return mixed |
354
|
|
|
*/ |
355
|
1 |
|
public function sum($criteria, $column) |
356
|
|
|
{ |
357
|
1 |
|
$result = $this->matching($criteria)->sum($column); |
358
|
|
|
|
359
|
1 |
|
return $result ?: 0; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Retrieve the average of the values of a given column. |
364
|
|
|
* |
365
|
|
|
* @param \Recca0120\Repository\Criteria[] $criteria |
366
|
|
|
* @param string $column |
367
|
|
|
* @return mixed |
368
|
|
|
*/ |
369
|
2 |
|
public function avg($criteria, $column) |
370
|
|
|
{ |
371
|
2 |
|
return $this->matching($criteria)->avg($column); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Alias for the "avg" method. |
376
|
|
|
* |
377
|
|
|
* @param string $column |
378
|
|
|
* @return mixed |
379
|
|
|
*/ |
380
|
2 |
|
public function average($criteria, $column) |
381
|
|
|
{ |
382
|
2 |
|
return $this->avg($criteria, $column); |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
/** |
386
|
|
|
* matching. |
387
|
|
|
* |
388
|
|
|
* @param \Recca0120\Repository\Criteria[] $criteria |
389
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
390
|
|
|
*/ |
391
|
16 |
|
public function matching($criteria) |
392
|
|
|
{ |
393
|
16 |
|
$criteria = is_array($criteria) === false ? [$criteria] : $criteria; |
394
|
|
|
|
395
|
|
|
return array_reduce($criteria, function ($query, $criteria) { |
396
|
15 |
|
$criteria->each(function ($method) use ($query) { |
397
|
15 |
|
call_user_func_array([$query, $method->name], $method->parameters); |
398
|
15 |
|
}); |
399
|
|
|
|
400
|
15 |
|
return $query; |
401
|
16 |
|
}, $this->newQuery()); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* getQuery. |
406
|
|
|
* |
407
|
|
|
* @param \Recca0120\Repository\Criteria[] $criteria |
408
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
409
|
|
|
*/ |
410
|
1 |
|
public function getQuery($criteria = []) |
411
|
|
|
{ |
412
|
1 |
|
return $this->matching($criteria)->getQuery(); |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
/** |
416
|
|
|
* getModel. |
417
|
|
|
* |
418
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
419
|
|
|
*/ |
420
|
1 |
|
public function getModel() |
421
|
|
|
{ |
422
|
1 |
|
return $this->model instanceof Model |
423
|
1 |
|
? clone $this->model |
424
|
1 |
|
: $this->model->getModel(); |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* Get a new query builder for the model's table. |
429
|
|
|
* |
430
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
431
|
|
|
*/ |
432
|
30 |
|
public function newQuery() |
433
|
|
|
{ |
434
|
30 |
|
return $this->model instanceof Model |
435
|
29 |
|
? $this->model->newQuery() |
436
|
30 |
|
: clone $this->model; |
437
|
|
|
} |
438
|
|
|
} |
439
|
|
|
|