|
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
|
|
|
$filters = []; |
|
39
|
|
|
$translatable = $this->repo->model->translatable ?? []; |
|
40
|
|
|
foreach ($conditions as $key => $value) { |
|
|
|
|
|
|
41
|
|
|
if($value && in_array($key, $this->repo->model->fillable ?? [])) { |
|
42
|
|
|
$key = in_array($key, $translatable) ? $key . '->' . (\Session::get('locale') == 'all' ? 'en' : \Session::get('locale')) : $key; |
|
43
|
|
|
$isBool = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) !== null; |
|
44
|
|
|
$filters[$key] = $isBool ? filter_var($value, FILTER_VALIDATE_BOOLEAN) : $value; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
$sortBy = in_array($sortBy, $translatable) ? $sortBy . '->' . \Session::get('locale') : $sortBy; |
|
48
|
|
|
|
|
49
|
|
|
if ($trashed) { |
|
50
|
|
|
return $this->deleted(['and' => $filters], $perPage ?? 15, $sortBy ?? 'created_at', $desc ?? true); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if (count($filters)) { |
|
54
|
|
|
return $this->paginateBy(['and' => $filters], $perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $this->paginate($perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
|
|
|
|
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Fetch all records with relations from the storage. |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $relations |
|
64
|
|
|
* @param string $sortBy |
|
65
|
|
|
* @param boolean $desc |
|
66
|
|
|
* @param array $columns |
|
67
|
|
|
* @return collection |
|
68
|
|
|
*/ |
|
69
|
|
|
public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->repo->all($relations, $sortBy, $desc, $columns); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Fetch all records with relations from storage in pages. |
|
76
|
|
|
* |
|
77
|
|
|
* @param integer $perPage |
|
78
|
|
|
* @param array $relations |
|
79
|
|
|
* @param string $sortBy |
|
80
|
|
|
* @param boolean $desc |
|
81
|
|
|
* @param array $columns |
|
82
|
|
|
* @return collection |
|
83
|
|
|
*/ |
|
84
|
|
|
public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->repo->paginate($perPage, $relations, $sortBy, $desc, $columns); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Fetch all records with relations based on |
|
91
|
|
|
* the given condition from storage in pages. |
|
92
|
|
|
* |
|
93
|
|
|
* @param array $conditions array of conditions |
|
94
|
|
|
* @param integer $perPage |
|
95
|
|
|
* @param array $relations |
|
96
|
|
|
* @param string $sortBy |
|
97
|
|
|
* @param boolean $desc |
|
98
|
|
|
* @param array $columns |
|
99
|
|
|
* @return collection |
|
100
|
|
|
*/ |
|
101
|
|
|
public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->repo->paginateBy($conditions, $perPage, $relations, $sortBy, $desc, $columns); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Save the given model to the storage. |
|
108
|
|
|
* |
|
109
|
|
|
* @param array $data |
|
110
|
|
|
* @return mixed |
|
111
|
|
|
*/ |
|
112
|
|
|
public function save(array $data) |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->repo->save($data); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Delete record from the storage based on the given |
|
119
|
|
|
* condition. |
|
120
|
|
|
* |
|
121
|
|
|
* @param var $value condition value |
|
122
|
|
|
* @param string $attribute condition column name |
|
123
|
|
|
* @return void |
|
124
|
|
|
*/ |
|
125
|
|
|
public function delete($value, $attribute = 'id') |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->repo->delete($value, $attribute); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Fetch records from the storage based on the given |
|
132
|
|
|
* id. |
|
133
|
|
|
* |
|
134
|
|
|
* @param integer $id |
|
135
|
|
|
* @param array $relations |
|
136
|
|
|
* @param array $columns |
|
137
|
|
|
* @return object |
|
138
|
|
|
*/ |
|
139
|
|
|
public function find($id, $relations = [], $columns = ['*']) |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->repo->find($id, $relations, $columns); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Fetch records from the storage based on the given |
|
146
|
|
|
* condition. |
|
147
|
|
|
* |
|
148
|
|
|
* @param array $conditions array of conditions |
|
149
|
|
|
* @param array $relations |
|
150
|
|
|
* @param string $sortBy |
|
151
|
|
|
* @param boolean $desc |
|
152
|
|
|
* @param array $columns |
|
153
|
|
|
* @return collection |
|
154
|
|
|
*/ |
|
155
|
|
|
public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->repo->findBy($conditions, $relations, $sortBy, $desc, $columns); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Fetch the first record from the storage based on the given |
|
162
|
|
|
* condition. |
|
163
|
|
|
* |
|
164
|
|
|
* @param array $conditions array of conditions |
|
165
|
|
|
* @param array $relations |
|
166
|
|
|
* @param array $columns |
|
167
|
|
|
* @return object |
|
168
|
|
|
*/ |
|
169
|
|
|
public function first($conditions, $relations = [], $columns = ['*']) |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->repo->first($conditions, $relations, $columns); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Return the deleted models in pages based on the given conditions. |
|
176
|
|
|
* |
|
177
|
|
|
* @param array $conditions array of conditions |
|
178
|
|
|
* @param integer $perPage |
|
179
|
|
|
* @param string $sortBy |
|
180
|
|
|
* @param boolean $desc |
|
181
|
|
|
* @param array $columns |
|
182
|
|
|
* @return collection |
|
183
|
|
|
*/ |
|
184
|
|
|
public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
185
|
|
|
{ |
|
186
|
|
|
return $this->repo->deleted($conditions, $perPage, $sortBy, $desc, $columns); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Restore the deleted model. |
|
191
|
|
|
* |
|
192
|
|
|
* @param integer $id |
|
193
|
|
|
* @return void |
|
194
|
|
|
*/ |
|
195
|
|
|
public function restore($id) |
|
196
|
|
|
{ |
|
197
|
|
|
return $this->repo->restore($id); |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
|
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.