1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ablunier\Laravel\Database\Repository\Eloquent; |
4
|
|
|
|
5
|
|
|
use Ablunier\Laravel\Database\Contracts\Repository\Criteria; |
6
|
|
|
use Ablunier\Laravel\Database\Contracts\Repository\CriteriaPerformer; |
7
|
|
|
use Ablunier\Laravel\Database\Contracts\Repository\Repository as RepositoryContract; |
8
|
|
|
use Ablunier\Laravel\Database\Repository\Eloquent\Criteria\WithCriteria; |
9
|
|
|
use Ablunier\Laravel\Database\Repository\Exceptions\RepositoryException; |
10
|
|
|
use Illuminate\Database\Eloquent\Model as EloquentModel; |
11
|
|
|
use Illuminate\Support\Collection; |
12
|
|
|
|
13
|
|
|
class Repository implements RepositoryContract, CriteriaPerformer |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var EloquentModel |
17
|
|
|
*/ |
18
|
|
|
protected $model; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Collection |
22
|
|
|
*/ |
23
|
|
|
protected $criteria; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
protected $skipCriteria = false; |
29
|
|
|
|
30
|
|
|
public function __construct(EloquentModel $model) |
31
|
17 |
|
{ |
32
|
|
|
$this->model = $model; |
33
|
17 |
|
$this->criteria = new Collection(); |
34
|
17 |
|
} |
35
|
17 |
|
|
36
|
|
|
/** |
37
|
|
|
* @return EloquentModel |
38
|
|
|
*/ |
39
|
|
|
public function getModel() |
40
|
1 |
|
{ |
41
|
|
|
return $this->model; |
42
|
1 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param array $with |
46
|
|
|
* |
47
|
|
|
* @return Collection |
48
|
|
|
*/ |
49
|
1 |
|
public function all(array $with = []) |
50
|
|
|
{ |
51
|
1 |
|
$this->addWithCriteria($with); |
52
|
1 |
|
$this->applyCriteria(); |
53
|
|
|
|
54
|
1 |
|
$result = $this->model->get(); |
55
|
|
|
|
56
|
1 |
|
$this->refresh(); |
57
|
|
|
|
58
|
1 |
|
return $result; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param int $perPage |
63
|
|
|
* |
64
|
|
|
* @return mixed |
65
|
1 |
|
*/ |
66
|
|
|
public function paginate($perPage = 15, array $with = []) |
67
|
1 |
|
{ |
68
|
1 |
|
$this->addWithCriteria($with); |
69
|
|
|
$this->applyCriteria(); |
70
|
1 |
|
|
71
|
|
|
$result = $this->model->paginate($perPage); |
72
|
1 |
|
|
73
|
|
|
$this->refresh(); |
74
|
1 |
|
|
75
|
|
|
return $result; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param array $data |
80
|
|
|
* |
81
|
7 |
|
* @return EloquentModel |
82
|
|
|
*/ |
83
|
7 |
|
public function create(array $data) |
84
|
|
|
{ |
85
|
7 |
|
$result = $this->model->create($data); |
|
|
|
|
86
|
|
|
|
87
|
7 |
|
$this->refresh(); |
88
|
|
|
|
89
|
|
|
return $result; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array $data |
94
|
|
|
* @param $id |
95
|
|
|
* @param string $field |
96
|
2 |
|
* |
97
|
|
|
* @return EloquentModel |
98
|
2 |
|
*/ |
99
|
|
|
public function update(array $data, $id, $field = 'id') |
100
|
2 |
|
{ |
101
|
|
|
$result = $this->model->where($field, '=', $id)->update($data); |
102
|
2 |
|
|
103
|
1 |
|
$this->refresh(); |
104
|
|
|
|
105
|
|
|
if (!$result) { |
106
|
1 |
|
throw new RepositoryException('There was an error updating the model'); |
107
|
|
|
} |
108
|
1 |
|
|
109
|
|
|
$model = $this->find($id); |
110
|
|
|
|
111
|
|
|
return $model; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
3 |
|
* @param $id |
116
|
|
|
* |
117
|
3 |
|
* @return int |
118
|
|
|
*/ |
119
|
3 |
|
public function delete($id) |
120
|
|
|
{ |
121
|
3 |
|
$result = $this->model->destroy($id); |
122
|
|
|
|
123
|
|
|
$this->refresh(); |
124
|
|
|
|
125
|
|
|
return $result; |
126
|
|
|
} |
127
|
|
|
|
128
|
3 |
|
/** |
129
|
|
|
* @param $id |
130
|
3 |
|
* |
131
|
3 |
|
* @return mixed |
132
|
|
|
*/ |
133
|
3 |
|
public function find($id, array $with = []) |
|
|
|
|
134
|
|
|
{ |
135
|
3 |
|
$this->addWithCriteria($with); |
136
|
|
|
$this->applyCriteria(); |
137
|
3 |
|
|
138
|
|
|
$result = $this->model->find($id); |
139
|
|
|
|
140
|
|
|
$this->refresh(); |
141
|
|
|
|
142
|
|
|
return $result; |
143
|
|
|
} |
144
|
1 |
|
|
145
|
|
|
/** |
146
|
1 |
|
* @param $id |
147
|
1 |
|
* |
148
|
|
|
* @return mixed|Exception |
149
|
1 |
|
*/ |
150
|
|
|
public function findOrFail($id, array $with = []) |
151
|
1 |
|
{ |
152
|
|
|
$this->addWithCriteria($with); |
153
|
1 |
|
$this->applyCriteria(); |
154
|
|
|
|
155
|
|
|
$result = $this->model->findOrFail($id); |
156
|
|
|
|
157
|
|
|
$this->refresh(); |
158
|
|
|
|
159
|
|
|
return $result; |
160
|
|
|
} |
161
|
1 |
|
|
162
|
|
|
/** |
163
|
1 |
|
* @param $field |
164
|
1 |
|
* @param $value |
165
|
|
|
* |
166
|
1 |
|
* @return mixed |
167
|
|
|
*/ |
168
|
1 |
|
public function findBy($field, $value, array $with = []) |
169
|
|
|
{ |
170
|
1 |
|
$this->addWithCriteria($with); |
171
|
|
|
$this->applyCriteria(); |
172
|
|
|
|
173
|
|
|
$result = $this->model->where($field, '=', $value)->first(); |
174
|
|
|
|
175
|
|
|
$this->refresh(); |
176
|
|
|
|
177
|
|
|
return $result; |
178
|
1 |
|
} |
179
|
|
|
|
180
|
1 |
|
/** |
181
|
1 |
|
* @param $field |
182
|
|
|
* @param $value |
183
|
1 |
|
* |
184
|
|
|
* @return mixed |
185
|
1 |
|
*/ |
186
|
|
|
public function findByOrFail($field, $value, array $with = []) |
187
|
1 |
|
{ |
188
|
|
|
$this->addWithCriteria($with); |
189
|
|
|
$this->applyCriteria(); |
190
|
|
|
|
191
|
|
|
$result = $this->model->where($field, '=', $value)->firstOrFail(); |
192
|
|
|
|
193
|
|
|
$this->refresh(); |
194
|
|
|
|
195
|
1 |
|
return $result; |
196
|
|
|
} |
197
|
1 |
|
|
198
|
1 |
|
/** |
199
|
|
|
* @param $field |
200
|
1 |
|
* @param $value |
201
|
|
|
* |
202
|
1 |
|
* @return mixed |
203
|
|
|
*/ |
204
|
1 |
|
public function findAllBy($field, $value, array $with = []) |
|
|
|
|
205
|
|
|
{ |
206
|
|
|
$this->addWithCriteria($with); |
207
|
|
|
$this->applyCriteria(); |
208
|
|
|
|
209
|
|
|
$result = $this->model->where($field, '=', $value)->get(); |
210
|
|
|
|
211
|
9 |
|
$this->refresh(); |
212
|
|
|
|
213
|
9 |
|
return $result; |
214
|
|
|
} |
215
|
|
|
|
216
|
9 |
|
/** |
217
|
|
|
* @param array $with |
218
|
|
|
* |
219
|
|
|
* @return mixed |
220
|
|
|
*/ |
221
|
11 |
|
public function first(array $with = []) |
222
|
|
|
{ |
223
|
11 |
|
$this->addWithCriteria($with); |
224
|
|
|
$this->applyCriteria(); |
225
|
|
|
|
226
|
|
|
$result = $this->model->first(); |
227
|
11 |
|
|
228
|
11 |
|
$this->refresh(); |
229
|
11 |
|
|
230
|
|
|
return $result; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @param array $with |
235
|
|
|
* |
236
|
|
|
* @return mixed |
237
|
|
|
*/ |
238
|
|
|
public function firstOrFail(array $with = []) |
239
|
|
|
{ |
240
|
|
|
$this->addWithCriteria($with); |
241
|
|
|
$this->applyCriteria(); |
242
|
|
|
|
243
|
|
|
$result = $this->model->firstOrFail(); |
244
|
|
|
|
245
|
9 |
|
$this->refresh(); |
246
|
|
|
|
247
|
9 |
|
return $result; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
/** |
251
|
|
|
* @param array $with |
252
|
|
|
* |
253
|
|
|
* @return void |
254
|
|
|
*/ |
255
|
|
|
protected function addWithCriteria(array $with = []) |
256
|
|
|
{ |
257
|
|
|
if (count($with) > 0) { |
258
|
|
|
$this->pushCriteria(new WithCriteria($with)); |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
protected function refresh() |
263
|
|
|
{ |
264
|
|
|
if (!$this->model instanceof EloquentModel) { |
265
|
|
|
$this->model = $this->model->getModel(); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
$this->model = $this->model->newInstance(); |
269
|
|
|
$this->criteria = new Collection(); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @param bool $status |
274
|
|
|
* |
275
|
9 |
|
* @return $this |
276
|
|
|
*/ |
277
|
9 |
|
public function skipCriteria($status = true) |
278
|
|
|
{ |
279
|
|
|
$this->skipCriteria = $status; |
280
|
|
|
|
281
|
9 |
|
return $this; |
282
|
|
|
} |
283
|
9 |
|
|
284
|
|
|
/** |
285
|
9 |
|
* @return Collection |
286
|
|
|
*/ |
287
|
|
|
public function getCriteria() |
288
|
|
|
{ |
289
|
|
|
return $this->criteria; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* @param Criteria $criteria |
294
|
|
|
* |
295
|
|
|
* @return $this |
296
|
|
|
*/ |
297
|
|
|
public function getByCriteria(Criteria $criteria) |
298
|
|
|
{ |
299
|
|
|
$this->model = $criteria->apply($this->model, $this); |
300
|
|
|
|
301
|
|
|
return $this; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @param Criteria $criteria |
306
|
|
|
* |
307
|
|
|
* @return $this |
308
|
|
|
*/ |
309
|
|
|
public function pushCriteria(Criteria $criteria) |
310
|
|
|
{ |
311
|
|
|
$this->criteria->push($criteria); |
312
|
|
|
|
313
|
|
|
return $this; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @return $this |
318
|
|
|
*/ |
319
|
|
|
public function applyCriteria() |
320
|
|
|
{ |
321
|
|
|
if ($this->skipCriteria === true) { |
322
|
|
|
return $this; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
foreach ($this->getCriteria() as $criteria) { |
326
|
|
|
$this->model = $criteria->apply($this->model, $this); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
return $this; |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.