|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SebastianBerc\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Container\Container; |
|
6
|
|
|
use Illuminate\Contracts\Container\Container as Application; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Model as Eloquent; |
|
9
|
|
|
use Illuminate\Pagination\LengthAwarePaginator; |
|
10
|
|
|
use SebastianBerc\Repositories\Contracts\CriteriaInterface; |
|
11
|
|
|
use SebastianBerc\Repositories\Contracts\RepositoryInterface; |
|
12
|
|
|
use SebastianBerc\Repositories\Contracts\ShouldCache; |
|
13
|
|
|
use SebastianBerc\Repositories\Contracts\TransformerInterface; |
|
14
|
|
|
use SebastianBerc\Repositories\Exceptions\InvalidCriteria; |
|
15
|
|
|
use SebastianBerc\Repositories\Exceptions\InvalidRepositoryModel; |
|
16
|
|
|
use SebastianBerc\Repositories\Exceptions\InvalidTransformer; |
|
17
|
|
|
use SebastianBerc\Repositories\Mediators\RepositoryMediator; |
|
18
|
|
|
use SebastianBerc\Repositories\Services\CriteriaService; |
|
19
|
|
|
use SebastianBerc\Repositories\Traits\Filterable; |
|
20
|
|
|
use SebastianBerc\Repositories\Traits\Sortable; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Class Repositories. |
|
24
|
|
|
* |
|
25
|
|
|
* @author Sebastian Berć <[email protected]> |
|
26
|
|
|
* @copyright Copyright (c) Sebastian Berć |
|
27
|
|
|
*/ |
|
28
|
|
|
abstract class Repository implements RepositoryInterface |
|
29
|
|
|
{ |
|
30
|
|
|
use Filterable, Sortable; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Contains Laravel Application instance. |
|
34
|
|
|
* |
|
35
|
|
|
* @var Application |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $app; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Contains Eloquent model instance. |
|
41
|
|
|
* |
|
42
|
|
|
* @var Eloquent |
|
43
|
|
|
*/ |
|
44
|
|
|
public $model; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Contains relations to eager load. |
|
48
|
|
|
* |
|
49
|
|
|
* @var array |
|
50
|
|
|
*/ |
|
51
|
|
|
public $with = []; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Contains time of caching. |
|
55
|
|
|
* |
|
56
|
|
|
* @var int |
|
57
|
|
|
*/ |
|
58
|
|
|
public $lifetime; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Contains transformer instance. |
|
62
|
|
|
* |
|
63
|
|
|
* @var string |
|
64
|
|
|
*/ |
|
65
|
|
|
public $transformer; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Contains repository mediator instance. |
|
69
|
|
|
* |
|
70
|
|
|
* @var RepositoryMediator |
|
71
|
|
|
*/ |
|
72
|
|
|
public $mediator; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Create a new RepositoryInterface instance. |
|
76
|
|
|
* |
|
77
|
|
|
* @param Application $app |
|
78
|
|
|
*/ |
|
79
|
120 |
|
public function __construct(Application $app) |
|
80
|
|
|
{ |
|
81
|
120 |
|
$this->app = $app; |
|
82
|
120 |
|
$this->mediator = new RepositoryMediator($app, $this); |
|
83
|
120 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Create a new RepositoryInterface instance. |
|
87
|
|
|
* |
|
88
|
|
|
* @return static |
|
89
|
|
|
*/ |
|
90
|
8 |
|
public static function instance() |
|
91
|
|
|
{ |
|
92
|
8 |
|
return new static(function_exists('app') ? app() : Container::getInstance()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Call an action on mediator. |
|
97
|
|
|
* |
|
98
|
|
|
* @param array $parameters |
|
99
|
|
|
* |
|
100
|
|
|
* @return mixed |
|
101
|
|
|
*/ |
|
102
|
86 |
|
public function mediator(array $parameters) |
|
103
|
|
|
{ |
|
104
|
86 |
|
$caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function']; |
|
105
|
|
|
|
|
106
|
86 |
|
if ($this->shouldCache()) { |
|
107
|
20 |
|
return $this->mediator->cache($caller, $parameters); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
66 |
|
return $this->mediator->database($caller, $parameters); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Add criteria to repository query or return criteria service. |
|
115
|
|
|
* |
|
116
|
|
|
* @param CriteriaInterface|null $criteria |
|
117
|
|
|
* |
|
118
|
|
|
* @throws InvalidCriteria |
|
119
|
|
|
* |
|
120
|
|
|
* @return CriteriaService|$this |
|
121
|
|
|
*/ |
|
122
|
8 |
|
public function criteria($criteria = null) |
|
123
|
|
|
{ |
|
124
|
8 |
|
if (is_null($criteria)) { |
|
125
|
2 |
|
return $this->mediator->criteria(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
8 |
|
if (!is_null($criteria) && !is_a($criteria, CriteriaInterface::class)) { |
|
129
|
2 |
|
throw new InvalidCriteria(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
6 |
|
$this->mediator->criteria()->addCriteria($criteria); |
|
133
|
|
|
|
|
134
|
6 |
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Return fully qualified model class name. |
|
139
|
|
|
* |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
|
|
abstract public function takeModel(); |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Return instance of Eloquent model. |
|
146
|
|
|
* |
|
147
|
|
|
* @throws InvalidRepositoryModel |
|
148
|
|
|
* |
|
149
|
|
|
* @return Eloquent |
|
150
|
|
|
*/ |
|
151
|
120 |
|
public function makeModel() |
|
152
|
|
|
{ |
|
153
|
120 |
|
if (!($this->model = $this->app->make($this->takeModel())) instanceof Eloquent) { |
|
154
|
8 |
|
throw new InvalidRepositoryModel(get_class($this->model), Eloquent::class); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
120 |
|
return $this->model; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Return instance of query builder for Eloquent model. |
|
162
|
|
|
* |
|
163
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
164
|
|
|
*/ |
|
165
|
84 |
|
public function makeQuery() |
|
166
|
|
|
{ |
|
167
|
84 |
|
$query = $this->makeModel()->query(); |
|
168
|
|
|
|
|
169
|
84 |
|
if ($this->mediator->hasCriteria()) { |
|
170
|
4 |
|
$query = $this->mediator->criteria()->executeOn($this->makeModel()->query()); |
|
171
|
4 |
|
} |
|
172
|
|
|
|
|
173
|
84 |
|
return empty($this->with) ? $query : $query->with($this->with); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Adds relation to eager loads. |
|
178
|
|
|
* |
|
179
|
|
|
* @param string|string[] $relation |
|
180
|
|
|
* |
|
181
|
|
|
* @return static |
|
182
|
|
|
*/ |
|
183
|
4 |
|
public function with($relation) |
|
184
|
|
|
{ |
|
185
|
4 |
|
if (func_num_args() == 1) { |
|
186
|
2 |
|
$this->with = array_merge($this->with, is_array($relation) ? $relation : [$relation]); |
|
187
|
2 |
|
} else { |
|
188
|
2 |
|
$this->with = array_merge($this->with, func_get_args()); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
4 |
|
return $this; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Sets transformer to current repository instance. |
|
196
|
|
|
* |
|
197
|
|
|
* @param string $transformer |
|
198
|
|
|
* |
|
199
|
|
|
* @throws InvalidTransformer |
|
200
|
|
|
* |
|
201
|
|
|
* @return static |
|
202
|
|
|
*/ |
|
203
|
4 |
|
public function setTransformer($transformer) |
|
204
|
|
|
{ |
|
205
|
4 |
|
if (!(new \ReflectionClass($transformer))->implementsInterface(TransformerInterface::class)) { |
|
206
|
2 |
|
throw new InvalidTransformer(); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
2 |
|
$this->transformer = $transformer; |
|
210
|
|
|
|
|
211
|
2 |
|
return $this; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Get all of the models from the database. |
|
216
|
|
|
* |
|
217
|
|
|
* @param string[] $columns |
|
218
|
|
|
* |
|
219
|
|
|
* @return Collection |
|
220
|
|
|
*/ |
|
221
|
12 |
|
public function all(array $columns = ['*']) |
|
222
|
|
|
{ |
|
223
|
12 |
|
return $this->mediator->transform($this->mediator(func_get_args())); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Create a new basic where query clause on model. |
|
228
|
|
|
* |
|
229
|
|
|
* @param string|array $column |
|
230
|
|
|
* @param string $operator |
|
231
|
|
|
* @param mixed $value |
|
232
|
|
|
* @param string $boolean |
|
233
|
|
|
* @param string[] $columns |
|
234
|
|
|
* |
|
235
|
|
|
* @return mixed |
|
236
|
|
|
*/ |
|
237
|
8 |
|
public function where($column, $operator = '=', $value = null, $boolean = 'and', array $columns = ['*']) |
|
238
|
|
|
{ |
|
239
|
8 |
|
return $this->mediator->transform($this->mediator(func_get_args())); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Paginate the given query. |
|
244
|
|
|
* |
|
245
|
|
|
* @param int $perPage |
|
246
|
|
|
* @param string[] $columns |
|
247
|
|
|
* |
|
248
|
|
|
* @return LengthAwarePaginator |
|
249
|
|
|
*/ |
|
250
|
8 |
|
public function paginate($perPage = 15, array $columns = ['*']) |
|
251
|
|
|
{ |
|
252
|
8 |
|
return $this->mediator->transformPaginator($this->mediator(func_get_args())); |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Save a new model and return the instance. |
|
257
|
|
|
* |
|
258
|
|
|
* @param array $attributes |
|
259
|
|
|
* |
|
260
|
|
|
* @return Eloquent |
|
261
|
|
|
*/ |
|
262
|
6 |
|
public function create(array $attributes = []) |
|
263
|
|
|
{ |
|
264
|
6 |
|
return $this->mediator->transform($this->mediator(func_get_args()))->first(); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
/** |
|
268
|
|
|
* Save or update the model in the database. |
|
269
|
|
|
* |
|
270
|
|
|
* @param mixed $identifier |
|
271
|
|
|
* @param array $attributes |
|
272
|
|
|
* |
|
273
|
|
|
* @return Eloquent |
|
274
|
|
|
*/ |
|
275
|
6 |
|
public function update($identifier, array $attributes = []) |
|
276
|
|
|
{ |
|
277
|
6 |
|
return $this->mediator->transform($this->mediator(func_get_args()))->first(); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* Delete the model from the database. |
|
282
|
|
|
* |
|
283
|
|
|
* @param int $identifier |
|
284
|
|
|
* |
|
285
|
|
|
* @return bool|null |
|
286
|
|
|
*/ |
|
287
|
6 |
|
public function delete($identifier) |
|
288
|
|
|
{ |
|
289
|
6 |
|
return $this->mediator(func_get_args()); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Find a model by its primary key. |
|
294
|
|
|
* |
|
295
|
|
|
* @param int $identifier |
|
296
|
|
|
* @param string[] $columns |
|
297
|
|
|
* |
|
298
|
|
|
* @return Eloquent |
|
299
|
|
|
*/ |
|
300
|
28 |
|
public function find($identifier, array $columns = ['*']) |
|
301
|
|
|
{ |
|
302
|
28 |
|
return $this->mediator->transform($this->mediator(func_get_args()))->first(); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* Find a model by its specified column and value. |
|
307
|
|
|
* |
|
308
|
|
|
* @param mixed $column |
|
309
|
|
|
* @param mixed $value |
|
310
|
|
|
* @param string[] $columns |
|
311
|
|
|
* |
|
312
|
|
|
* @return Eloquent |
|
313
|
|
|
*/ |
|
314
|
8 |
|
public function findBy($column, $value, array $columns = ['*']) |
|
315
|
|
|
{ |
|
316
|
8 |
|
return $this->mediator->transform($this->mediator(func_get_args()))->first(); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* Find a model by its specified columns and values. |
|
321
|
|
|
* |
|
322
|
|
|
* @param array $wheres |
|
323
|
|
|
* @param string[] $columns |
|
324
|
|
|
* |
|
325
|
|
|
* @return Eloquent |
|
326
|
|
|
*/ |
|
327
|
8 |
|
public function findWhere(array $wheres, array $columns = ['*']) |
|
328
|
|
|
{ |
|
329
|
8 |
|
return $this->mediator->transform($this->mediator(func_get_args()))->first(); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* Returns total count of whole collection. |
|
334
|
|
|
* |
|
335
|
|
|
* @return int |
|
336
|
|
|
*/ |
|
337
|
4 |
|
public function count() |
|
338
|
|
|
{ |
|
339
|
4 |
|
return $this->mediator(func_get_args()); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* Fetch collection ordered and filtrated by specified columns for specified page. |
|
344
|
|
|
* |
|
345
|
|
|
* @param int $page |
|
346
|
|
|
* @param int $perPage |
|
347
|
|
|
* @param array $filter |
|
348
|
|
|
* @param array $sort |
|
349
|
|
|
* @param array $columns |
|
350
|
|
|
* |
|
351
|
|
|
* @return LengthAwarePaginator |
|
352
|
|
|
*/ |
|
353
|
12 |
|
public function fetch($page = 1, $perPage = 15, array $columns = ['*'], array $filter = [], array $sort = []) |
|
354
|
|
|
{ |
|
355
|
12 |
|
return $this->mediator->transformPaginator($this->mediator(func_get_args())); |
|
356
|
|
|
} |
|
357
|
|
|
|
|
358
|
|
|
/** |
|
359
|
|
|
* @param int $page |
|
360
|
|
|
* @param int $perPage |
|
361
|
|
|
* @param array $columns |
|
362
|
|
|
* @param array $filter |
|
363
|
|
|
* @param array $sort |
|
364
|
|
|
* |
|
365
|
|
|
* @return Collection |
|
366
|
|
|
*/ |
|
367
|
4 |
|
public function simpleFetch($page = 1, $perPage = 15, array $columns = ['*'], array $filter = [], array $sort = []) |
|
368
|
|
|
{ |
|
369
|
4 |
|
return $this->mediator->transform($this->mediator(func_get_args())); |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
/** |
|
373
|
|
|
* Determinate if repository should be cached. |
|
374
|
|
|
* |
|
375
|
|
|
* @return bool |
|
376
|
|
|
*/ |
|
377
|
86 |
|
protected function shouldCache() |
|
378
|
|
|
{ |
|
379
|
86 |
|
if ($this instanceof ShouldCache) { |
|
380
|
20 |
|
return true; |
|
381
|
|
|
} |
|
382
|
|
|
|
|
383
|
66 |
|
return (new \ReflectionClass($this))->implementsInterface(ShouldCache::class); |
|
384
|
|
|
} |
|
385
|
|
|
|
|
386
|
|
|
/** |
|
387
|
|
|
* Dynamicaly calls method on model instance. |
|
388
|
|
|
* |
|
389
|
|
|
* @param string $method |
|
390
|
|
|
* @param array $parameters |
|
391
|
|
|
* |
|
392
|
|
|
* @throws InvalidRepositoryModel |
|
393
|
|
|
* |
|
394
|
|
|
* @return mixed |
|
395
|
|
|
*/ |
|
396
|
12 |
|
public function __call($method, $parameters) |
|
397
|
|
|
{ |
|
398
|
12 |
|
if (method_exists($model = $this->makeModel(), $method)) { |
|
399
|
4 |
|
return call_user_func_array([$model, $method], $parameters); |
|
400
|
|
|
} |
|
401
|
|
|
|
|
402
|
8 |
|
throw new \BadMethodCallException(); |
|
403
|
|
|
} |
|
404
|
|
|
} |
|
405
|
|
|
|