1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Hamza Alayed |
5
|
|
|
* Date: 11/29/18 |
6
|
|
|
* Time: 9:38 AM. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Shamaseen\Repository\Generator\Utility; |
10
|
|
|
|
11
|
|
|
use Exception; |
12
|
|
|
use Illuminate\Container\Container as App; |
13
|
|
|
use Illuminate\Contracts\Pagination\LengthAwarePaginator; |
14
|
|
|
use Illuminate\Contracts\Pagination\Paginator; |
15
|
|
|
use Illuminate\Database\Eloquent\Builder; |
16
|
|
|
use Illuminate\Database\Eloquent\Collection; |
17
|
|
|
use Illuminate\Database\Eloquent\Model; |
18
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Database. |
22
|
|
|
*/ |
23
|
|
|
abstract class AbstractRepository implements ContractInterface |
24
|
|
|
{ |
25
|
|
|
protected $with = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var App |
29
|
|
|
*/ |
30
|
|
|
protected $app; |
31
|
|
|
|
32
|
|
|
/** @var string */ |
33
|
|
|
protected $order = null; |
34
|
|
|
|
35
|
|
|
protected $direction = 'desc'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Entity |
39
|
|
|
*/ |
40
|
|
|
protected $model; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var bool |
44
|
|
|
*/ |
45
|
|
|
private $trash = false; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var bool |
49
|
|
|
*/ |
50
|
|
|
private $withTrash = false; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var bool |
54
|
|
|
*/ |
55
|
|
|
private $allowCaching = true; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
private $cache = []; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param App $app |
64
|
|
|
*/ |
65
|
|
|
public function __construct(App $app) |
66
|
|
|
{ |
67
|
|
|
$this->app = $app; |
68
|
|
|
$this->makeModel(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function makeModel() |
72
|
|
|
{ |
73
|
|
|
$this->model = $this->app->make($this->getModelClass()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
abstract protected function getModelClass(): string; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param int $limit |
83
|
|
|
* @param array $criteria |
84
|
|
|
* |
85
|
|
|
* @return Paginator |
86
|
|
|
*/ |
87
|
|
|
public function simplePaginate($limit = 10, $criteria = []) |
88
|
|
|
{ |
89
|
|
|
return $this->filter($criteria)->simplePaginate($limit); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return \Illuminate\Database\Query\Builder|Entity |
94
|
|
|
*/ |
95
|
|
|
public function builder() |
96
|
|
|
{ |
97
|
|
|
return $this->model->query(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param array $criteria |
102
|
|
|
* |
103
|
|
|
* @return Builder |
104
|
|
|
*/ |
105
|
|
|
public function filter($criteria = []) |
106
|
|
|
{ |
107
|
|
|
$criteria = $this->order($criteria); |
108
|
|
|
|
109
|
|
|
/** @var Entity $latest */ |
110
|
|
|
$latest = $this->model->with($this->with); |
111
|
|
|
if ('' != $this->order) { |
112
|
|
|
$latest->orderBy($this->order, $this->direction); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (isset($criteria['search'])) { |
116
|
|
|
foreach ($this->model->searchable as $method => $columns) { |
117
|
|
|
if (method_exists($this->model, $method)) { |
118
|
|
|
$latest->orWhereHas($method, function ($query) use ($criteria, $columns) { |
119
|
|
|
/* @var $query Builder */ |
120
|
|
|
$query->where(function ($query2) use ($criteria, $columns) { |
121
|
|
|
/* @var $query2 Builder */ |
122
|
|
|
foreach ((array) $columns as $column) { |
123
|
|
|
$query2->orWhere($column, 'like', '%'.$criteria['search'].'%'); |
124
|
|
|
} |
125
|
|
|
}); |
126
|
|
|
}); |
127
|
|
|
} else { |
128
|
|
|
$latest->orWhere($columns, 'like', '%'.$criteria['search'].'%'); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
unset($criteria['search']); |
133
|
|
|
|
134
|
|
|
if ($this->trash) { |
135
|
|
|
$latest->onlyTrashed(); |
136
|
|
|
} |
137
|
|
|
if ($this->withTrash) { |
138
|
|
|
$latest->withTrashed(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $latest->where($criteria); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* prepare order for query. |
146
|
|
|
* |
147
|
|
|
* @param array $criteria |
148
|
|
|
* |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
|
|
private function order($criteria = []) |
152
|
|
|
{ |
153
|
|
|
if (isset($criteria['order'])) { |
154
|
|
|
$this->order = $criteria['order']; |
155
|
|
|
unset($criteria['order']); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if (isset($criteria['direction'])) { |
159
|
|
|
$this->direction = $criteria['direction']; |
160
|
|
|
unset($criteria['direction']); |
161
|
|
|
} |
162
|
|
|
unset($criteria['page']); |
163
|
|
|
|
164
|
|
|
return $criteria; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param int $limit |
169
|
|
|
* @param array $criteria |
170
|
|
|
* |
171
|
|
|
* @return LengthAwarePaginator |
172
|
|
|
*/ |
173
|
|
|
public function paginate($limit = 10, $criteria = []) |
174
|
|
|
{ |
175
|
|
|
return $this->filter($criteria)->paginate($limit); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param array $criteria |
180
|
|
|
* |
181
|
|
|
* @return Builder[]|Collection |
182
|
|
|
*/ |
183
|
|
|
public function get($criteria = []) |
184
|
|
|
{ |
185
|
|
|
return $this->filter($criteria)->get(); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param $entityId |
190
|
|
|
* @param array $attributes |
191
|
|
|
* |
192
|
|
|
* @return bool|Collection|Model|Entity |
193
|
|
|
*/ |
194
|
|
|
public function update($entityId = 0, $attributes = []) |
195
|
|
|
{ |
196
|
|
|
$item = $this->model->findOrFail($entityId); |
197
|
|
|
|
198
|
|
|
if ($item->update($attributes)) { |
199
|
|
|
return $item; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return false; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param $entityId |
207
|
|
|
* |
208
|
|
|
* @throws Exception |
209
|
|
|
* |
210
|
|
|
* @return bool |
211
|
|
|
*/ |
212
|
|
|
public function delete($entityId = 0) |
213
|
|
|
{ |
214
|
|
|
$item = $this->model->findOrFail($entityId); |
215
|
|
|
|
216
|
|
|
return $item->delete(); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @param array $attributes |
221
|
|
|
* |
222
|
|
|
* @return bool |
223
|
|
|
*/ |
224
|
|
|
public function insert($attributes = []) |
225
|
|
|
{ |
226
|
|
|
return $this->model->insert($attributes); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @param array $columns |
231
|
|
|
* |
232
|
|
|
* @return Collection|static[] |
233
|
|
|
*/ |
234
|
|
|
public function all($columns = ['*']) |
235
|
|
|
{ |
236
|
|
|
return $this->model->all($columns); |
|
|
|
|
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @param string $name |
241
|
|
|
* @param string $entityId |
242
|
|
|
* @param array $criteria |
243
|
|
|
* |
244
|
|
|
* @return array |
245
|
|
|
*/ |
246
|
|
|
public function pluck($name = 'name', $entityId = 'id', $criteria = []) |
247
|
|
|
{ |
248
|
|
|
return $this->model->where($criteria)->pluck($name, $entityId)->toArray(); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @param $entityId |
253
|
|
|
* @param array $columns |
254
|
|
|
* |
255
|
|
|
* @return Entity|Model |
256
|
|
|
*/ |
257
|
|
|
public function find($entityId = 0, $columns = ['*']) |
258
|
|
|
{ |
259
|
|
|
if($this->allowCaching) |
260
|
|
|
{ |
261
|
|
|
if (isset($this->cache[$entityId])) |
262
|
|
|
return $this->cache[$entityId]; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
$entity = $this->model->with($this->with)->find($entityId, $columns); |
266
|
|
|
|
267
|
|
|
if($this->allowCaching) |
268
|
|
|
$this->cache[$entityId] = $entity; |
269
|
|
|
|
270
|
|
|
return $entity; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param $entityId |
275
|
|
|
* @param array $columns |
276
|
|
|
* |
277
|
|
|
*@throws ModelNotFoundException |
278
|
|
|
* |
279
|
|
|
* @return Entity|Model |
280
|
|
|
*/ |
281
|
|
|
public function findOrFail($entityId = 0, $columns = ['*']) |
282
|
|
|
{ |
283
|
|
|
if($this->allowCaching) |
284
|
|
|
{ |
285
|
|
|
if (isset($this->cache[$entityId])) |
286
|
|
|
return $this->cache[$entityId]; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
$entity = $this->model->with($this->with)->findOrFail($entityId, $columns); |
290
|
|
|
|
291
|
|
|
if($this->allowCaching) |
292
|
|
|
$this->cache[$entityId] = $entity; |
293
|
|
|
|
294
|
|
|
return $entity; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* @param array $filter |
299
|
|
|
* @param array $columns |
300
|
|
|
* |
301
|
|
|
* @return Entity|Model |
302
|
|
|
*/ |
303
|
|
|
public function first($filter = [], $columns = ['*']) |
304
|
|
|
{ |
305
|
|
|
if($this->allowCaching) |
306
|
|
|
{ |
307
|
|
|
if (isset($this->cache['first'])) |
308
|
|
|
return $this->cache['first']; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
$entity = $this->model->with($this->with)->select($columns)->where($filter)->first(); |
312
|
|
|
|
313
|
|
|
if($this->allowCaching) |
314
|
|
|
$this->cache['first'] = $entity; |
315
|
|
|
|
316
|
|
|
return $entity; |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* @param array $filter |
321
|
|
|
* @param array $columns |
322
|
|
|
* |
323
|
|
|
* @return Entity|Model |
324
|
|
|
*/ |
325
|
|
|
public function last($filter = [], $columns = ['*']) |
326
|
|
|
{ |
327
|
|
|
if($this->allowCaching) |
328
|
|
|
{ |
329
|
|
|
if (isset($this->cache['last'])) |
330
|
|
|
return $this->cache['last']; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
$entity = $this->model->with($this->with)->select($columns)->where($filter) |
334
|
|
|
->orderBy('id','desc')->first(); |
335
|
|
|
|
336
|
|
|
if($this->allowCaching) |
337
|
|
|
$this->cache['last'] = $entity; |
338
|
|
|
|
339
|
|
|
return $entity; |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @param $haystack |
344
|
|
|
* @param $needle |
345
|
|
|
* |
346
|
|
|
* @return Entity[]|Model[]|Collection |
347
|
|
|
*/ |
348
|
|
|
public function search($haystack, $needle) |
349
|
|
|
{ |
350
|
|
|
return $this->model->where($haystack, 'like', $needle)->get(); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* @param $criteria |
355
|
|
|
* @param array $columns |
356
|
|
|
* |
357
|
|
|
* @return Entity|Model |
358
|
|
|
*/ |
359
|
|
|
public function findBy($criteria = [], $columns = ['*']) |
360
|
|
|
{ |
361
|
|
|
return $this->model->with($this->with)->select($columns)->where($criteria)->first(); |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
/** |
365
|
|
|
* @param array $attributes |
366
|
|
|
* |
367
|
|
|
* @return Entity|Model |
368
|
|
|
*/ |
369
|
|
|
public function create($attributes = []) |
370
|
|
|
{ |
371
|
|
|
return $this->model->create($attributes); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @param array $attributes |
376
|
|
|
* |
377
|
|
|
* @return Entity|Model |
378
|
|
|
*/ |
379
|
|
|
public function createOrUpdate($attributes = []) |
380
|
|
|
{ |
381
|
|
|
return $this->model->updateOrCreate($attributes); |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* @param array $data |
386
|
|
|
* |
387
|
|
|
* @return Model |
388
|
|
|
*/ |
389
|
|
|
public function createOrFirst($data = []) |
390
|
|
|
{ |
391
|
|
|
return $this->model->firstOrCreate($data); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* Get entity name. |
396
|
|
|
* |
397
|
|
|
* @return string |
398
|
|
|
*/ |
399
|
|
|
public function entityName() |
400
|
|
|
{ |
401
|
|
|
return $this->getModelClass(); |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* @param int $entityId |
406
|
|
|
* |
407
|
|
|
* @return bool |
408
|
|
|
*/ |
409
|
|
|
public function restore($entityId = 0) |
410
|
|
|
{ |
411
|
|
|
/** @var Entity $entity */ |
412
|
|
|
$entity = $this->model->withTrashed() |
413
|
|
|
->whereId($entityId) |
414
|
|
|
->first(); |
415
|
|
|
if ($entity) { |
|
|
|
|
416
|
|
|
return $entity->restore() ?? false; |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
return false; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* @param int $entityId |
424
|
|
|
* |
425
|
|
|
* @return bool |
426
|
|
|
*/ |
427
|
|
|
public function forceDelete($entityId = 0) |
428
|
|
|
{ |
429
|
|
|
/** @var Entity $entity */ |
430
|
|
|
$entity = $this->model->withTrashed() |
431
|
|
|
->whereId($entityId) |
432
|
|
|
->first(); |
433
|
|
|
if ($entity) { |
|
|
|
|
434
|
|
|
return $entity->forceDelete() ?? false; |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
return false; |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
public function trash() |
441
|
|
|
{ |
442
|
|
|
$this->trash = true; |
443
|
|
|
$this->withTrash = false; |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
public function withTrash() |
447
|
|
|
{ |
448
|
|
|
$this->trash = false; |
449
|
|
|
$this->withTrash = true; |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
public function disableCaching() |
453
|
|
|
{ |
454
|
|
|
$this->allowCaching = false; |
455
|
|
|
return $this; |
456
|
|
|
} |
457
|
|
|
|
458
|
|
|
public function allowCaching() |
459
|
|
|
{ |
460
|
|
|
$this->allowCaching = true; |
461
|
|
|
return $this; |
462
|
|
|
} |
463
|
|
|
} |
464
|
|
|
|