1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Hamza Alayed |
5
|
|
|
* Date: 11/29/18 |
6
|
|
|
* Time: 9:38 AM. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Shamaseen\Repository\Generator\Bases; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Illuminate\Container\Container as App; |
13
|
|
|
use Illuminate\Database\Eloquent\Builder; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Database. |
17
|
|
|
*/ |
18
|
|
|
abstract class Repository implements Contract |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
protected $with = []; |
21
|
|
|
/** |
22
|
|
|
* @var App |
23
|
|
|
*/ |
24
|
|
|
protected $app; |
25
|
|
|
|
26
|
|
|
/** @var string */ |
27
|
|
|
protected $order = null; |
28
|
|
|
|
29
|
|
|
protected $direction = 'desc'; |
30
|
|
|
/** |
31
|
|
|
* @var Entity |
32
|
|
|
*/ |
33
|
|
|
protected $model; |
34
|
|
|
/** |
35
|
|
|
* @var boolean |
36
|
|
|
*/ |
37
|
|
|
private $trash = false; |
38
|
|
|
/** |
39
|
|
|
* @var boolean |
40
|
|
|
*/ |
41
|
|
|
private $withTrash = false; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param App $app |
45
|
|
|
*/ |
46
|
|
|
public function __construct(App $app) |
47
|
|
|
{ |
48
|
|
|
$this->app = $app; |
49
|
|
|
$this->makeModel(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function makeModel() |
53
|
|
|
{ |
54
|
|
|
$this->model = $this->app->make($this->getModelClass()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
abstract protected function getModelClass(): string; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param int $limit |
64
|
|
|
* @param array $filters |
65
|
|
|
* |
66
|
|
|
* @return \Illuminate\Contracts\Pagination\Paginator |
67
|
|
|
*/ |
68
|
|
|
public function simplePaginate($limit = 10, $filters = []) |
69
|
|
|
{ |
70
|
|
|
return $this->filter($filters)->simplePaginate($limit); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param array $filters |
75
|
|
|
* @return Entity |
76
|
|
|
*/ |
77
|
|
|
public function filter($filters = []) |
78
|
|
|
{ |
79
|
|
|
if (isset($filters['order'])) { |
80
|
|
|
$this->order = $filters['order']; |
81
|
|
|
unset($filters['order']); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if (isset($filters['direction'])) { |
85
|
|
|
$this->direction = $filters['direction']; |
86
|
|
|
unset($filters['direction']); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** @var Entity $latest */ |
90
|
|
|
$latest = $this->model->with($this->with); |
91
|
|
|
if ('' != $this->order) { |
92
|
|
|
$latest->orderBy($this->order, $this->direction); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (isset($filters['search'])) { |
96
|
|
|
foreach ($this->model->searchable as $item) { |
|
|
|
|
97
|
|
|
$latest->where($item, 'like', '%' . $filters['search'] . '%', 'or'); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
unset($filters['search']); |
101
|
|
|
} |
102
|
|
|
unset($filters['page']); |
103
|
|
|
|
104
|
|
|
if ($this->trash) { |
105
|
|
|
$latest->onlyTrashed(); |
106
|
|
|
} |
107
|
|
|
if ($this->withTrash) { |
108
|
|
|
$latest->withTrashed(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $latest->where($filters); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param int $limit |
116
|
|
|
* @param array $filters |
117
|
|
|
* |
118
|
|
|
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator |
119
|
|
|
*/ |
120
|
|
|
public function paginate($limit = 10, $filters = []) |
121
|
|
|
{ |
122
|
|
|
return $this->filter($filters)->paginate($limit); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @param array $filters |
127
|
|
|
* |
128
|
|
|
* @return Builder[]|\Illuminate\Database\Eloquent\Collection |
129
|
|
|
*/ |
130
|
|
|
public function get($filters = []) |
131
|
|
|
{ |
132
|
|
|
return $this->filter($filters)->get(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param $entityId |
137
|
|
|
* @param array $attributes |
138
|
|
|
* |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
public function update($entityId = 0, $attributes = []) |
142
|
|
|
{ |
143
|
|
|
$item = $this->model->where('id', $entityId); |
144
|
|
|
|
145
|
|
|
if ($item) { |
146
|
|
|
return $item->update($attributes); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return false; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param $entityId |
154
|
|
|
* |
155
|
|
|
* @throws \Exception |
156
|
|
|
* |
157
|
|
|
* @return bool |
158
|
|
|
*/ |
159
|
|
|
public function delete($entityId = 0) |
160
|
|
|
{ |
161
|
|
|
$item = $this->model->where('id', $entityId); |
162
|
|
|
|
163
|
|
|
if ($item && $item->delete()) { |
|
|
|
|
164
|
|
|
return true; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return false; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param array $attributes |
172
|
|
|
* |
173
|
|
|
* @return bool |
174
|
|
|
*/ |
175
|
|
|
public function insert($attributes = []) |
176
|
|
|
{ |
177
|
|
|
return $this->model->insert($attributes); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param array $columns |
183
|
|
|
* |
184
|
|
|
* @return mixed |
185
|
|
|
*/ |
186
|
|
|
public function all($columns = ['*']) |
187
|
|
|
{ |
188
|
|
|
return $this->model->all($columns); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param string $name |
193
|
|
|
* @param string $entityId |
194
|
|
|
* @param array $filters |
195
|
|
|
* |
196
|
|
|
* @return array |
197
|
|
|
*/ |
198
|
|
|
public function pluck($name = 'name', $entityId = 'id', $filters = []) |
199
|
|
|
{ |
200
|
|
|
return $this->model->where($filters)->pluck($name, $entityId)->toArray(); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param $entityId |
205
|
|
|
* @param array $columns |
206
|
|
|
* |
207
|
|
|
* @return Entity |
208
|
|
|
*/ |
209
|
|
|
public function find($entityId = 0, $columns = ['*']) |
210
|
|
|
{ |
211
|
|
|
return $this->model->with($this->with)->select($columns)->where('id', $entityId)->first(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @param array $filter |
216
|
|
|
* @param array $columns |
217
|
|
|
* |
218
|
|
|
* @return Entity |
219
|
|
|
*/ |
220
|
|
|
public function first($filter = [], $columns = ['*']) |
221
|
|
|
{ |
222
|
|
|
return $this->model->with($this->with)->select($columns)->where($filter)->first(); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* @param $haystack |
227
|
|
|
* @param $needle |
228
|
|
|
* |
229
|
|
|
* @return Entity[]|\Illuminate\Database\Eloquent\Collection |
230
|
|
|
*/ |
231
|
|
|
public function search($haystack, $needle) |
232
|
|
|
{ |
233
|
|
|
return $this->model->where($haystack, 'like', $needle)->get(); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param $filters |
239
|
|
|
* @param array $columns |
240
|
|
|
* |
241
|
|
|
* @return Entity |
242
|
|
|
*/ |
243
|
|
|
public function findBy($filters = [], $columns = ['*']) |
244
|
|
|
{ |
245
|
|
|
return $this->model->with($this->with)->select($columns)->where($filters)->first(); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @param array $attributes |
250
|
|
|
* |
251
|
|
|
* @return Entity|\Illuminate\Database\Eloquent\Model |
252
|
|
|
*/ |
253
|
|
|
public function create($attributes = []) |
254
|
|
|
{ |
255
|
|
|
return $this->model->create($attributes); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @param array $attributes |
260
|
|
|
* |
261
|
|
|
* @return Entity|\Illuminate\Database\Eloquent\Model |
262
|
|
|
*/ |
263
|
|
|
public function createOrUpdate($attributes = []) |
264
|
|
|
{ |
265
|
|
|
return $this->model->updateOrCreate($attributes); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @param array $data |
270
|
|
|
* |
271
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
272
|
|
|
*/ |
273
|
|
|
public function createOrFirst($data = []) |
274
|
|
|
{ |
275
|
|
|
return $this->model->firstOrCreate($data); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Get entity name |
280
|
|
|
* |
281
|
|
|
* @return string |
282
|
|
|
*/ |
283
|
|
|
public function entityName() |
284
|
|
|
{ |
285
|
|
|
return $this->getModelClass(); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @param int $entityId |
290
|
|
|
* |
291
|
|
|
* @return bool|null |
292
|
|
|
*/ |
293
|
|
|
public function restore($entityId = 0) |
294
|
|
|
{ |
295
|
|
|
/** @var Entity $entity */ |
296
|
|
|
$entity = $this->model->withTrashed() |
297
|
|
|
->whereId($entityId) |
298
|
|
|
->first(); |
299
|
|
|
if ($entity) { |
300
|
|
|
return $entity->restore(); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
return false; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @param int $entityId |
308
|
|
|
* |
309
|
|
|
* @return bool|null |
310
|
|
|
*/ |
311
|
|
|
public function forceDelete($entityId = 0) |
312
|
|
|
{ |
313
|
|
|
/** @var Entity $entity */ |
314
|
|
|
$entity = $this->model->withTrashed() |
315
|
|
|
->whereId($entityId) |
316
|
|
|
->first(); |
317
|
|
|
if ($entity) { |
318
|
|
|
return $entity->forceDelete(); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
return false; |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** |
325
|
|
|
* @return void |
326
|
|
|
*/ |
327
|
|
|
public function trash() |
328
|
|
|
{ |
329
|
|
|
$this->trash = true; |
330
|
|
|
$this->withTrash = false; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* @return void |
335
|
|
|
*/ |
336
|
|
|
public function withTrash() |
337
|
|
|
{ |
338
|
|
|
$this->trash = false; |
339
|
|
|
$this->withTrash = true; |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.