1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Recca0120\Repository; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Recca0120\Repository\Compilers\EloquentCompiler; |
7
|
|
|
|
8
|
|
|
class EloquentRepository extends AbstractRepository |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* $converter. |
12
|
|
|
* |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $compiler = EloquentCompiler::class; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* __construct. |
19
|
|
|
* |
20
|
|
|
* @param \Illuminate\Database\Eloquent\Model $model |
21
|
|
|
*/ |
22
|
25 |
|
public function __construct(Model $model) |
23
|
|
|
{ |
24
|
25 |
|
$this->model = $model; |
25
|
25 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* get. |
29
|
|
|
* |
30
|
|
|
* @param \Recca0120\Repository\Criteria|array $criteria |
31
|
|
|
* @param int $limit |
32
|
|
|
* @param int $offset |
33
|
|
|
* @return \Illuminate\Support\Collection |
34
|
|
|
*/ |
35
|
5 |
View Code Duplication |
public function get($criteria = [], $columns = ['*'], $limit = null, $offset = null) |
36
|
|
|
{ |
37
|
5 |
|
$model = $this->match($criteria); |
38
|
|
|
|
39
|
5 |
|
if (is_null($limit) === false) { |
40
|
2 |
|
$model = $model->take($limit); |
41
|
2 |
|
} |
42
|
|
|
|
43
|
5 |
|
if (is_null($offset) === false) { |
44
|
2 |
|
$model = $model->skip($offset); |
45
|
2 |
|
} |
46
|
|
|
|
47
|
5 |
|
return $model->get($columns); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* paginate. |
52
|
|
|
* |
53
|
|
|
* @param mixed $criteria |
54
|
|
|
* @param string $perPage |
55
|
|
|
* @param int $pageName |
56
|
|
|
* @param int $page |
57
|
|
|
* @return \Illuminate\Contracts\Pagination\LengthAwarePaginator |
58
|
|
|
*/ |
59
|
3 |
|
public function paginate($criteria = [], $perPage = null, $columns = ['*'], $pageName = 'page', $page = null) |
60
|
|
|
{ |
61
|
3 |
|
$perPage = $perPage ?: $this->perPage; |
62
|
|
|
|
63
|
3 |
|
return $this->match($criteria)->paginate($perPage, $columns, $pageName, $page); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* simplePaginate. |
68
|
|
|
* |
69
|
|
|
* @param mixed $criteria |
70
|
|
|
* @param string $perPage |
71
|
|
|
* @param int $pageName |
72
|
|
|
* @param int $page |
73
|
|
|
* @return \Illuminate\Contracts\Pagination\Paginator |
74
|
|
|
*/ |
75
|
1 |
|
public function simplePaginate($criteria = [], $perPage = null, $columns = ['*'], $pageName = 'page', $page = null) |
76
|
|
|
{ |
77
|
1 |
|
$perPage = $perPage ?: $this->perPage; |
78
|
|
|
|
79
|
1 |
|
return $this->match($criteria)->simplePaginate($perPage, $columns, $pageName, $page); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* first. |
84
|
|
|
* |
85
|
|
|
* @param \Recca0120\Repository\Criteria|array $criteria |
86
|
|
|
* @return mixed |
87
|
|
|
*/ |
88
|
2 |
|
public function first($criteria = [], $columns = ['*']) |
89
|
|
|
{ |
90
|
2 |
|
return $this->match($criteria)->first($columns); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* find. |
95
|
|
|
* |
96
|
|
|
* @param int $id |
97
|
|
|
* @return mixed |
98
|
|
|
*/ |
99
|
4 |
View Code Duplication |
public function find($id, $columns = ['*']) |
100
|
|
|
{ |
101
|
4 |
|
$clone = $this->cloneModel(); |
102
|
4 |
|
$model = ($clone instanceof Model) ? $clone : $clone->getModel(); |
103
|
|
|
|
104
|
4 |
|
return $model->find($id); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* newInstance. |
109
|
|
|
* |
110
|
|
|
* @param array $attributes |
111
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
112
|
|
|
*/ |
113
|
4 |
View Code Duplication |
public function newInstance($attributes = []) |
114
|
|
|
{ |
115
|
4 |
|
$clone = $this->cloneModel(); |
116
|
4 |
|
$model = ($clone instanceof Model) ? $clone : $clone->getModel(); |
117
|
|
|
|
118
|
4 |
|
return $model->forceFill($attributes); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* create. |
123
|
|
|
* |
124
|
|
|
* @param array $attributes |
125
|
|
|
* @param bool $forceFill |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
2 |
View Code Duplication |
public function create($attributes, $forceFill = false) |
129
|
|
|
{ |
130
|
2 |
|
$model = $this->newInstance(); |
131
|
2 |
|
$model = ($forceFill === false) ? $model->fill($attributes) : $model->forceFill($attributes); |
132
|
2 |
|
$model->save(); |
133
|
|
|
|
134
|
2 |
|
return $model; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* update. |
139
|
|
|
* |
140
|
|
|
* @param array $attributes |
141
|
|
|
* @param int $id |
142
|
|
|
* @param bool $forceFill |
143
|
|
|
* @return mixed |
144
|
|
|
*/ |
145
|
2 |
View Code Duplication |
public function update($attributes, $id, $forceFill = false) |
146
|
|
|
{ |
147
|
2 |
|
$model = $this->find($id); |
148
|
2 |
|
$model = ($forceFill === false) ? $model->fill($attributes) : $model->forceFill($attributes); |
149
|
2 |
|
$model->save(); |
150
|
|
|
|
151
|
2 |
|
return $model; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* delete. |
156
|
|
|
* |
157
|
|
|
* @param int $id |
158
|
|
|
* @return bool |
159
|
|
|
*/ |
160
|
1 |
|
public function delete($id) |
161
|
|
|
{ |
162
|
1 |
|
return $this->find($id)->delete(); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* destroy. |
167
|
|
|
* |
168
|
|
|
* @param \Recca0120\Repository\Criteria|array $criteria |
169
|
|
|
* @return int |
170
|
|
|
*/ |
171
|
|
|
public function destroy($criteria = []) |
172
|
|
|
{ |
173
|
|
|
return $this->match($criteria)->delete(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* chunk. |
178
|
|
|
* |
179
|
|
|
* @param Criteria|array $criteria |
180
|
|
|
* @param int $count |
181
|
|
|
* @param callable $callback |
182
|
|
|
* @return \Illuminate\Support\Collection |
183
|
|
|
*/ |
184
|
2 |
|
public function chunk($criteria, $count, callable $callback) |
185
|
|
|
{ |
186
|
2 |
|
return $this->match($criteria)->chunk($count, $callback); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.