1 | <?php |
||
2 | |||
3 | namespace Yeelight\Base\Repositories\Eloquent; |
||
4 | |||
5 | use Illuminate\Database\Eloquent\Model; |
||
6 | use Illuminate\Database\Eloquent\Relations\Relation; |
||
7 | use Prettus\Repository\Eloquent\BaseRepository; |
||
8 | use Prettus\Repository\Exceptions\RepositoryException; |
||
9 | use Prettus\Validator\Contracts\ValidatorInterface; |
||
10 | use Yeelight\Base\Presenters\Presenter; |
||
11 | use Yeelight\Base\Repositories\Interfaces\RepositoryInterface; |
||
12 | use Yeelight\Repositories\Criteria\AuthUserCriteria; |
||
13 | |||
14 | /** |
||
15 | * Class Repository |
||
16 | * |
||
17 | * @category Yeelight |
||
18 | * |
||
19 | * @package Yeelight\Base\Repositories\Eloquent |
||
20 | * |
||
21 | * @author Sheldon Lee <[email protected]> |
||
22 | * |
||
23 | * @license https://opensource.org/licenses/MIT MIT |
||
24 | * |
||
25 | * @link https://www.yeelight.com |
||
26 | */ |
||
27 | abstract class Repository extends BaseRepository implements RepositoryInterface |
||
28 | { |
||
29 | /** |
||
30 | * Is Searchable Force And Where |
||
31 | * |
||
32 | * @var bool |
||
33 | */ |
||
34 | protected $isSearchableForceAndWhere = false; |
||
35 | |||
36 | /** |
||
37 | * Relate Model |
||
38 | * |
||
39 | * @var Model |
||
40 | */ |
||
41 | protected $relateModel; |
||
42 | |||
43 | /** |
||
44 | * Relation |
||
45 | * |
||
46 | * @var Relation |
||
47 | */ |
||
48 | protected $relation; |
||
49 | |||
50 | /** |
||
51 | * ByAuthUser |
||
52 | * |
||
53 | * @return $this|RepositoryInterface |
||
54 | * @throws RepositoryException |
||
55 | */ |
||
56 | public function byAuthUser() |
||
57 | { |
||
58 | $this->pushCriteria(new AuthUserCriteria()); |
||
59 | |||
60 | return $this; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Get auth UserId |
||
65 | * |
||
66 | * @return mixed |
||
67 | */ |
||
68 | public function authUserId() |
||
69 | { |
||
70 | return current_auth_user()->user_id; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Validate Creater |
||
75 | * |
||
76 | * @param array $attributes attributes |
||
77 | * |
||
78 | * @throws \Prettus\Validator\Exceptions\ValidatorException |
||
79 | * |
||
80 | * @return mixed |
||
81 | */ |
||
82 | public function validateCreate(array $attributes) |
||
83 | { |
||
84 | if (!is_null($this->validator)) { |
||
85 | $this->validator->with($attributes) |
||
86 | ->passesOrFail(ValidatorInterface::RULE_CREATE); |
||
87 | } |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * Validate Updater |
||
92 | * |
||
93 | * @param array $attributes attributes |
||
94 | * |
||
95 | * @throws \Prettus\Validator\Exceptions\ValidatorException |
||
96 | * |
||
97 | * @return mixed |
||
98 | */ |
||
99 | public function validateUpdate(array $attributes) |
||
100 | { |
||
101 | if (!is_null($this->validator)) { |
||
102 | $this->validator->with($attributes) |
||
103 | ->passesOrFail(ValidatorInterface::RULE_UPDATE); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * SetValidator |
||
109 | * |
||
110 | * @param ValidatorInterface $validator validator |
||
111 | * |
||
112 | * @return $this |
||
113 | */ |
||
114 | public function setValidator($validator) |
||
115 | { |
||
116 | $this->validator = $validator; |
||
117 | |||
118 | return $this; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Present |
||
123 | * |
||
124 | * @param Object $results results |
||
125 | * |
||
126 | * @return mixed |
||
127 | */ |
||
128 | public function present($results) |
||
129 | { |
||
130 | return $this->parserResult($results); |
||
131 | } |
||
132 | |||
133 | /** |
||
134 | * SetRelateModel |
||
135 | * |
||
136 | * @param Model $relateModel relateModel |
||
137 | * |
||
138 | * @return $this |
||
139 | * @throws RepositoryException |
||
140 | */ |
||
141 | public function setRelateModel(Model $relateModel) |
||
142 | { |
||
143 | $this->relateModel = $relateModel; |
||
144 | if ($relateModel) { |
||
145 | $this->makeModel(); |
||
146 | } |
||
147 | |||
148 | return $this; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Relation |
||
153 | * |
||
154 | * @return \Prettus\Repository\Contracts\PresenterInterface |
||
155 | */ |
||
156 | public function relation() |
||
157 | { |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * MakeModel |
||
162 | * |
||
163 | * @return Model|Relation|mixed|void |
||
164 | * @throws RepositoryException |
||
165 | */ |
||
166 | public function makeModel() |
||
167 | { |
||
168 | $model = $this->relateModel ? $this->relation() : $this->app->make($this->model()); |
||
169 | |||
170 | if (!($model instanceof Model || $model instanceof Relation)) { |
||
171 | throw new RepositoryException('Class '.get_class($model).' must be an instance of Illuminate\\Database\\Eloquent\\Model'); |
||
172 | } |
||
173 | |||
174 | return $this->model = $model; |
||
0 ignored issues
–
show
|
|||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Retrieve data array for populate field select. |
||
179 | * |
||
180 | * @param string $column $column |
||
181 | * @param string|null $key $key |
||
182 | * |
||
183 | * @return \Illuminate\Support\Collection|array |
||
184 | * @throws RepositoryException |
||
185 | */ |
||
186 | public function lists($column, $key = null) |
||
187 | { |
||
188 | $this->applyCriteria(); |
||
189 | $this->applyScope(); |
||
190 | |||
191 | $lists = $this->model->lists($column, $key); |
||
192 | |||
193 | $this->resetModel(); |
||
194 | |||
195 | return $lists; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Retrieve all data of repository. |
||
200 | * |
||
201 | * @param array $columns columns |
||
202 | * |
||
203 | * @return mixed |
||
204 | * @throws RepositoryException |
||
205 | */ |
||
206 | public function all($columns = ['*']) |
||
207 | { |
||
208 | $this->applyCriteria(); |
||
209 | $this->applyScope(); |
||
210 | |||
211 | if ($this->model instanceof \Illuminate\Database\Eloquent\Builder |
||
212 | || $this->model instanceof \Illuminate\Database\Eloquent\Relations\Relation |
||
213 | ) { |
||
214 | $results = $this->model->get($columns); |
||
215 | } else { |
||
216 | $results = $this->model->all($columns); |
||
217 | } |
||
218 | |||
219 | $this->resetModel(); |
||
220 | |||
221 | return $this->parserResult($results); |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * Add a basic where clause to the model. |
||
226 | * |
||
227 | * @param string|array|\Closure $column $column |
||
228 | * @param mixed $value $value |
||
229 | * |
||
230 | * @return $this |
||
231 | */ |
||
232 | protected function modelWhere($column, $value = null) |
||
233 | { |
||
234 | $this->model = $this->model->where($column, $value); |
||
0 ignored issues
–
show
It seems like
$this->model->where($column, $value) of type Illuminate\Database\Eloquent\Builder is incompatible with the declared type Illuminate\Database\Eloquent\Model of property $model .
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..
Loading history...
|
|||
235 | |||
236 | return $this; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * GetPresenter |
||
241 | * |
||
242 | * @return \Prettus\Repository\Contracts\PresenterInterface |
||
243 | */ |
||
244 | public function getPresenter() |
||
245 | { |
||
246 | return $this->presenter; |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * SetPresenterMeta |
||
251 | * |
||
252 | * @param array $meta $meta |
||
253 | * |
||
254 | * @return mixed |
||
255 | */ |
||
256 | public function setPresenterMeta(array $meta) |
||
257 | { |
||
258 | if ($this->presenter instanceof Presenter) { |
||
259 | $this->presenter->setMeta($meta); |
||
260 | } |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * GetIsSearchableForceAndWhere |
||
265 | * |
||
266 | * @return bool |
||
267 | */ |
||
268 | public function getIsSearchableForceAndWhere() |
||
269 | { |
||
270 | return $this->isSearchableForceAndWhere; |
||
271 | } |
||
272 | |||
273 | /** |
||
274 | * Find data by where conditions. |
||
275 | * |
||
276 | * @param array $where $where |
||
277 | * |
||
278 | * @return $this |
||
279 | */ |
||
280 | public function where(array $where) |
||
281 | { |
||
282 | $this->applyCriteria(); |
||
283 | $this->applyScope(); |
||
284 | |||
285 | $this->applyConditions($where); |
||
286 | |||
287 | return $this; |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Retrieve first data of repository with fail if not found. |
||
292 | * |
||
293 | * @param array $columns columns |
||
294 | * |
||
295 | * @return mixed |
||
296 | * @throws RepositoryException |
||
297 | */ |
||
298 | public function firstOrFail($columns = ['*']) |
||
299 | { |
||
300 | $this->applyCriteria(); |
||
301 | $this->applyScope(); |
||
302 | |||
303 | $results = $this->model->firstOrFail($columns); |
||
304 | |||
305 | $this->resetModel(); |
||
306 | |||
307 | return $this->parserResult($results); |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Where first. |
||
312 | * |
||
313 | * @param array $where where |
||
314 | * @param array $columns columns |
||
315 | * |
||
316 | * @return mixed |
||
317 | * @throws RepositoryException |
||
318 | */ |
||
319 | public function whereFirst(array $where, $columns = ['*']) |
||
320 | { |
||
321 | return $this->where($where)->firstOrFail($columns = ['*']); |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * Use Model for custom usages. |
||
326 | * |
||
327 | * @param callable $callback callback |
||
328 | * |
||
329 | * @return $this |
||
330 | */ |
||
331 | public function useModel(callable $callback) |
||
332 | { |
||
333 | $this->model = $callback($this->model); |
||
334 | |||
335 | return $this; |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Remove all or passed registered global scopes. |
||
340 | * |
||
341 | * @param array|null $scopes scopes |
||
342 | * |
||
343 | * @return $this |
||
344 | */ |
||
345 | public function withoutGlobalScopes(array $scopes = null) |
||
346 | { |
||
347 | $this->model = $this->model->withoutGlobalScopes($scopes); |
||
0 ignored issues
–
show
It seems like
$this->model->withoutGlobalScopes($scopes) of type Illuminate\Database\Eloquent\Builder is incompatible with the declared type Illuminate\Database\Eloquent\Model of property $model .
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property. Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..
Loading history...
|
|||
348 | |||
349 | return $this; |
||
350 | } |
||
351 | } |
||
352 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.