|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Modules\Core\BaseClasses; |
|
4
|
|
|
|
|
5
|
|
|
use App\Modules\Core\Interfaces\BaseServiceInterface; |
|
6
|
|
|
|
|
7
|
|
|
abstract class BaseService implements BaseServiceInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var object |
|
11
|
|
|
*/ |
|
12
|
|
|
public $repo; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Init new object. |
|
16
|
|
|
* |
|
17
|
|
|
* @param mixed $repo |
|
18
|
|
|
* @return void |
|
|
|
|
|
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct($repo) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->repo = $repo; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Fetch records with relations based on the given params. |
|
27
|
|
|
* |
|
28
|
|
|
* @param array $relations |
|
29
|
|
|
* @param array $conditions |
|
30
|
|
|
* @param integer $perPage |
|
31
|
|
|
* @param string $sortBy |
|
32
|
|
|
* @param boolean $desc |
|
33
|
|
|
* @param boolean $trashed |
|
34
|
|
|
* @return collection |
|
35
|
|
|
*/ |
|
36
|
|
|
public function list($relations = [], $conditions = false, $perPage = 15, $sortBy = 'created_at', $desc = true, $trashed = false) |
|
37
|
|
|
{ |
|
38
|
|
|
$translatable = $this->repo->model->translatable ?? []; |
|
39
|
|
|
$filters = $this->constructFilters($conditions); |
|
|
|
|
|
|
40
|
|
|
$sortBy = in_array($sortBy, $translatable) ? $sortBy . '->' . \Session::get('locale') : $sortBy; |
|
41
|
|
|
|
|
42
|
|
|
if ($trashed) { |
|
43
|
|
|
return $this->deleted(['and' => $filters], $perPage ?? 15, $sortBy ?? 'created_at', $desc ?? true); |
|
|
|
|
|
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if (count($filters)) { |
|
47
|
|
|
return $this->paginateBy(['and' => $filters], $perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $this->paginate($perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Fetch all records with relations from the storage. |
|
55
|
|
|
* |
|
56
|
|
|
* @param array $relations |
|
57
|
|
|
* @param string $sortBy |
|
58
|
|
|
* @param boolean $desc |
|
59
|
|
|
* @param array $columns |
|
60
|
|
|
* @return collection |
|
61
|
|
|
*/ |
|
62
|
|
|
public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->repo->all($relations, $sortBy, $desc, $columns); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Fetch all records with relations from storage in pages. |
|
69
|
|
|
* |
|
70
|
|
|
* @param integer $perPage |
|
71
|
|
|
* @param array $relations |
|
72
|
|
|
* @param string $sortBy |
|
73
|
|
|
* @param boolean $desc |
|
74
|
|
|
* @param array $columns |
|
75
|
|
|
* @return collection |
|
76
|
|
|
*/ |
|
77
|
|
|
public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->repo->paginate($perPage, $relations, $sortBy, $desc, $columns); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Fetch all records with relations based on |
|
84
|
|
|
* the given condition from storage in pages. |
|
85
|
|
|
* |
|
86
|
|
|
* @param array $conditions array of conditions |
|
87
|
|
|
* @param integer $perPage |
|
88
|
|
|
* @param array $relations |
|
89
|
|
|
* @param string $sortBy |
|
90
|
|
|
* @param boolean $desc |
|
91
|
|
|
* @param array $columns |
|
92
|
|
|
* @return collection |
|
93
|
|
|
*/ |
|
94
|
|
|
public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->repo->paginateBy($conditions, $perPage, $relations, $sortBy, $desc, $columns); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Save the given model to the storage. |
|
101
|
|
|
* |
|
102
|
|
|
* @param array $data |
|
103
|
|
|
* @return mixed |
|
104
|
|
|
*/ |
|
105
|
|
|
public function save(array $data) |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->repo->save($data); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Delete record from the storage based on the given |
|
112
|
|
|
* condition. |
|
113
|
|
|
* |
|
114
|
|
|
* @param var $value condition value |
|
115
|
|
|
* @param string $attribute condition column name |
|
116
|
|
|
* @return void |
|
117
|
|
|
*/ |
|
118
|
|
|
public function delete($value, $attribute = 'id') |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->repo->delete($value, $attribute); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Fetch records from the storage based on the given |
|
125
|
|
|
* id. |
|
126
|
|
|
* |
|
127
|
|
|
* @param integer $id |
|
128
|
|
|
* @param array $relations |
|
129
|
|
|
* @param array $columns |
|
130
|
|
|
* @return object |
|
131
|
|
|
*/ |
|
132
|
|
|
public function find($id, $relations = [], $columns = ['*']) |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->repo->find($id, $relations, $columns); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Fetch records from the storage based on the given |
|
139
|
|
|
* condition. |
|
140
|
|
|
* |
|
141
|
|
|
* @param array $conditions array of conditions |
|
142
|
|
|
* @param array $relations |
|
143
|
|
|
* @param string $sortBy |
|
144
|
|
|
* @param boolean $desc |
|
145
|
|
|
* @param array $columns |
|
146
|
|
|
* @return collection |
|
147
|
|
|
*/ |
|
148
|
|
|
public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
149
|
|
|
{ |
|
150
|
|
|
return $this->repo->findBy($conditions, $relations, $sortBy, $desc, $columns); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Fetch the first record from the storage based on the given |
|
155
|
|
|
* condition. |
|
156
|
|
|
* |
|
157
|
|
|
* @param array $conditions array of conditions |
|
158
|
|
|
* @param array $relations |
|
159
|
|
|
* @param array $columns |
|
160
|
|
|
* @return object |
|
161
|
|
|
*/ |
|
162
|
|
|
public function first($conditions, $relations = [], $columns = ['*']) |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->repo->first($conditions, $relations, $columns); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Return the deleted models in pages based on the given conditions. |
|
169
|
|
|
* |
|
170
|
|
|
* @param array $conditions array of conditions |
|
171
|
|
|
* @param integer $perPage |
|
172
|
|
|
* @param string $sortBy |
|
173
|
|
|
* @param boolean $desc |
|
174
|
|
|
* @param array $columns |
|
175
|
|
|
* @return collection |
|
176
|
|
|
*/ |
|
177
|
|
|
public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
178
|
|
|
{ |
|
179
|
|
|
return $this->repo->deleted($conditions, $perPage, $sortBy, $desc, $columns); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Restore the deleted model. |
|
184
|
|
|
* |
|
185
|
|
|
* @param integer $id |
|
186
|
|
|
* @return void |
|
187
|
|
|
*/ |
|
188
|
|
|
public function restore($id) |
|
189
|
|
|
{ |
|
190
|
|
|
return $this->repo->restore($id); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Prepare filters for repo. |
|
195
|
|
|
* |
|
196
|
|
|
* @param array $conditions |
|
197
|
|
|
* @return array |
|
198
|
|
|
*/ |
|
199
|
|
|
public function constructFilters($conditions) |
|
200
|
|
|
{ |
|
201
|
|
|
$filters = []; |
|
202
|
|
|
$translatable = $this->repo->model->translatable ?? []; |
|
203
|
|
|
foreach ($conditions as $key => $value) { |
|
204
|
|
|
if ((in_array($key, $this->repo->model->fillable ?? []) || method_exists($this->repo->model, $key) || in_array($key, ['or', 'and'])) && $key !== 'trashed') { |
|
205
|
|
|
$key = in_array($key, $translatable) ? $key . '->' . (\Session::get('locale') == 'all' ? 'en' : \Session::get('locale')) : $key; |
|
206
|
|
|
if (filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null && strpos($key, '_id') === false && ! is_null($value)) { |
|
207
|
|
|
$filters[$key] = filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
208
|
|
|
} elseif (! is_array($value) && strpos($key, '_id')) { |
|
209
|
|
|
$filters[$key] = [ |
|
210
|
|
|
'op' => 'in', |
|
211
|
|
|
'val' => explode(',', $value) |
|
212
|
|
|
]; |
|
213
|
|
|
} elseif (is_array($value)) { |
|
214
|
|
|
$filters[$key] = $value; |
|
215
|
|
|
} elseif($value) { |
|
216
|
|
|
$key = 'LOWER(' . $key . ')'; |
|
217
|
|
|
$value = strtolower($value); |
|
218
|
|
|
$filters[$key] = [ |
|
219
|
|
|
'op' => 'like', |
|
220
|
|
|
'val' => '%' . $value . '%' |
|
221
|
|
|
]; |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
return $filters; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.