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\Database\Eloquent\Model; |
19
|
|
|
use Illuminate\Pagination\Paginator; |
20
|
|
|
use Rinvex\Repository\Exceptions\RepositoryException; |
21
|
|
|
|
22
|
|
|
class EloquentRepository extends BaseRepository |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
public function createModel() |
28
|
|
|
{ |
29
|
|
|
if (is_string($model = $this->getModel())) { |
30
|
|
|
if (! class_exists($class = '\\'.ltrim($model, '\\'))) { |
31
|
|
|
throw new RepositoryException("Class {$model} does NOT exist!"); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$model = $this->getContainer()->make($class); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// Set the connection used by the model |
38
|
|
|
if (! empty($this->connection)) { |
39
|
|
|
$model = $model->setConnection($this->connection); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (! $model instanceof Model) { |
43
|
|
|
throw new RepositoryException("Class {$model} must be an instance of \\Illuminate\\Database\\Eloquent\\Model"); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return $model; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function find($id, $attributes = ['*']) |
53
|
|
|
{ |
54
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($id, $attributes) { |
55
|
|
|
return $this->prepareQuery($this->createModel())->find($id, $attributes); |
56
|
|
|
}); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function findBy($attribute, $value, $attributes = ['*']) |
63
|
|
|
{ |
64
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($attribute, $value, $attributes) { |
65
|
|
|
return $this->prepareQuery($this->createModel())->where($attribute, '=', $value)->first($attributes); |
66
|
|
|
}); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
|
|
public function findFirst($attributes = ['*']) |
73
|
|
|
{ |
74
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($attributes) { |
75
|
|
|
return $this->prepareQuery($this->createModel())->first($attributes); |
76
|
|
|
}); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
public function findAll($attributes = ['*']) |
83
|
|
|
{ |
84
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($attributes) { |
85
|
|
|
return $this->prepareQuery($this->createModel())->get($attributes); |
86
|
|
|
}); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function paginate($perPage = null, $attributes = ['*'], $pageName = 'page', $page = null) |
93
|
|
|
{ |
94
|
|
|
$page = $page ?: Paginator::resolveCurrentPage($pageName); |
95
|
|
|
|
96
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, array_merge(func_get_args(), compact('page')), function () use ($perPage, $attributes, $pageName, $page) { |
97
|
|
|
return $this->prepareQuery($this->createModel())->paginate($perPage, $attributes, $pageName, $page); |
98
|
|
|
}); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function simplePaginate($perPage = null, $attributes = ['*'], $pageName = 'page', $page = null) |
105
|
|
|
{ |
106
|
|
|
$page = $page ?: Paginator::resolveCurrentPage($pageName); |
107
|
|
|
|
108
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, array_merge(func_get_args(), compact('page')), function () use ($perPage, $attributes, $pageName, $page) { |
|
|
|
|
109
|
|
|
return $this->prepareQuery($this->createModel())->simplePaginate($perPage, $attributes, $pageName, $page); |
110
|
|
|
}); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
|
|
public function findWhere(array $where, $attributes = ['*']) |
117
|
|
|
{ |
118
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($where, $attributes) { |
|
|
|
|
119
|
|
|
list($attribute, $operator, $value, $boolean) = array_pad($where, 4, null); |
120
|
|
|
|
121
|
|
|
$this->where($attribute, $operator, $value, $boolean); |
122
|
|
|
|
123
|
|
|
return $this->prepareQuery($this->createModel())->get($attributes); |
124
|
|
|
}); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
|
|
public function findWhereIn(array $where, $attributes = ['*']) |
131
|
|
|
{ |
132
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($where, $attributes) { |
|
|
|
|
133
|
|
|
list($attribute, $values, $boolean, $not) = array_pad($where, 4, null); |
134
|
|
|
|
135
|
|
|
$this->whereIn($attribute, $values, $boolean, $not); |
136
|
|
|
|
137
|
|
|
return $this->prepareQuery($this->createModel())->get($attributes); |
138
|
|
|
}); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
|
|
public function findWhereNotIn(array $where, $attributes = ['*']) |
145
|
|
|
{ |
146
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($where, $attributes) { |
147
|
|
|
list($attribute, $values, $boolean) = array_pad($where, 3, null); |
148
|
|
|
|
149
|
|
|
$this->whereNotIn($attribute, $values, $boolean); |
150
|
|
|
|
151
|
|
|
return $this->prepareQuery($this->createModel())->get($attributes); |
152
|
|
|
}); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
*/ |
158
|
|
|
public function findWhereHas(array $where, $attributes = ['*']) |
159
|
|
|
{ |
160
|
|
|
return $this->executeCallback(get_called_class(), __FUNCTION__, func_get_args(), function () use ($where, $attributes) { |
|
|
|
|
161
|
|
|
list($relation, $callback, $operator, $count) = array_pad($where, 4, null); |
162
|
|
|
|
163
|
|
|
$this->whereHas($relation, $callback, $operator, $count); |
164
|
|
|
|
165
|
|
|
return $this->prepareQuery($this->createModel())->get($attributes); |
166
|
|
|
}); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* {@inheritdoc} |
171
|
|
|
*/ |
172
|
|
|
public function create(array $attributes = []) |
173
|
|
|
{ |
174
|
|
|
// Create a new instance |
175
|
|
|
$instance = $this->createModel(); |
176
|
|
|
|
177
|
|
|
// Fire the created event |
178
|
|
|
$this->getContainer('events')->fire($this->getRepositoryId().'.entity.creating', [$this, $instance]); |
179
|
|
|
|
180
|
|
|
// Fill instance with data |
181
|
|
|
$instance->fill($attributes); |
182
|
|
|
|
183
|
|
|
// Save the instance |
184
|
|
|
$created = $instance->save(); |
185
|
|
|
|
186
|
|
|
// Fire the created event |
187
|
|
|
$this->getContainer('events')->fire($this->getRepositoryId().'.entity.created', [$this, $instance]); |
188
|
|
|
|
189
|
|
|
// Return instance |
190
|
|
|
return $created ? $instance : $created; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* {@inheritdoc} |
195
|
|
|
*/ |
196
|
|
|
public function update($id, array $attributes = []) |
197
|
|
|
{ |
198
|
|
|
// Find the given instance |
199
|
|
|
$instance = $id instanceof Model ? $id : $this->find($id); |
200
|
|
|
|
201
|
|
|
if ($instance) { |
202
|
|
|
// Fire the updated event |
203
|
|
|
$this->getContainer('events')->fire($this->getRepositoryId().'.entity.updating', [$this, $instance]); |
204
|
|
|
|
205
|
|
|
// Fill instance with data |
206
|
|
|
$instance->fill($attributes); |
207
|
|
|
|
208
|
|
|
// Update the instance |
209
|
|
|
$updated = $instance->save(); |
210
|
|
|
|
211
|
|
|
// Fire the updated event |
212
|
|
|
$this->getContainer('events')->fire($this->getRepositoryId().'.entity.updated', [$this, $instance]); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $updated ? $instance : $updated; |
|
|
|
|
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* {@inheritdoc} |
220
|
|
|
*/ |
221
|
|
|
public function delete($id) |
222
|
|
|
{ |
223
|
|
|
// Find the given instance |
224
|
|
|
$deleted = false; |
225
|
|
|
|
226
|
|
|
// Find the given instance |
227
|
|
|
$instance = $id instanceof Model ? $id : $this->find($id); |
228
|
|
|
|
229
|
|
|
if ($instance) { |
230
|
|
|
// Fire the deleted event |
231
|
|
|
$this->getContainer('events')->fire($this->getRepositoryId().'.entity.deleting', [$this, $instance]); |
232
|
|
|
|
233
|
|
|
// Delete the instance |
234
|
|
|
$deleted = $instance->delete(); |
235
|
|
|
|
236
|
|
|
// Fire the deleted event |
237
|
|
|
$this->getContainer('events')->fire($this->getRepositoryId().'.entity.deleted', [$this, $instance]); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return $deleted ? $instance : $deleted; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
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.