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 |
View Code Duplication |
public function paginate($criteria = [], $perPage = null, $columns = ['*'], $pageName = 'page', $page = null) |
|
|
|
|
60
|
|
|
{ |
61
|
3 |
|
$model = $this->match($criteria); |
62
|
3 |
|
$perPage = $perPage ?: $this->perPage; |
63
|
|
|
|
64
|
3 |
|
return $model->paginate($perPage, $columns, $pageName, $page); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* simplePaginate. |
69
|
|
|
* |
70
|
|
|
* @param mixed $criteria |
71
|
|
|
* @param string $perPage |
72
|
|
|
* @param int $pageName |
73
|
|
|
* @param int $page |
74
|
|
|
* @return \Illuminate\Contracts\Pagination\Paginator |
75
|
|
|
*/ |
76
|
1 |
View Code Duplication |
public function simplePaginate($criteria = [], $perPage = null, $columns = ['*'], $pageName = 'page', $page = null) |
|
|
|
|
77
|
|
|
{ |
78
|
1 |
|
$model = $this->match($criteria); |
79
|
1 |
|
$perPage = $perPage ?: $this->perPage; |
80
|
|
|
|
81
|
1 |
|
return $model->simplePaginate($perPage, $columns, $pageName, $page); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* first. |
86
|
|
|
* |
87
|
|
|
* @param \Recca0120\Repository\Criteria|array $criteria |
88
|
|
|
* @return mixed |
89
|
|
|
*/ |
90
|
2 |
|
public function first($criteria = [], $columns = ['*']) |
91
|
|
|
{ |
92
|
2 |
|
$model = $this->match($criteria); |
93
|
|
|
|
94
|
2 |
|
return $model->first($columns); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* find. |
99
|
|
|
* |
100
|
|
|
* @param int $id |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
4 |
View Code Duplication |
public function find($id, $columns = ['*']) |
|
|
|
|
104
|
|
|
{ |
105
|
4 |
|
$model = $this->cloneModel(); |
106
|
4 |
|
$model = ($model instanceof Model) ? $model : $model->getModel(); |
107
|
|
|
|
108
|
4 |
|
return $model->find($id); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* newInstance. |
113
|
|
|
* |
114
|
|
|
* @param array $attributes |
115
|
|
|
* @return \Illuminate\Database\Eloquent |
116
|
|
|
*/ |
117
|
4 |
View Code Duplication |
public function newInstance($attributes = []) |
|
|
|
|
118
|
|
|
{ |
119
|
4 |
|
$model = $this->cloneModel(); |
120
|
4 |
|
$model = ($model instanceof Model) ? $model : $model->getModel(); |
121
|
|
|
|
122
|
4 |
|
return $model->forceFill($attributes); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* create. |
127
|
|
|
* |
128
|
|
|
* @param array $attributes |
129
|
|
|
* @param bool $forceFill |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
2 |
View Code Duplication |
public function create($attributes, $forceFill = false) |
|
|
|
|
133
|
|
|
{ |
134
|
2 |
|
$model = $this->newInstance(); |
135
|
2 |
|
$model = ($forceFill === false) ? $model->fill($attributes) : $model->forceFill($attributes); |
136
|
2 |
|
$model->save(); |
137
|
|
|
|
138
|
2 |
|
return $model; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* update. |
143
|
|
|
* |
144
|
|
|
* @param array $attributes |
145
|
|
|
* @param int $id |
146
|
|
|
* @param bool $forceFill |
147
|
|
|
* @return mixed |
148
|
|
|
*/ |
149
|
2 |
View Code Duplication |
public function update($attributes, $id, $forceFill = false) |
|
|
|
|
150
|
|
|
{ |
151
|
2 |
|
$model = $this->find($id); |
152
|
2 |
|
$model = ($forceFill === false) ? $model->fill($attributes) : $model->forceFill($attributes); |
153
|
2 |
|
$model->save(); |
154
|
|
|
|
155
|
2 |
|
return $model; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* delete. |
160
|
|
|
* |
161
|
|
|
* @param int $id |
162
|
|
|
* @return bool |
163
|
|
|
*/ |
164
|
1 |
|
public function delete($id) |
165
|
|
|
{ |
166
|
1 |
|
return $this->find($id)->delete(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* destroy. |
171
|
|
|
* |
172
|
|
|
* @param \Recca0120\Repository\Criteria|array $criteria |
173
|
|
|
* @return int |
174
|
|
|
*/ |
175
|
|
|
public function destroy($criteria = []) |
176
|
|
|
{ |
177
|
|
|
$model = $this->match($criteria); |
178
|
|
|
|
179
|
|
|
return $model->delete(); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* chunk. |
184
|
|
|
* |
185
|
|
|
* @param Criteria|array $criteria |
186
|
|
|
* @param int $count |
187
|
|
|
* @param callable $callback |
188
|
|
|
* @return \Illuminate\Support\Collection |
189
|
|
|
*/ |
190
|
2 |
|
public function chunk($criteria, $count, callable $callback) |
191
|
|
|
{ |
192
|
2 |
|
$model = $this->match($criteria); |
193
|
|
|
|
194
|
2 |
|
return $model->chunk($count, $callback); |
|
|
|
|
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.