|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Core; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Query\Builder; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
8
|
|
|
use Illuminate\Contracts\Container\Container; |
|
9
|
|
|
|
|
10
|
|
|
abstract class AbstractRepository |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var Container |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $app; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var Model |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $model; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Builder |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $workingModel; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* AbstractRepository constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param Container $app |
|
31
|
|
|
* @param Model $model |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct(Container $app, Model $model) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->app = $app; |
|
36
|
|
|
$this->model = $model; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Find a model by its primary key. If none found, throw an exception |
|
41
|
|
|
* |
|
42
|
|
|
* @param mixed $id |
|
43
|
|
|
* @param array $columns |
|
44
|
|
|
* |
|
45
|
|
|
* @return Collection|Model |
|
46
|
|
|
*/ |
|
47
|
|
|
public function findOrFail($id, array $columns = ['*']) |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->getWorkingModel()->findOrFail($id, $columns); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Find a model by its primary key |
|
54
|
|
|
* |
|
55
|
|
|
* @param mixed $id |
|
56
|
|
|
* @param array $columns |
|
57
|
|
|
* |
|
58
|
|
|
* @return Collection|Model |
|
59
|
|
|
*/ |
|
60
|
|
|
public function find($id, array $columns = ['*']) |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->getWorkingModel()->find($id, $columns); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get model |
|
67
|
|
|
* |
|
68
|
|
|
* @return Model |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function getModel() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->model; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Create a new instance of model |
|
77
|
|
|
* |
|
78
|
|
|
* @param array $attributes |
|
79
|
|
|
* |
|
80
|
|
|
* @return Model |
|
81
|
|
|
*/ |
|
82
|
|
|
public function newInstance(array $attributes = []) |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->getModel()->newInstance($attributes); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Save a new model and return the instance. |
|
89
|
|
|
* |
|
90
|
|
|
* @param array $attributes |
|
91
|
|
|
* |
|
92
|
|
|
* @return Model |
|
93
|
|
|
*/ |
|
94
|
|
|
public function create(array $attributes = []) |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->getModel()->create($attributes); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Save a new model and return the instance. Allow mass-assignment. |
|
101
|
|
|
* |
|
102
|
|
|
* @param array $attributes |
|
103
|
|
|
* |
|
104
|
|
|
* @return Model |
|
105
|
|
|
*/ |
|
106
|
|
|
public function forceCreate(array $attributes = []) |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->getModel()->forceCreate($attributes); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Destroy the models for the given primary keys. |
|
113
|
|
|
* |
|
114
|
|
|
* @param array|int $ids |
|
115
|
|
|
* |
|
116
|
|
|
* @return bool |
|
117
|
|
|
*/ |
|
118
|
|
|
public function destroy($ids) |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->getWorkingModel()->destroy($ids); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Sets working model |
|
125
|
|
|
* |
|
126
|
|
|
* @param Builder $model |
|
127
|
|
|
*/ |
|
128
|
|
|
public function setWorkingModel(Builder $model) |
|
129
|
|
|
{ |
|
130
|
|
|
$this->workingModel = $model; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Clears working model |
|
135
|
|
|
*/ |
|
136
|
|
|
public function clearWorkingModel() |
|
137
|
|
|
{ |
|
138
|
|
|
$this->workingModel = null; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Get working model (if none set it will return model) |
|
143
|
|
|
* |
|
144
|
|
|
* @return Model |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function getWorkingModel() |
|
147
|
|
|
{ |
|
148
|
|
|
return $this->workingModel ?: $this->model; |
|
|
|
|
|
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
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.