1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* NOTICE OF LICENSE |
5
|
|
|
* |
6
|
|
|
* Part of the Rinvex Repository Package. |
7
|
|
|
* |
8
|
|
|
* This source file is subject to The MIT License (MIT) |
9
|
|
|
* that is bundled with this package in the LICENSE file. |
10
|
|
|
* |
11
|
|
|
* Package: Rinvex Repository Package |
12
|
|
|
* License: The MIT License (MIT) |
13
|
|
|
* Link: https://rinvex.com |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Rinvex\Repository\Repositories; |
17
|
|
|
|
18
|
|
|
use Illuminate\Pagination\Paginator; |
19
|
|
|
use Illuminate\Database\Eloquent\Model; |
20
|
|
|
use Rinvex\Repository\Exceptions\RepositoryException; |
21
|
|
|
use Rinvex\Repository\Exceptions\EntityNotFoundException; |
22
|
|
|
|
23
|
|
|
class EloquentRepository extends BaseRepository |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function createModel() |
29
|
|
|
{ |
30
|
|
|
if (is_string($model = $this->getModel())) { |
31
|
|
|
if (! class_exists($class = '\\'.ltrim($model, '\\'))) { |
32
|
|
|
throw new RepositoryException("Class {$model} does NOT exist!"); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$model = $this->getContainer()->make($class); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// Set the connection used by the model |
39
|
|
|
if (! empty($this->connection)) { |
40
|
|
|
$model = $model->setConnection($this->connection); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (! $model instanceof Model) { |
44
|
|
|
throw new RepositoryException("Class {$model} must be an instance of \\Illuminate\\Database\\Eloquent\\Model"); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $model; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function find($id, $attributes = ['*']) |
54
|
|
|
{ |
55
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($id, $attributes) { |
56
|
|
|
return $this->prepareQuery($this->createModel())->find($id, $attributes); |
57
|
|
|
}); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function findOrFail($id, $attributes = ['*']) |
64
|
|
|
{ |
65
|
|
|
$result = $this->find($id, $attributes); |
66
|
|
|
|
67
|
|
|
if (is_array($id)) { |
68
|
|
|
if (count($result) == count(array_unique($id))) { |
69
|
|
|
return $result; |
70
|
|
|
} |
71
|
|
|
} elseif (! is_null($result)) { |
72
|
|
|
return $result; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
throw new EntityNotFoundException($this->getModel(), $id); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function findOrNew($id, $attributes = ['*']) |
82
|
|
|
{ |
83
|
|
|
if (! is_null($entity = $this->find($id, $attributes))) { |
84
|
|
|
return $entity; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $this->createModel(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function findBy($attribute, $value, $attributes = ['*']) |
94
|
|
|
{ |
95
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($attribute, $value, $attributes) { |
96
|
|
|
return $this->prepareQuery($this->createModel())->where($attribute, '=', $value)->first($attributes); |
97
|
|
|
}); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public function findFirst($attributes = ['*']) |
104
|
|
|
{ |
105
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($attributes) { |
106
|
|
|
return $this->prepareQuery($this->createModel())->first($attributes); |
107
|
|
|
}); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
|
|
public function findAll($attributes = ['*']) |
114
|
|
|
{ |
115
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($attributes) { |
116
|
|
|
return $this->prepareQuery($this->createModel())->get($attributes); |
117
|
|
|
}); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
|
|
public function paginate($perPage = null, $attributes = ['*'], $pageName = 'page', $page = null) |
124
|
|
|
{ |
125
|
|
|
$page = $page ?: Paginator::resolveCurrentPage($pageName); |
126
|
|
|
|
127
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, array_merge(func_get_args(), compact('page')), function () use ($perPage, $attributes, $pageName, $page) { |
128
|
|
|
return $this->prepareQuery($this->createModel())->paginate($perPage, $attributes, $pageName, $page); |
129
|
|
|
}); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
|
|
*/ |
135
|
|
|
public function simplePaginate($perPage = null, $attributes = ['*'], $pageName = 'page', $page = null) |
136
|
|
|
{ |
137
|
|
|
$page = $page ?: Paginator::resolveCurrentPage($pageName); |
138
|
|
|
|
139
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, array_merge(func_get_args(), compact('page')), function () use ($perPage, $attributes, $pageName, $page) { |
|
|
|
|
140
|
|
|
return $this->prepareQuery($this->createModel())->simplePaginate($perPage, $attributes, $pageName, $page); |
141
|
|
|
}); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
*/ |
147
|
|
|
public function findWhere(array $where, $attributes = ['*']) |
148
|
|
|
{ |
149
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($where, $attributes) { |
|
|
|
|
150
|
|
|
list($attribute, $operator, $value, $boolean) = array_pad($where, 4, null); |
151
|
|
|
|
152
|
|
|
$this->where($attribute, $operator, $value, $boolean); |
153
|
|
|
|
154
|
|
|
return $this->prepareQuery($this->createModel())->get($attributes); |
155
|
|
|
}); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
|
|
public function findWhereIn(array $where, $attributes = ['*']) |
162
|
|
|
{ |
163
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($where, $attributes) { |
164
|
|
|
list($attribute, $values, $boolean, $not) = array_pad($where, 4, null); |
165
|
|
|
|
166
|
|
|
$this->whereIn($attribute, $values, $boolean, $not); |
167
|
|
|
|
168
|
|
|
return $this->prepareQuery($this->createModel())->get($attributes); |
169
|
|
|
}); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* {@inheritdoc} |
174
|
|
|
*/ |
175
|
|
|
public function findWhereNotIn(array $where, $attributes = ['*']) |
176
|
|
|
{ |
177
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($where, $attributes) { |
178
|
|
|
list($attribute, $values, $boolean) = array_pad($where, 3, null); |
179
|
|
|
|
180
|
|
|
$this->whereNotIn($attribute, $values, $boolean); |
181
|
|
|
|
182
|
|
|
return $this->prepareQuery($this->createModel())->get($attributes); |
183
|
|
|
}); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* {@inheritdoc} |
188
|
|
|
*/ |
189
|
|
|
public function findWhereHas(array $where, $attributes = ['*']) |
190
|
|
|
{ |
191
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($where, $attributes) { |
|
|
|
|
192
|
|
|
list($relation, $callback, $operator, $count) = array_pad($where, 4, null); |
193
|
|
|
|
194
|
|
|
$this->whereHas($relation, $callback, $operator, $count); |
195
|
|
|
|
196
|
|
|
return $this->prepareQuery($this->createModel())->get($attributes); |
197
|
|
|
}); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* {@inheritdoc} |
202
|
|
|
*/ |
203
|
|
|
public function create(array $attributes = []) |
204
|
|
|
{ |
205
|
|
|
// Create a new instance |
206
|
|
|
$entity = $this->createModel(); |
207
|
|
|
|
208
|
|
|
// Fire the created event |
209
|
|
|
$this->getContainer('events')->fire($this->getRepositoryId().'.entity.creating', [$this, $entity]); |
210
|
|
|
|
211
|
|
|
// Fill instance with data |
212
|
|
|
$entity->fill($attributes); |
213
|
|
|
|
214
|
|
|
// Save the instance |
215
|
|
|
$created = $entity->save(); |
216
|
|
|
|
217
|
|
|
// Fire the created event |
218
|
|
|
$this->getContainer('events')->fire($this->getRepositoryId().'.entity.created', [$this, $entity]); |
219
|
|
|
|
220
|
|
|
// Return instance |
221
|
|
|
return $created ? $entity : $created; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* {@inheritdoc} |
226
|
|
|
*/ |
227
|
|
|
public function update($id, array $attributes = []) |
228
|
|
|
{ |
229
|
|
|
$updated = false; |
230
|
|
|
|
231
|
|
|
// Find the given instance |
232
|
|
|
$entity = $id instanceof Model ? $id : $this->find($id); |
233
|
|
|
|
234
|
|
|
if ($entity) { |
235
|
|
|
// Fire the updated event |
236
|
|
|
$this->getContainer('events')->fire($this->getRepositoryId().'.entity.updating', [$this, $entity]); |
237
|
|
|
|
238
|
|
|
// Fill instance with data |
239
|
|
|
$entity->fill($attributes); |
240
|
|
|
|
241
|
|
|
// Update the instance |
242
|
|
|
$updated = $entity->save(); |
243
|
|
|
|
244
|
|
|
// Fire the updated event |
245
|
|
|
$this->getContainer('events')->fire($this->getRepositoryId().'.entity.updated', [$this, $entity]); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return $updated ? $entity : $updated; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* {@inheritdoc} |
253
|
|
|
*/ |
254
|
|
|
public function delete($id) |
255
|
|
|
{ |
256
|
|
|
$deleted = false; |
257
|
|
|
|
258
|
|
|
// Find the given instance |
259
|
|
|
$entity = $id instanceof Model ? $id : $this->find($id); |
260
|
|
|
|
261
|
|
|
if ($entity) { |
262
|
|
|
// Fire the deleted event |
263
|
|
|
$this->getContainer('events')->fire($this->getRepositoryId().'.entity.deleting', [$this, $entity]); |
264
|
|
|
|
265
|
|
|
// Delete the instance |
266
|
|
|
$deleted = $entity->delete(); |
267
|
|
|
|
268
|
|
|
// Fire the deleted event |
269
|
|
|
$this->getContainer('events')->fire($this->getRepositoryId().'.entity.deleted', [$this, $entity]); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return $deleted ? $entity : $deleted; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* {@inheritdoc} |
277
|
|
|
*/ |
278
|
|
|
public function beginTransaction() |
279
|
|
|
{ |
280
|
|
|
$this->getContainer('db')->beginTransaction(); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* {@inheritdoc} |
285
|
|
|
*/ |
286
|
|
|
public function commit() |
287
|
|
|
{ |
288
|
|
|
$this->getContainer('db')->commit(); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* {@inheritdoc} |
293
|
|
|
*/ |
294
|
|
|
public function rollBack() |
295
|
|
|
{ |
296
|
|
|
$this->getContainer('db')->rollBack(); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* {@inheritdoc} |
301
|
|
|
*/ |
302
|
|
|
public function count($columns = '*') |
303
|
|
|
{ |
304
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($columns) { |
305
|
|
|
return $this->prepareQuery($this->createModel())->count($columns); |
306
|
|
|
}); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* {@inheritdoc} |
311
|
|
|
*/ |
312
|
|
|
public function min($column) |
313
|
|
|
{ |
314
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($column) { |
315
|
|
|
return $this->prepareQuery($this->createModel())->min($column); |
316
|
|
|
}); |
317
|
|
|
} |
318
|
|
|
|
319
|
|
|
/** |
320
|
|
|
* {@inheritdoc} |
321
|
|
|
*/ |
322
|
|
|
public function max($column) |
323
|
|
|
{ |
324
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($column) { |
325
|
|
|
return $this->prepareQuery($this->createModel())->max($column); |
326
|
|
|
}); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* {@inheritdoc} |
331
|
|
|
*/ |
332
|
|
|
public function avg($column) |
333
|
|
|
{ |
334
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($column) { |
335
|
|
|
return $this->prepareQuery($this->createModel())->avg($column); |
336
|
|
|
}); |
337
|
|
|
} |
338
|
|
|
|
339
|
|
|
/** |
340
|
|
|
* {@inheritdoc} |
341
|
|
|
*/ |
342
|
|
|
public function sum($column) |
343
|
|
|
{ |
344
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($column) { |
345
|
|
|
return $this->prepareQuery($this->createModel())->sum($column); |
346
|
|
|
}); |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.