-
Notifications
You must be signed in to change notification settings - Fork 1
/
Repository.php
503 lines (453 loc) · 13.3 KB
/
Repository.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
<?php
namespace ActivismeBE\DatabaseLayering\Repositories\Eloquent;
use ActivismeBE\DatabaseLayering\Repositories\Contracts\CriteriaInterface;
use ActivismeBE\DatabaseLayering\Repositories\Contracts\RepositoryInterface;
use ActivismeBE\DatabaseLayering\Repositories\Criteria\Criteria;
use ActivismeBE\DatabaseLayering\Repositories\Exceptions\RepositoryException;
use Closure;
use Illuminate\Container\Container as App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Collection;
/**
* Class Repository
*
* @category
* @author
* @author
* @license MIT License <https://cpsb.github.io/ActivismeBE-database-layering/license>
* @link https://cpsb.github.io/ActivismeBE-database-layering/license
* @package ActivismeBE\DatabaseLayering\Eloquent
*/
abstract class Repository implements RepositoryInterface, CriteriaInterface
{
/**
* @var App
*/
private $app;
/**
* @var Model
*/
protected $model;
/**
* @var Collection
*/
protected $newModel;
/**
* @var Collection
*/
protected $criteria;
/**
* @var bool
*/
protected $skipCriteria = false;
/**
* Prevents from overwriting same criteria in chain usage.
*
* @var bool
*/
protected $preventCriteriaOverwriting = true;
/**
* @param App $app
* @param Collection $collection
*
* @throws \ActivismeBE\DatabaseLayering\Repositories\Exceptions\RepositoryException
*/
public function __construct(App $app, Collection $collection)
{
$this->app = $app;
$this->criteria = $collection;
$this->resetScope();
$this->makeModel();
}
/**
* Specify Model class name
*
* @return mixed
*/
abstract public function model();
/**
* Get the base enttiy form the model.
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function entity()
{
return $this->newModel;
}
/**
* Get all the records form the database table.
*
* @param array $columns The database column names u want to use in your view.
*
* @return mixed
*/
public function all($columns = ['*'])
{
$this->applyCriteria();
return $this->model->get($columns);
}
/**
* Apply database relations on the query.
*
* @param array $relations The relations u want to apply on your query.
*
* @return $this
*/
public function with(array $relations)
{
$this->model = $this->model->with($relations);
return $this;
}
/**
* Pluck all the values based on key and column.
*
* @param string $value The value for the lists function.
* @param string $key The key for the lists function.
*
* @deprecated deprecated in Laravel 5.3
*
* @return array
*/
public function pluck($value, $key = null)
{
$this->applyCriteria();
$lists = $this->model->lists($value, $key);
if (is_array($lists)) {
return $lists;
}
return $lists->all();
}
/**
* Paginate the database results from the query.
*
* @param integer $perPage The data records u want to display per page.
* @param array $columns Te database columns u want to use in the view.
*
* @return mixed
*/
public function paginate($perPage = 25, $columns = ['*'])
{
$this->applyCriteria();
return $this->model->paginate($perPage, $columns);
}
/**
* (Simple) paginate the database results from the query
*
* @param integer $perPage The data rows per page in the view
* @param array $columns The columns u want to use in your view
* @return mixed
*/
public function simplePaginate(int $perPage = 25, array $columns = ['*'])
{
$this->applyCriteria();
return $this->model->simplePaginate($perPage, $columns);
}
/**
* Create a new data record in the database.
*
* @param array $data The data fields u want to store in the database table.
*
* @return mixed
*/
public function create(array $data)
{
return $this->model->create($data);
}
/**
* Fill attributes data before saving.
*
* @param array $data The given data from the input.
* @param integer $primaryKey The primary key in the database table.
*
* @throws ModelNotFoundException
*
* @return mixed
*/
public function fill(array $data, $primaryKey)
{
$model = $this->makeModel()->find($primaryKey);
if (! $model) {
throw new ModelNotFoundException("Model '" . $this->model() . "' with id ${id} not found.");
}
return $model->fill($data)->save();
}
/**
* Save a model without mass assignment
*
* @param array $data
*
* @return bool
*/
public function saveModel(array $data)
{
foreach ($data as $key => $value) {
$this->model->$key = $value;
}
return $this->model->save();
}
/**
* Update a record in the database table.
*
* @param array $data
* @param int $primaryKey
* @param string $attribute
*
* @return mixed
*/
public function update(array $data, $primaryKey, $attribute = "id")
{
return $this->model->where($attribute, '=', $primaryKey)->update($data);
}
/**
* Update database records through the eloquent fill method.
*
* @param array $data The data fields u want to
* @param integer $primaryKey The primary key in the database column.
*
* @return mixed
*/
public function updateRich(array $data, $primaryKey)
{
if (! ($model = $this->model->find($primaryKey))) {
return false;
}
return $model->fill($data)->save();
}
/**
* Delete a record in the database.
*
* @param int $primaryKey The resource id in the database.
*
* @return mixed
*/
public function delete($primaryKey)
{
return $this->model->destroy($primaryKey);
}
/**
* Delete all by field and value.
*
* @param string $field The database column name.
* @param string $selector The where clause selector.
* @param string $value The value for the given database column.
*
* @return boolean
*/
public function deleteAllBy($field, $selector, $value)
{
$this->applyCriteria();
return $this->model->where($field, $selector, $value)->delete();
}
/**
* Find a record in the database based on the primary key.
*
* @param int $primaryKey The resource id in the database.
* @param array $columns The database columns u want to use.
* @return mixed
*/
public function find($primaryKey, $columns = ['*'])
{
$this->applyCriteria();
return $this->model->find($primaryKey, $columns);
}
/**
* Try to find a record in the database table based on the primary key.
*
* @param integer $primaryKey The primary key in the database table.
* @param array $columns The database columns u want to use.
* @return mixed
*/
public function findOrFail(int $primaryKey, array $columns = ['*'])
{
return $this->model->findOrFail($primaryKey, $columns);
}
/**
* Find the first record in the database based on column and value.
*
* @param string $attribute The database column name.
* @param string $value The value that u want to find in the database table.
* @param array $columns The database columns u want to use.
*
* @return mixed
*/
public function findBy($attribute, $value, $columns = ['*'])
{
$this->applyCriteria();
return $this->model->where($attribute, '=', $value)->first($columns);
}
/**
* Find all the records in the database bases on column and value.
*
* @param string $attribute The database column name.
* @param string $value The value where u want to search on.
* @param array $columns The database columns want to use.
*
* @return mixed
*/
public function findAllBy($attribute, $value, $columns = ['*'])
{
$this->applyCriteria();
return $this->model->where($attribute, '=', $value)->get($columns);
}
/**
* Find a collection of models by the given query conditions.
*
* @param array $where The where criteria for the find query.
* @param array $columns The database table columns u want to use in your view.
* @param bool $or Enable of disable setter for OR WHERE queries.
*
* @return \Illuminate\Database\Eloquent\Collection|null
*/
public function findWhere($where, $columns = ['*'], $or = false)
{
$this->applyCriteria();
$model = $this->model;
foreach ($where as $field => $value) {
if ($value instanceof Closure) {
$model = (!$or)
? $model->where($value)
: $model->orWhere($value);
} elseif (is_array($value)) {
if (count($value) === 3) {
list($field, $operator, $search) = $value;
$model = (!$or)
? $model->where($field, $operator, $search)
: $model->orWhere($field, $operator, $search);
} elseif (count($value) === 2) {
list($field, $search) = $value;
$model = (!$or)
? $model->where($field, '=', $search)
: $model->orWhere($field, '=', $search);
}
} else {
$model = (!$or)
? $model->where($field, '=', $value)
: $model->orWhere($field, '=', $value);
}
}
return $model->get($columns);
}
/**
* Search for matching values in some specific attribute.
*
* @param string $attribute The name from the attribute.
* @param array $values The array of matching values
* @param array$columns The columns u want to display
* @return mixed
*/
public function whereIn($attribute, array $values, $columns = ['*'])
{
$this->applyCriteria();
return $this->model->whereIn($attribute, $values)->get($columns);
}
/**
* Make a model instance in the repository.
*
* @return \Illuminate\Database\Eloquent\Builder
* @throws RepositoryException
*/
public function makeModel()
{
return $this->setModel($this->model());
}
/**
* Set Eloquent Model to instantiate
*
* @param $eloquentModel
*
* @throws RepositoryException
*
* @return Model
*/
public function setModel($eloquentModel)
{
$this->newModel = $this->app->make($eloquentModel);
if (! $this->newModel instanceof Model) {
throw new RepositoryException(
"Class {$this->newModel} must be an instance of Illuminate\\Database\\Eloquent\\Model"
);
}
return $this->model = $this->newModel;
}
/**
* Reset the query scope.
*
* @return $this
*/
public function resetScope()
{
$this->skipCriteria(false);
return $this;
}
/**
* Skip the given criteria.
*
* @param bool $status
*
* @return $this
*/
public function skipCriteria($status = true)
{
$this->skipCriteria = $status;
return $this;
}
/**
* Get the criteria for the database query.
*
* @return mixed
*/
public function getCriteria()
{
return $this->criteria;
}
/**
* Get records based on the repository call criteria.
*
* @param Criteria $criteria The criteria u want to take.
*
* @return $this
*/
public function getByCriteria(Criteria $criteria)
{
$this->model = $criteria->apply($this->model, $this);
return $this;
}
/**
* Push new query criteria in the interface call.
*
* @param Criteria $criteria The Criteria instance u want to apply.
*
* @return $this
*/
public function pushCriteria(Criteria $criteria)
{
if ($this->preventCriteriaOverwriting) { // Find existing criteria
$key = $this->criteria->search(function ($item) use ($criteria) {
return (is_object($item) && (get_class($item) == get_class($criteria)));
});
if (is_int($key)) { // Remove old criteria
$this->criteria->offsetUnset($key);
}
}
$this->criteria->push($criteria);
return $this;
}
/**
* Applies a new criteria in the repository call.
*
* @return $this
*/
public function applyCriteria()
{
if ($this->skipCriteria === true) {
return $this;
}
foreach ($this->getCriteria() as $criteria) {
if ($criteria instanceof Criteria) {
$this->model = $criteria->apply($this->model, $this);
}
}
return $this;
}
}