1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Core\Repositories\Eloquent; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Modules\Core\Repositories\BaseRepository; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class EloquentCoreRepository |
10
|
|
|
* |
11
|
|
|
* @package Modules\Core\Repositories\Eloquent |
12
|
|
|
*/ |
13
|
|
|
abstract class EloquentBaseRepository implements BaseRepository |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var \Illuminate\Database\Eloquent\Model An instance of the Eloquent Model |
17
|
|
|
*/ |
18
|
|
|
protected $model; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Model $model |
22
|
|
|
*/ |
23
|
|
|
public function __construct($model) |
24
|
|
|
{ |
25
|
|
|
$this->model = $model; |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param int $id |
30
|
|
|
* @return object |
31
|
|
|
*/ |
32
|
|
|
public function find($id) |
33
|
|
|
{ |
34
|
|
|
if (method_exists($this->model, 'translations')) { |
35
|
|
|
return $this->model->with('translations')->find($id); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $this->model->find($id); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
public function all() |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
if (method_exists($this->model, 'translations')) { |
47
|
|
|
return $this->model->with('translations')->orderBy('created_at', 'DESC')->get(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $this->model->orderBy('created_at', 'DESC')->get(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return \Illuminate\Pagination\LengthAwarePaginator |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function paginate($perPage = 15) |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
if (method_exists($this->model, 'translations')) { |
59
|
|
|
return $this->model->with('translations')->orderBy('created_at', 'DESC')->paginate($perPage); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->model->orderBy('created_at', 'DESC')->paginate($perPage); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param mixed $data |
67
|
|
|
* @return object |
68
|
|
|
*/ |
69
|
|
|
public function create($data) |
70
|
|
|
{ |
71
|
|
|
return $this->model->create($data); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param $model |
76
|
|
|
* @param array $data |
77
|
|
|
* @return object |
78
|
|
|
*/ |
79
|
|
|
public function update($model, $data) |
80
|
|
|
{ |
81
|
|
|
$model->update($data); |
82
|
|
|
|
83
|
|
|
return $model; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param Model $model |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
public function destroy($model) |
91
|
|
|
{ |
92
|
|
|
return $model->delete(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Return all resources in the given language |
97
|
|
|
* |
98
|
|
|
* @param string $lang |
99
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
100
|
|
|
*/ |
101
|
|
|
public function allTranslatedIn($lang) |
102
|
|
|
{ |
103
|
|
|
return $this->model->whereHas('translations', function (Builder $q) use ($lang) { |
104
|
|
|
$q->where('locale', "$lang"); |
105
|
|
|
})->with('translations')->orderBy('created_at', 'DESC')->get(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Find a resource by the given slug |
110
|
|
|
* |
111
|
|
|
* @param string $slug |
112
|
|
|
* @return object |
113
|
|
|
*/ |
114
|
|
|
public function findBySlug($slug) |
115
|
|
|
{ |
116
|
|
|
if (method_exists($this->model, 'translations')) { |
117
|
|
|
return $this->model->whereHas('translations', function (Builder $q) use ($slug) { |
118
|
|
|
$q->where('slug', $slug); |
119
|
|
|
})->with('translations')->first(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->model->where('slug', $slug)->first(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Find a resource by an array of attributes |
127
|
|
|
* @param array $attributes |
128
|
|
|
* @return object |
129
|
|
|
*/ |
130
|
|
|
public function findByAttributes(array $attributes) |
131
|
|
|
{ |
132
|
|
|
$query = $this->buildQueryByAttributes($attributes); |
133
|
|
|
|
134
|
|
|
return $query->first(); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get resources by an array of attributes |
139
|
|
|
* @param array $attributes |
140
|
|
|
* @param null|string $orderBy |
141
|
|
|
* @param string $sortOrder |
142
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
143
|
|
|
*/ |
144
|
|
|
public function getByAttributes(array $attributes, $orderBy = null, $sortOrder = 'asc') |
145
|
|
|
{ |
146
|
|
|
$query = $this->buildQueryByAttributes($attributes, $orderBy, $sortOrder); |
147
|
|
|
|
148
|
|
|
return $query->get(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Build Query to catch resources by an array of attributes and params |
153
|
|
|
* @param array $attributes |
154
|
|
|
* @param null|string $orderBy |
155
|
|
|
* @param string $sortOrder |
156
|
|
|
* @return \Illuminate\Database\Query\Builder object |
157
|
|
|
*/ |
158
|
|
|
private function buildQueryByAttributes(array $attributes, $orderBy = null, $sortOrder = 'asc') |
159
|
|
|
{ |
160
|
|
|
$query = $this->model->query(); |
161
|
|
|
|
162
|
|
|
if (method_exists($this->model, 'translations')) { |
163
|
|
|
$query = $query->with('translations'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
foreach ($attributes as $field => $value) { |
167
|
|
|
$query = $query->where($field, $value); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
if (null !== $orderBy) { |
171
|
|
|
$query->orderBy($orderBy, $sortOrder); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return $query; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Return a collection of elements who's ids match |
179
|
|
|
* @param array $ids |
180
|
|
|
* @return mixed |
181
|
|
|
*/ |
182
|
|
|
public function findByMany(array $ids) |
183
|
|
|
{ |
184
|
|
|
$query = $this->model->query(); |
185
|
|
|
|
186
|
|
|
if (method_exists($this->model, 'translations')) { |
187
|
|
|
$query = $query->with('translations'); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
return $query->whereIn("id", $ids)->get(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Clear the cache for this Repositories' Entity |
195
|
|
|
* @return bool |
196
|
|
|
*/ |
197
|
|
|
public function clearCache() |
198
|
|
|
{ |
199
|
|
|
return true; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
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..