@@ -8,605 +8,605 @@ |
||
8 | 8 | |
9 | 9 | abstract class BaseRepository implements BaseRepositoryInterface |
10 | 10 | { |
11 | - /** |
|
12 | - * @var object |
|
13 | - */ |
|
14 | - public $model; |
|
11 | + /** |
|
12 | + * @var object |
|
13 | + */ |
|
14 | + public $model; |
|
15 | 15 | |
16 | - /** |
|
17 | - * Init new object. |
|
18 | - * |
|
19 | - * @var mixed model |
|
20 | - * @return void |
|
21 | - */ |
|
22 | - public function __construct($model) |
|
23 | - { |
|
24 | - $this->model = $model; |
|
25 | - } |
|
26 | - |
|
27 | - /** |
|
28 | - * Fetch all records with relations from the storage. |
|
29 | - * |
|
30 | - * @param array $relations |
|
31 | - * @param string $sortBy |
|
32 | - * @param boolean $desc |
|
33 | - * @param array $columns |
|
34 | - * @return collection |
|
35 | - */ |
|
36 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
37 | - { |
|
38 | - $sort = $desc ? 'desc' : 'asc'; |
|
39 | - return $this->model->with($relations)->orderBy($sortBy, $sort)->get($columns); |
|
40 | - } |
|
16 | + /** |
|
17 | + * Init new object. |
|
18 | + * |
|
19 | + * @var mixed model |
|
20 | + * @return void |
|
21 | + */ |
|
22 | + public function __construct($model) |
|
23 | + { |
|
24 | + $this->model = $model; |
|
25 | + } |
|
26 | + |
|
27 | + /** |
|
28 | + * Fetch all records with relations from the storage. |
|
29 | + * |
|
30 | + * @param array $relations |
|
31 | + * @param string $sortBy |
|
32 | + * @param boolean $desc |
|
33 | + * @param array $columns |
|
34 | + * @return collection |
|
35 | + */ |
|
36 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
37 | + { |
|
38 | + $sort = $desc ? 'desc' : 'asc'; |
|
39 | + return $this->model->with($relations)->orderBy($sortBy, $sort)->get($columns); |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Fetch all records with relations from storage in pages. |
|
44 | - * |
|
45 | - * @param integer $perPage |
|
46 | - * @param array $relations |
|
47 | - * @param string $sortBy |
|
48 | - * @param boolean $desc |
|
49 | - * @param array $columns |
|
50 | - * @return collection |
|
51 | - */ |
|
52 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
53 | - { |
|
54 | - $sort = $desc ? 'desc' : 'asc'; |
|
55 | - return $this->model->with($relations)->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Fetch all records with relations based on |
|
60 | - * the given condition from storage in pages. |
|
61 | - * |
|
62 | - * @param array $conditions array of conditions |
|
63 | - * @param integer $perPage |
|
64 | - * @param array $relations |
|
65 | - * @param string $sortBy |
|
66 | - * @param boolean $desc |
|
67 | - * @param array $columns |
|
68 | - * @return collection |
|
69 | - */ |
|
70 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
71 | - { |
|
72 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
73 | - $sort = $desc ? 'desc' : 'asc'; |
|
74 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Count all records based on the given condition from storage. |
|
79 | - * |
|
80 | - * @param array $conditions array of conditions |
|
81 | - * @return collection |
|
82 | - */ |
|
83 | - public function count($conditions = false) |
|
84 | - { |
|
85 | - if($conditions) { |
|
86 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
87 | - return $this->model->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->count(); |
|
88 | - } |
|
42 | + /** |
|
43 | + * Fetch all records with relations from storage in pages. |
|
44 | + * |
|
45 | + * @param integer $perPage |
|
46 | + * @param array $relations |
|
47 | + * @param string $sortBy |
|
48 | + * @param boolean $desc |
|
49 | + * @param array $columns |
|
50 | + * @return collection |
|
51 | + */ |
|
52 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
53 | + { |
|
54 | + $sort = $desc ? 'desc' : 'asc'; |
|
55 | + return $this->model->with($relations)->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Fetch all records with relations based on |
|
60 | + * the given condition from storage in pages. |
|
61 | + * |
|
62 | + * @param array $conditions array of conditions |
|
63 | + * @param integer $perPage |
|
64 | + * @param array $relations |
|
65 | + * @param string $sortBy |
|
66 | + * @param boolean $desc |
|
67 | + * @param array $columns |
|
68 | + * @return collection |
|
69 | + */ |
|
70 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
71 | + { |
|
72 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
73 | + $sort = $desc ? 'desc' : 'asc'; |
|
74 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Count all records based on the given condition from storage. |
|
79 | + * |
|
80 | + * @param array $conditions array of conditions |
|
81 | + * @return collection |
|
82 | + */ |
|
83 | + public function count($conditions = false) |
|
84 | + { |
|
85 | + if($conditions) { |
|
86 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
87 | + return $this->model->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->count(); |
|
88 | + } |
|
89 | 89 | |
90 | - return $this->model->count(); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Pluck column based on the given condition from storage. |
|
95 | - * |
|
96 | - * @param array $conditions array of conditions |
|
97 | - * @param string $column |
|
98 | - * @return collection |
|
99 | - */ |
|
100 | - public function pluck($conditions, $column) |
|
101 | - { |
|
102 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
103 | - return $this->model->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->pluck($column); |
|
104 | - } |
|
90 | + return $this->model->count(); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Pluck column based on the given condition from storage. |
|
95 | + * |
|
96 | + * @param array $conditions array of conditions |
|
97 | + * @param string $column |
|
98 | + * @return collection |
|
99 | + */ |
|
100 | + public function pluck($conditions, $column) |
|
101 | + { |
|
102 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
103 | + return $this->model->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->pluck($column); |
|
104 | + } |
|
105 | 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 | - $local = \Session::get('locale'); |
|
115 | - \Session::put('locale', 'all'); |
|
116 | - $model = false; |
|
117 | - $relations = []; |
|
118 | - |
|
119 | - \DB::transaction(function () use (&$model, &$relations, $data) { |
|
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 | + $local = \Session::get('locale'); |
|
115 | + \Session::put('locale', 'all'); |
|
116 | + $model = false; |
|
117 | + $relations = []; |
|
118 | + |
|
119 | + \DB::transaction(function () use (&$model, &$relations, $data) { |
|
120 | 120 | |
121 | - $model = $this->prepareModel($data); |
|
122 | - $relations = $this->prepareRelations($data, $model); |
|
123 | - $model = $this->saveModel($model, $relations); |
|
124 | - }); |
|
125 | - \Session::put('locale', $local); |
|
121 | + $model = $this->prepareModel($data); |
|
122 | + $relations = $this->prepareRelations($data, $model); |
|
123 | + $model = $this->saveModel($model, $relations); |
|
124 | + }); |
|
125 | + \Session::put('locale', $local); |
|
126 | 126 | |
127 | - if (count($relations)) { |
|
128 | - $model->load(...array_keys($relations)); |
|
129 | - } |
|
127 | + if (count($relations)) { |
|
128 | + $model->load(...array_keys($relations)); |
|
129 | + } |
|
130 | 130 | |
131 | - return $model; |
|
132 | - } |
|
131 | + return $model; |
|
132 | + } |
|
133 | 133 | |
134 | - /** |
|
135 | - * Insert the given model/models to the storage. |
|
136 | - * |
|
137 | - * @param array $data |
|
138 | - * @return mixed |
|
139 | - */ |
|
140 | - public function insert(array $data) |
|
141 | - { |
|
142 | - return $this->model->insert($data); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Delete record from the storage based on the given |
|
147 | - * condition. |
|
148 | - * |
|
149 | - * @param var $value condition value |
|
150 | - * @param string $attribute condition column name |
|
151 | - * @return void |
|
152 | - */ |
|
153 | - public function delete($value, $attribute = 'id') |
|
154 | - { |
|
155 | - \DB::transaction(function () use ($value, $attribute) { |
|
156 | - $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
157 | - $model->delete(); |
|
158 | - }); |
|
159 | - }); |
|
160 | - } |
|
134 | + /** |
|
135 | + * Insert the given model/models to the storage. |
|
136 | + * |
|
137 | + * @param array $data |
|
138 | + * @return mixed |
|
139 | + */ |
|
140 | + public function insert(array $data) |
|
141 | + { |
|
142 | + return $this->model->insert($data); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Delete record from the storage based on the given |
|
147 | + * condition. |
|
148 | + * |
|
149 | + * @param var $value condition value |
|
150 | + * @param string $attribute condition column name |
|
151 | + * @return void |
|
152 | + */ |
|
153 | + public function delete($value, $attribute = 'id') |
|
154 | + { |
|
155 | + \DB::transaction(function () use ($value, $attribute) { |
|
156 | + $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
157 | + $model->delete(); |
|
158 | + }); |
|
159 | + }); |
|
160 | + } |
|
161 | 161 | |
162 | - /** |
|
163 | - * Fetch records from the storage based on the given |
|
164 | - * id. |
|
165 | - * |
|
166 | - * @param integer $id |
|
167 | - * @param string[] $relations |
|
168 | - * @param array $columns |
|
169 | - * @return object |
|
170 | - */ |
|
171 | - public function find($id, $relations = [], $columns = ['*']) |
|
172 | - { |
|
173 | - return $this->model->with($relations)->find($id, $columns); |
|
174 | - } |
|
162 | + /** |
|
163 | + * Fetch records from the storage based on the given |
|
164 | + * id. |
|
165 | + * |
|
166 | + * @param integer $id |
|
167 | + * @param string[] $relations |
|
168 | + * @param array $columns |
|
169 | + * @return object |
|
170 | + */ |
|
171 | + public function find($id, $relations = [], $columns = ['*']) |
|
172 | + { |
|
173 | + return $this->model->with($relations)->find($id, $columns); |
|
174 | + } |
|
175 | 175 | |
176 | - /** |
|
177 | - * Fetch records from the storage based on the given |
|
178 | - * condition. |
|
179 | - * |
|
180 | - * @param array $conditions array of conditions |
|
181 | - * @param array $relations |
|
182 | - * @param string $sortBy |
|
183 | - * @param boolean $desc |
|
184 | - * @param array $columns |
|
185 | - * @return collection |
|
186 | - */ |
|
187 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
188 | - { |
|
189 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
190 | - $sort = $desc ? 'desc' : 'asc'; |
|
191 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * Fetch the first record from the storage based on the given |
|
196 | - * condition. |
|
197 | - * |
|
198 | - * @param array $conditions array of conditions |
|
199 | - * @param array $relations |
|
200 | - * @param array $columns |
|
201 | - * @return object |
|
202 | - */ |
|
203 | - public function first($conditions, $relations = [], $columns = ['*']) |
|
204 | - { |
|
205 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
206 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Return the deleted models in pages based on the given conditions. |
|
211 | - * |
|
212 | - * @param array $conditions array of conditions |
|
213 | - * @param integer $perPage |
|
214 | - * @param string $sortBy |
|
215 | - * @param boolean $desc |
|
216 | - * @param array $columns |
|
217 | - * @return collection |
|
218 | - */ |
|
219 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
220 | - { |
|
221 | - unset($conditions['page']); |
|
222 | - unset($conditions['perPage']); |
|
223 | - unset($conditions['sortBy']); |
|
224 | - unset($conditions['sort']); |
|
225 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
226 | - $sort = $desc ? 'desc' : 'asc'; |
|
227 | - $model = $this->model->onlyTrashed(); |
|
228 | - |
|
229 | - if (count($conditions['conditionValues'])) { |
|
230 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
231 | - } |
|
232 | - |
|
233 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * Restore the deleted model. |
|
238 | - * |
|
239 | - * @param integer $id |
|
240 | - * @return void |
|
241 | - */ |
|
242 | - public function restore($id) |
|
243 | - { |
|
244 | - $model = $this->model->onlyTrashed()->find($id); |
|
245 | - |
|
246 | - if (! $model) { |
|
247 | - \Errors::notFound(class_basename($this->model).' with id : '.$id); |
|
248 | - } |
|
249 | - |
|
250 | - $model->restore(); |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * Fill the model with the given data. |
|
255 | - * |
|
256 | - * @param array $data |
|
257 | - * |
|
258 | - * @return object |
|
259 | - */ |
|
260 | - public function prepareModel($data) |
|
261 | - { |
|
262 | - $modelClass = $this->model; |
|
263 | - |
|
264 | - /** |
|
265 | - * If the id is present in the data then select the model for updating, |
|
266 | - * else create new model. |
|
267 | - * @var array |
|
268 | - */ |
|
269 | - $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
270 | - if (! $model) { |
|
271 | - \Errors::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
272 | - } |
|
273 | - |
|
274 | - /** |
|
275 | - * Construct the model object with the given data, |
|
276 | - * and if there is a relation add it to relations array, |
|
277 | - * then save the model. |
|
278 | - */ |
|
279 | - foreach ($data as $key => $value) { |
|
280 | - if (array_search($key, $model->getFillable(), true) !== false) { |
|
281 | - /** |
|
282 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
283 | - */ |
|
284 | - $model->$key = $value; |
|
285 | - } |
|
286 | - } |
|
287 | - |
|
288 | - return $model; |
|
289 | - } |
|
176 | + /** |
|
177 | + * Fetch records from the storage based on the given |
|
178 | + * condition. |
|
179 | + * |
|
180 | + * @param array $conditions array of conditions |
|
181 | + * @param array $relations |
|
182 | + * @param string $sortBy |
|
183 | + * @param boolean $desc |
|
184 | + * @param array $columns |
|
185 | + * @return collection |
|
186 | + */ |
|
187 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
188 | + { |
|
189 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
190 | + $sort = $desc ? 'desc' : 'asc'; |
|
191 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * Fetch the first record from the storage based on the given |
|
196 | + * condition. |
|
197 | + * |
|
198 | + * @param array $conditions array of conditions |
|
199 | + * @param array $relations |
|
200 | + * @param array $columns |
|
201 | + * @return object |
|
202 | + */ |
|
203 | + public function first($conditions, $relations = [], $columns = ['*']) |
|
204 | + { |
|
205 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
206 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Return the deleted models in pages based on the given conditions. |
|
211 | + * |
|
212 | + * @param array $conditions array of conditions |
|
213 | + * @param integer $perPage |
|
214 | + * @param string $sortBy |
|
215 | + * @param boolean $desc |
|
216 | + * @param array $columns |
|
217 | + * @return collection |
|
218 | + */ |
|
219 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
220 | + { |
|
221 | + unset($conditions['page']); |
|
222 | + unset($conditions['perPage']); |
|
223 | + unset($conditions['sortBy']); |
|
224 | + unset($conditions['sort']); |
|
225 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
226 | + $sort = $desc ? 'desc' : 'asc'; |
|
227 | + $model = $this->model->onlyTrashed(); |
|
228 | + |
|
229 | + if (count($conditions['conditionValues'])) { |
|
230 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
231 | + } |
|
232 | + |
|
233 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * Restore the deleted model. |
|
238 | + * |
|
239 | + * @param integer $id |
|
240 | + * @return void |
|
241 | + */ |
|
242 | + public function restore($id) |
|
243 | + { |
|
244 | + $model = $this->model->onlyTrashed()->find($id); |
|
245 | + |
|
246 | + if (! $model) { |
|
247 | + \Errors::notFound(class_basename($this->model).' with id : '.$id); |
|
248 | + } |
|
249 | + |
|
250 | + $model->restore(); |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * Fill the model with the given data. |
|
255 | + * |
|
256 | + * @param array $data |
|
257 | + * |
|
258 | + * @return object |
|
259 | + */ |
|
260 | + public function prepareModel($data) |
|
261 | + { |
|
262 | + $modelClass = $this->model; |
|
263 | + |
|
264 | + /** |
|
265 | + * If the id is present in the data then select the model for updating, |
|
266 | + * else create new model. |
|
267 | + * @var array |
|
268 | + */ |
|
269 | + $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
270 | + if (! $model) { |
|
271 | + \Errors::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
272 | + } |
|
273 | + |
|
274 | + /** |
|
275 | + * Construct the model object with the given data, |
|
276 | + * and if there is a relation add it to relations array, |
|
277 | + * then save the model. |
|
278 | + */ |
|
279 | + foreach ($data as $key => $value) { |
|
280 | + if (array_search($key, $model->getFillable(), true) !== false) { |
|
281 | + /** |
|
282 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
283 | + */ |
|
284 | + $model->$key = $value; |
|
285 | + } |
|
286 | + } |
|
287 | + |
|
288 | + return $model; |
|
289 | + } |
|
290 | 290 | |
291 | - /** |
|
292 | - * Prepare related models based on the given data for the given model. |
|
293 | - * |
|
294 | - * @param array $data |
|
295 | - * @param object $model |
|
296 | - * |
|
297 | - * @return array |
|
298 | - */ |
|
299 | - public function prepareRelations($data, $model) |
|
300 | - { |
|
301 | - /** |
|
302 | - * Init the relation array |
|
303 | - * |
|
304 | - * @var array |
|
305 | - */ |
|
306 | - $relations = []; |
|
307 | - |
|
308 | - /** |
|
309 | - * Construct the model object with the given data, |
|
310 | - * and if there is a relation add it to relations array, |
|
311 | - * then save the model. |
|
312 | - */ |
|
313 | - foreach ($data as $key => $value) { |
|
314 | - /** |
|
315 | - * If the attribute is a relation. |
|
316 | - */ |
|
317 | - $relation = \Str::camel($key); |
|
318 | - if (method_exists($model, $relation) && \Core::$relation()) { |
|
319 | - /** |
|
320 | - * Check if the relation is a collection. |
|
321 | - */ |
|
322 | - if (class_basename($model->$relation) == 'Collection') { |
|
323 | - /** |
|
324 | - * If the relation has no value then marke the relation data |
|
325 | - * related to the model to be deleted. |
|
326 | - */ |
|
327 | - if (! $value || ! count($value)) { |
|
328 | - $relations[$relation] = 'delete'; |
|
329 | - } |
|
330 | - } |
|
331 | - if (is_array($value)) { |
|
332 | - /** |
|
333 | - * Loop through the relation data. |
|
334 | - */ |
|
335 | - foreach ($value as $attr => $val) { |
|
336 | - /** |
|
337 | - * Get the relation model. |
|
338 | - */ |
|
339 | - $relationBaseModel = \Core::$relation()->model; |
|
340 | - |
|
341 | - /** |
|
342 | - * Check if the relation is a collection. |
|
343 | - */ |
|
344 | - if (class_basename($model->$relation) == 'Collection') { |
|
345 | - if (! is_array($val)) { |
|
346 | - $relationModel = $relationBaseModel->lockForUpdate()->find($val); |
|
347 | - } else { |
|
348 | - /** |
|
349 | - * If the id is present in the data then select the relation model for updating, |
|
350 | - * else create new model. |
|
351 | - */ |
|
352 | - $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
353 | - } |
|
354 | - |
|
355 | - /** |
|
356 | - * If model doesn't exists. |
|
357 | - */ |
|
358 | - if (! $relationModel) { |
|
359 | - \Errors::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
360 | - } |
|
361 | - |
|
362 | - if (is_array($val)) { |
|
363 | - /** |
|
364 | - * Loop through the relation attributes. |
|
365 | - */ |
|
366 | - foreach ($val as $attr => $val) { |
|
367 | - /** |
|
368 | - * Prevent the sub relations or attributes not in the fillable. |
|
369 | - */ |
|
370 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
|
371 | - $relationModel->$attr = $val; |
|
372 | - } elseif (gettype($val) !== 'object' && gettype($val) !== 'array' && $attr !== 'id') { |
|
373 | - $extra[$attr] = $val; |
|
374 | - } |
|
375 | - } |
|
376 | - } |
|
377 | - |
|
378 | - if (isset($extra)) { |
|
379 | - $relationModel->extra = $extra; |
|
380 | - } |
|
381 | - $relations[$relation][] = $relationModel; |
|
382 | - } else { |
|
383 | - /** |
|
384 | - * Prevent the sub relations. |
|
385 | - */ |
|
386 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
387 | - /** |
|
388 | - * If the id is present in the data then select the relation model for updating, |
|
389 | - * else create new model. |
|
390 | - */ |
|
391 | - $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
392 | - |
|
393 | - /** |
|
394 | - * If model doesn't exists. |
|
395 | - */ |
|
396 | - if (! $relationModel) { |
|
397 | - \Errors::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
398 | - } |
|
399 | - |
|
400 | - foreach ($value as $relationAttribute => $relationValue) { |
|
401 | - /** |
|
402 | - * Prevent attributes not in the fillable. |
|
403 | - */ |
|
404 | - if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
405 | - $relationModel->$relationAttribute = $relationValue; |
|
406 | - } |
|
407 | - } |
|
408 | - |
|
409 | - $relations[$relation] = $relationModel; |
|
410 | - } |
|
411 | - } |
|
412 | - } |
|
413 | - } |
|
414 | - } |
|
415 | - } |
|
416 | - |
|
417 | - return $relations; |
|
418 | - } |
|
419 | - |
|
420 | - /** |
|
421 | - * Save the model with related models. |
|
422 | - * |
|
423 | - * @param object $model |
|
424 | - * @param array $relations |
|
425 | - * |
|
426 | - * @return object |
|
427 | - */ |
|
428 | - public function saveModel($model, $relations) |
|
429 | - { |
|
430 | - |
|
431 | - /** |
|
432 | - * Loop through the relations array. |
|
433 | - */ |
|
434 | - foreach ($relations as $key => $value) { |
|
435 | - /** |
|
436 | - * If the relation is marked for delete then delete it. |
|
437 | - */ |
|
438 | - if ($value == 'delete' && $model->$key()->count()) { |
|
439 | - $model->$key()->delete(); |
|
440 | - } elseif (gettype($value) == 'array') { |
|
441 | - /** |
|
442 | - * Save the model. |
|
443 | - */ |
|
444 | - $model->save(); |
|
445 | - $ids = []; |
|
446 | - |
|
447 | - /** |
|
448 | - * Loop through the relations. |
|
449 | - */ |
|
450 | - foreach ($value as $val) { |
|
451 | - switch (class_basename($model->$key())) { |
|
452 | - /** |
|
453 | - * If the relation is one to many then update it's foreign key with |
|
454 | - * the model id and save it then add its id to ids array to delete all |
|
455 | - * relations who's id isn't in the ids array. |
|
456 | - */ |
|
457 | - case 'HasMany': |
|
458 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
459 | - $val->$foreignKeyName = $model->id; |
|
460 | - $val->save(); |
|
461 | - $ids[] = $val->id; |
|
462 | - break; |
|
463 | - |
|
464 | - /** |
|
465 | - * If the relation is many to many then add it's id to the ids array to |
|
466 | - * attache these ids to the model. |
|
467 | - */ |
|
468 | - case 'BelongsToMany': |
|
469 | - $extra = $val->extra; |
|
470 | - unset($val->extra); |
|
471 | - $val->save(); |
|
472 | - $ids[$val->id] = $extra ?? []; |
|
473 | - break; |
|
474 | - } |
|
475 | - } |
|
476 | - switch (class_basename($model->$key())) { |
|
477 | - /** |
|
478 | - * If the relation is one to many then delete all |
|
479 | - * relations who's id isn't in the ids array. |
|
480 | - */ |
|
481 | - case 'HasMany': |
|
482 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
483 | - break; |
|
484 | - |
|
485 | - /** |
|
486 | - * If the relation is many to many then |
|
487 | - * detach the previous data and attach |
|
488 | - * the ids array to the model. |
|
489 | - */ |
|
490 | - case 'BelongsToMany': |
|
491 | - $model->$key()->detach(); |
|
492 | - $model->$key()->attach($ids); |
|
493 | - break; |
|
494 | - } |
|
495 | - } else { |
|
496 | - switch (class_basename($model->$key())) { |
|
497 | - /** |
|
498 | - * If the relation is one to one. |
|
499 | - */ |
|
500 | - case 'HasOne': |
|
501 | - /** |
|
502 | - * Save the model. |
|
503 | - */ |
|
504 | - $model->save(); |
|
505 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
506 | - $value->$foreignKeyName = $model->id; |
|
507 | - $value->save(); |
|
508 | - break; |
|
509 | - case 'BelongsTo': |
|
510 | - /** |
|
511 | - * Save the model. |
|
512 | - */ |
|
513 | - $value->save(); |
|
514 | - $model->$key()->associate($value); |
|
515 | - break; |
|
516 | - } |
|
517 | - } |
|
518 | - } |
|
519 | - |
|
520 | - /** |
|
521 | - * Save the model. |
|
522 | - */ |
|
523 | - $model->save(); |
|
524 | - |
|
525 | - return $model; |
|
526 | - } |
|
527 | - |
|
528 | - /** |
|
529 | - * Build the conditions recursively for the retrieving methods. |
|
530 | - * @param array $conditions |
|
531 | - * @return array |
|
532 | - */ |
|
533 | - protected function constructConditions($conditions, $model) |
|
534 | - { |
|
535 | - $conditionString = ''; |
|
536 | - $conditionValues = []; |
|
537 | - foreach ($conditions as $key => $value) { |
|
538 | - if (Str::contains($key, '->')) { |
|
539 | - $key = $this->wrapJsonSelector($key); |
|
540 | - } |
|
541 | - |
|
542 | - if ($key == 'and') { |
|
543 | - $conditions = $this->constructConditions($value, $model); |
|
544 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
545 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
546 | - } elseif ($key == 'or') { |
|
547 | - $conditions = $this->constructConditions($value, $model); |
|
548 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
549 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
550 | - } else { |
|
551 | - if (is_array($value)) { |
|
552 | - $operator = $value['op']; |
|
553 | - if (strtolower($operator) == 'between') { |
|
554 | - $value1 = $value['val1']; |
|
555 | - $value2 = $value['val2']; |
|
556 | - } else { |
|
557 | - $value = Arr::get($value, 'val', ''); |
|
558 | - } |
|
559 | - } else { |
|
560 | - $operator = '='; |
|
561 | - } |
|
291 | + /** |
|
292 | + * Prepare related models based on the given data for the given model. |
|
293 | + * |
|
294 | + * @param array $data |
|
295 | + * @param object $model |
|
296 | + * |
|
297 | + * @return array |
|
298 | + */ |
|
299 | + public function prepareRelations($data, $model) |
|
300 | + { |
|
301 | + /** |
|
302 | + * Init the relation array |
|
303 | + * |
|
304 | + * @var array |
|
305 | + */ |
|
306 | + $relations = []; |
|
307 | + |
|
308 | + /** |
|
309 | + * Construct the model object with the given data, |
|
310 | + * and if there is a relation add it to relations array, |
|
311 | + * then save the model. |
|
312 | + */ |
|
313 | + foreach ($data as $key => $value) { |
|
314 | + /** |
|
315 | + * If the attribute is a relation. |
|
316 | + */ |
|
317 | + $relation = \Str::camel($key); |
|
318 | + if (method_exists($model, $relation) && \Core::$relation()) { |
|
319 | + /** |
|
320 | + * Check if the relation is a collection. |
|
321 | + */ |
|
322 | + if (class_basename($model->$relation) == 'Collection') { |
|
323 | + /** |
|
324 | + * If the relation has no value then marke the relation data |
|
325 | + * related to the model to be deleted. |
|
326 | + */ |
|
327 | + if (! $value || ! count($value)) { |
|
328 | + $relations[$relation] = 'delete'; |
|
329 | + } |
|
330 | + } |
|
331 | + if (is_array($value)) { |
|
332 | + /** |
|
333 | + * Loop through the relation data. |
|
334 | + */ |
|
335 | + foreach ($value as $attr => $val) { |
|
336 | + /** |
|
337 | + * Get the relation model. |
|
338 | + */ |
|
339 | + $relationBaseModel = \Core::$relation()->model; |
|
340 | + |
|
341 | + /** |
|
342 | + * Check if the relation is a collection. |
|
343 | + */ |
|
344 | + if (class_basename($model->$relation) == 'Collection') { |
|
345 | + if (! is_array($val)) { |
|
346 | + $relationModel = $relationBaseModel->lockForUpdate()->find($val); |
|
347 | + } else { |
|
348 | + /** |
|
349 | + * If the id is present in the data then select the relation model for updating, |
|
350 | + * else create new model. |
|
351 | + */ |
|
352 | + $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
353 | + } |
|
354 | + |
|
355 | + /** |
|
356 | + * If model doesn't exists. |
|
357 | + */ |
|
358 | + if (! $relationModel) { |
|
359 | + \Errors::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
360 | + } |
|
361 | + |
|
362 | + if (is_array($val)) { |
|
363 | + /** |
|
364 | + * Loop through the relation attributes. |
|
365 | + */ |
|
366 | + foreach ($val as $attr => $val) { |
|
367 | + /** |
|
368 | + * Prevent the sub relations or attributes not in the fillable. |
|
369 | + */ |
|
370 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
|
371 | + $relationModel->$attr = $val; |
|
372 | + } elseif (gettype($val) !== 'object' && gettype($val) !== 'array' && $attr !== 'id') { |
|
373 | + $extra[$attr] = $val; |
|
374 | + } |
|
375 | + } |
|
376 | + } |
|
377 | + |
|
378 | + if (isset($extra)) { |
|
379 | + $relationModel->extra = $extra; |
|
380 | + } |
|
381 | + $relations[$relation][] = $relationModel; |
|
382 | + } else { |
|
383 | + /** |
|
384 | + * Prevent the sub relations. |
|
385 | + */ |
|
386 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
387 | + /** |
|
388 | + * If the id is present in the data then select the relation model for updating, |
|
389 | + * else create new model. |
|
390 | + */ |
|
391 | + $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
392 | + |
|
393 | + /** |
|
394 | + * If model doesn't exists. |
|
395 | + */ |
|
396 | + if (! $relationModel) { |
|
397 | + \Errors::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
398 | + } |
|
399 | + |
|
400 | + foreach ($value as $relationAttribute => $relationValue) { |
|
401 | + /** |
|
402 | + * Prevent attributes not in the fillable. |
|
403 | + */ |
|
404 | + if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
405 | + $relationModel->$relationAttribute = $relationValue; |
|
406 | + } |
|
407 | + } |
|
408 | + |
|
409 | + $relations[$relation] = $relationModel; |
|
410 | + } |
|
411 | + } |
|
412 | + } |
|
413 | + } |
|
414 | + } |
|
415 | + } |
|
416 | + |
|
417 | + return $relations; |
|
418 | + } |
|
419 | + |
|
420 | + /** |
|
421 | + * Save the model with related models. |
|
422 | + * |
|
423 | + * @param object $model |
|
424 | + * @param array $relations |
|
425 | + * |
|
426 | + * @return object |
|
427 | + */ |
|
428 | + public function saveModel($model, $relations) |
|
429 | + { |
|
430 | + |
|
431 | + /** |
|
432 | + * Loop through the relations array. |
|
433 | + */ |
|
434 | + foreach ($relations as $key => $value) { |
|
435 | + /** |
|
436 | + * If the relation is marked for delete then delete it. |
|
437 | + */ |
|
438 | + if ($value == 'delete' && $model->$key()->count()) { |
|
439 | + $model->$key()->delete(); |
|
440 | + } elseif (gettype($value) == 'array') { |
|
441 | + /** |
|
442 | + * Save the model. |
|
443 | + */ |
|
444 | + $model->save(); |
|
445 | + $ids = []; |
|
446 | + |
|
447 | + /** |
|
448 | + * Loop through the relations. |
|
449 | + */ |
|
450 | + foreach ($value as $val) { |
|
451 | + switch (class_basename($model->$key())) { |
|
452 | + /** |
|
453 | + * If the relation is one to many then update it's foreign key with |
|
454 | + * the model id and save it then add its id to ids array to delete all |
|
455 | + * relations who's id isn't in the ids array. |
|
456 | + */ |
|
457 | + case 'HasMany': |
|
458 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
459 | + $val->$foreignKeyName = $model->id; |
|
460 | + $val->save(); |
|
461 | + $ids[] = $val->id; |
|
462 | + break; |
|
463 | + |
|
464 | + /** |
|
465 | + * If the relation is many to many then add it's id to the ids array to |
|
466 | + * attache these ids to the model. |
|
467 | + */ |
|
468 | + case 'BelongsToMany': |
|
469 | + $extra = $val->extra; |
|
470 | + unset($val->extra); |
|
471 | + $val->save(); |
|
472 | + $ids[$val->id] = $extra ?? []; |
|
473 | + break; |
|
474 | + } |
|
475 | + } |
|
476 | + switch (class_basename($model->$key())) { |
|
477 | + /** |
|
478 | + * If the relation is one to many then delete all |
|
479 | + * relations who's id isn't in the ids array. |
|
480 | + */ |
|
481 | + case 'HasMany': |
|
482 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
483 | + break; |
|
484 | + |
|
485 | + /** |
|
486 | + * If the relation is many to many then |
|
487 | + * detach the previous data and attach |
|
488 | + * the ids array to the model. |
|
489 | + */ |
|
490 | + case 'BelongsToMany': |
|
491 | + $model->$key()->detach(); |
|
492 | + $model->$key()->attach($ids); |
|
493 | + break; |
|
494 | + } |
|
495 | + } else { |
|
496 | + switch (class_basename($model->$key())) { |
|
497 | + /** |
|
498 | + * If the relation is one to one. |
|
499 | + */ |
|
500 | + case 'HasOne': |
|
501 | + /** |
|
502 | + * Save the model. |
|
503 | + */ |
|
504 | + $model->save(); |
|
505 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
506 | + $value->$foreignKeyName = $model->id; |
|
507 | + $value->save(); |
|
508 | + break; |
|
509 | + case 'BelongsTo': |
|
510 | + /** |
|
511 | + * Save the model. |
|
512 | + */ |
|
513 | + $value->save(); |
|
514 | + $model->$key()->associate($value); |
|
515 | + break; |
|
516 | + } |
|
517 | + } |
|
518 | + } |
|
519 | + |
|
520 | + /** |
|
521 | + * Save the model. |
|
522 | + */ |
|
523 | + $model->save(); |
|
524 | + |
|
525 | + return $model; |
|
526 | + } |
|
527 | + |
|
528 | + /** |
|
529 | + * Build the conditions recursively for the retrieving methods. |
|
530 | + * @param array $conditions |
|
531 | + * @return array |
|
532 | + */ |
|
533 | + protected function constructConditions($conditions, $model) |
|
534 | + { |
|
535 | + $conditionString = ''; |
|
536 | + $conditionValues = []; |
|
537 | + foreach ($conditions as $key => $value) { |
|
538 | + if (Str::contains($key, '->')) { |
|
539 | + $key = $this->wrapJsonSelector($key); |
|
540 | + } |
|
541 | + |
|
542 | + if ($key == 'and') { |
|
543 | + $conditions = $this->constructConditions($value, $model); |
|
544 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
545 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
546 | + } elseif ($key == 'or') { |
|
547 | + $conditions = $this->constructConditions($value, $model); |
|
548 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
549 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
550 | + } else { |
|
551 | + if (is_array($value)) { |
|
552 | + $operator = $value['op']; |
|
553 | + if (strtolower($operator) == 'between') { |
|
554 | + $value1 = $value['val1']; |
|
555 | + $value2 = $value['val2']; |
|
556 | + } else { |
|
557 | + $value = Arr::get($value, 'val', ''); |
|
558 | + } |
|
559 | + } else { |
|
560 | + $operator = '='; |
|
561 | + } |
|
562 | 562 | |
563 | - if (strtolower($operator) == 'between') { |
|
564 | - $conditionString .= $key.' >= ? and '; |
|
565 | - $conditionValues[] = $value1; |
|
566 | - |
|
567 | - $conditionString .= $key.' <= ? {op} '; |
|
568 | - $conditionValues[] = $value2; |
|
569 | - } elseif (strtolower($operator) == 'in') { |
|
570 | - $conditionValues = array_merge($conditionValues, $value); |
|
571 | - $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
572 | - $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
573 | - } elseif (strtolower($operator) == 'null') { |
|
574 | - $conditionString .= $key.' is null {op} '; |
|
575 | - } elseif (strtolower($operator) == 'not null') { |
|
576 | - $conditionString .= $key.' is not null {op} '; |
|
577 | - } elseif (strtolower($operator) == 'has') { |
|
578 | - $sql = $model->withTrashed()->withoutGlobalScopes()->has($key)->toSql(); |
|
579 | - $bindings = $model->withTrashed()->withoutGlobalScopes()->has($key)->getBindings(); |
|
580 | - $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
581 | - $conditionString .= substr(substr($sql, strpos($sql, 'exists')), 0, -1).' and '.$conditions['conditionString'].') {op} '; |
|
582 | - $conditionValues = array_merge($conditionValues, $bindings); |
|
583 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
584 | - } else { |
|
585 | - $conditionString .= $key.' '.$operator.' ? {op} '; |
|
586 | - $conditionValues[] = $value; |
|
587 | - } |
|
588 | - } |
|
589 | - } |
|
590 | - $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
591 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
592 | - } |
|
593 | - |
|
594 | - /** |
|
595 | - * Wrap the given JSON selector. |
|
596 | - * |
|
597 | - * @param string $value |
|
598 | - * @return string |
|
599 | - */ |
|
600 | - protected function wrapJsonSelector($value) |
|
601 | - { |
|
602 | - $removeLast = strpos($value, ')'); |
|
603 | - $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
604 | - $path = explode('->', $value); |
|
605 | - $field = array_shift($path); |
|
606 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
607 | - return '"'.$part.'"'; |
|
608 | - })->implode('.')); |
|
563 | + if (strtolower($operator) == 'between') { |
|
564 | + $conditionString .= $key.' >= ? and '; |
|
565 | + $conditionValues[] = $value1; |
|
566 | + |
|
567 | + $conditionString .= $key.' <= ? {op} '; |
|
568 | + $conditionValues[] = $value2; |
|
569 | + } elseif (strtolower($operator) == 'in') { |
|
570 | + $conditionValues = array_merge($conditionValues, $value); |
|
571 | + $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
572 | + $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
573 | + } elseif (strtolower($operator) == 'null') { |
|
574 | + $conditionString .= $key.' is null {op} '; |
|
575 | + } elseif (strtolower($operator) == 'not null') { |
|
576 | + $conditionString .= $key.' is not null {op} '; |
|
577 | + } elseif (strtolower($operator) == 'has') { |
|
578 | + $sql = $model->withTrashed()->withoutGlobalScopes()->has($key)->toSql(); |
|
579 | + $bindings = $model->withTrashed()->withoutGlobalScopes()->has($key)->getBindings(); |
|
580 | + $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
581 | + $conditionString .= substr(substr($sql, strpos($sql, 'exists')), 0, -1).' and '.$conditions['conditionString'].') {op} '; |
|
582 | + $conditionValues = array_merge($conditionValues, $bindings); |
|
583 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
584 | + } else { |
|
585 | + $conditionString .= $key.' '.$operator.' ? {op} '; |
|
586 | + $conditionValues[] = $value; |
|
587 | + } |
|
588 | + } |
|
589 | + } |
|
590 | + $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
591 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
592 | + } |
|
593 | + |
|
594 | + /** |
|
595 | + * Wrap the given JSON selector. |
|
596 | + * |
|
597 | + * @param string $value |
|
598 | + * @return string |
|
599 | + */ |
|
600 | + protected function wrapJsonSelector($value) |
|
601 | + { |
|
602 | + $removeLast = strpos($value, ')'); |
|
603 | + $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
604 | + $path = explode('->', $value); |
|
605 | + $field = array_shift($path); |
|
606 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
607 | + return '"'.$part.'"'; |
|
608 | + })->implode('.')); |
|
609 | 609 | |
610 | - return $removeLast === false ? $result : $result.')'; |
|
611 | - } |
|
610 | + return $removeLast === false ? $result : $result.')'; |
|
611 | + } |
|
612 | 612 | } |
@@ -21,7 +21,7 @@ discard block |
||
21 | 21 | */ |
22 | 22 | public function __construct($model) |
23 | 23 | { |
24 | - $this->model = $model; |
|
24 | + $this->model = $model; |
|
25 | 25 | } |
26 | 26 | |
27 | 27 | /** |
@@ -82,7 +82,7 @@ discard block |
||
82 | 82 | */ |
83 | 83 | public function count($conditions = false) |
84 | 84 | { |
85 | - if($conditions) { |
|
85 | + if ($conditions) { |
|
86 | 86 | $conditions = $this->constructConditions($conditions, $this->model); |
87 | 87 | return $this->model->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->count(); |
88 | 88 | } |
@@ -116,7 +116,7 @@ discard block |
||
116 | 116 | $model = false; |
117 | 117 | $relations = []; |
118 | 118 | |
119 | - \DB::transaction(function () use (&$model, &$relations, $data) { |
|
119 | + \DB::transaction(function() use (&$model, &$relations, $data) { |
|
120 | 120 | |
121 | 121 | $model = $this->prepareModel($data); |
122 | 122 | $relations = $this->prepareRelations($data, $model); |
@@ -152,8 +152,8 @@ discard block |
||
152 | 152 | */ |
153 | 153 | public function delete($value, $attribute = 'id') |
154 | 154 | { |
155 | - \DB::transaction(function () use ($value, $attribute) { |
|
156 | - $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
155 | + \DB::transaction(function() use ($value, $attribute) { |
|
156 | + $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function($model) { |
|
157 | 157 | $model->delete(); |
158 | 158 | }); |
159 | 159 | }); |
@@ -243,7 +243,7 @@ discard block |
||
243 | 243 | { |
244 | 244 | $model = $this->model->onlyTrashed()->find($id); |
245 | 245 | |
246 | - if (! $model) { |
|
246 | + if ( ! $model) { |
|
247 | 247 | \Errors::notFound(class_basename($this->model).' with id : '.$id); |
248 | 248 | } |
249 | 249 | |
@@ -267,7 +267,7 @@ discard block |
||
267 | 267 | * @var array |
268 | 268 | */ |
269 | 269 | $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
270 | - if (! $model) { |
|
270 | + if ( ! $model) { |
|
271 | 271 | \Errors::notFound(class_basename($modelClass).' with id : '.$data['id']); |
272 | 272 | } |
273 | 273 | |
@@ -324,7 +324,7 @@ discard block |
||
324 | 324 | * If the relation has no value then marke the relation data |
325 | 325 | * related to the model to be deleted. |
326 | 326 | */ |
327 | - if (! $value || ! count($value)) { |
|
327 | + if ( ! $value || ! count($value)) { |
|
328 | 328 | $relations[$relation] = 'delete'; |
329 | 329 | } |
330 | 330 | } |
@@ -342,7 +342,7 @@ discard block |
||
342 | 342 | * Check if the relation is a collection. |
343 | 343 | */ |
344 | 344 | if (class_basename($model->$relation) == 'Collection') { |
345 | - if (! is_array($val)) { |
|
345 | + if ( ! is_array($val)) { |
|
346 | 346 | $relationModel = $relationBaseModel->lockForUpdate()->find($val); |
347 | 347 | } else { |
348 | 348 | /** |
@@ -355,7 +355,7 @@ discard block |
||
355 | 355 | /** |
356 | 356 | * If model doesn't exists. |
357 | 357 | */ |
358 | - if (! $relationModel) { |
|
358 | + if ( ! $relationModel) { |
|
359 | 359 | \Errors::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
360 | 360 | } |
361 | 361 | |
@@ -393,7 +393,7 @@ discard block |
||
393 | 393 | /** |
394 | 394 | * If model doesn't exists. |
395 | 395 | */ |
396 | - if (! $relationModel) { |
|
396 | + if ( ! $relationModel) { |
|
397 | 397 | \Errors::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
398 | 398 | } |
399 | 399 | |
@@ -603,7 +603,7 @@ discard block |
||
603 | 603 | $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
604 | 604 | $path = explode('->', $value); |
605 | 605 | $field = array_shift($path); |
606 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
606 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function($part) { |
|
607 | 607 | return '"'.$part.'"'; |
608 | 608 | })->implode('.')); |
609 | 609 |
@@ -8,308 +8,308 @@ |
||
8 | 8 | |
9 | 9 | class GenerateDocCommand extends Command |
10 | 10 | { |
11 | - /** |
|
12 | - * The name and signature of the console command. |
|
13 | - * |
|
14 | - * @var string |
|
15 | - */ |
|
16 | - protected $signature = 'doc:generate'; |
|
17 | - |
|
18 | - /** |
|
19 | - * The console command description. |
|
20 | - * |
|
21 | - * @var string |
|
22 | - */ |
|
23 | - protected $description = 'Generate api documentation'; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var ReprotService |
|
27 | - */ |
|
28 | - protected $reportService; |
|
29 | - |
|
30 | - /** |
|
31 | - * Init new object. |
|
32 | - * |
|
33 | - * @return void |
|
34 | - */ |
|
35 | - public function __construct(ReportService $reportService) |
|
36 | - { |
|
37 | - $this->reportService = $reportService; |
|
38 | - parent::__construct(); |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * Execute the console command. |
|
43 | - * |
|
44 | - * @return mixed |
|
45 | - */ |
|
46 | - public function handle() |
|
47 | - { |
|
48 | - $docData = []; |
|
49 | - $docData['models'] = []; |
|
50 | - $routes = $this->getRoutes(); |
|
51 | - foreach ($routes as $route) { |
|
52 | - if ($route) { |
|
53 | - $actoinArray = explode('@', $route['action']); |
|
54 | - if (Arr::get($actoinArray, 1, false)) { |
|
55 | - $prefix = $route['prefix']; |
|
56 | - $module = \Str::camel(str_replace('/', '_', str_replace('api', '', $prefix))); |
|
57 | - if ($prefix === 'telescope') { |
|
58 | - continue; |
|
59 | - } |
|
60 | - |
|
61 | - $controller = $actoinArray[0]; |
|
62 | - $method = $actoinArray[1]; |
|
63 | - $route['name'] = $method; |
|
64 | - $reflectionClass = new \ReflectionClass($controller); |
|
65 | - $reflectionMethod = $reflectionClass->getMethod($method); |
|
66 | - $classProperties = $reflectionClass->getDefaultProperties(); |
|
67 | - $skipLoginCheck = Arr::get($classProperties, 'skipLoginCheck', false); |
|
68 | - $modelName = explode('\\', $controller); |
|
69 | - $modelName = lcfirst(str_replace('Controller', '', end($modelName))); |
|
70 | - |
|
71 | - $this->processDocBlock($route, $reflectionMethod); |
|
72 | - $this->getHeaders($route, $method, $skipLoginCheck); |
|
73 | - $this->getPostData($route, $reflectionMethod, explode('\\', $reflectionClass->getName())[2], $modelName); |
|
74 | - |
|
75 | - $route['response'] = $this->getResponseObject($modelName, $route['name'], $route['returnDocBlock']); |
|
76 | - $docData['modules'][$module][] = $route; |
|
77 | - |
|
78 | - $this->getModels($modelName, $docData, $reflectionClass); |
|
79 | - } |
|
80 | - } |
|
81 | - } |
|
11 | + /** |
|
12 | + * The name and signature of the console command. |
|
13 | + * |
|
14 | + * @var string |
|
15 | + */ |
|
16 | + protected $signature = 'doc:generate'; |
|
17 | + |
|
18 | + /** |
|
19 | + * The console command description. |
|
20 | + * |
|
21 | + * @var string |
|
22 | + */ |
|
23 | + protected $description = 'Generate api documentation'; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var ReprotService |
|
27 | + */ |
|
28 | + protected $reportService; |
|
29 | + |
|
30 | + /** |
|
31 | + * Init new object. |
|
32 | + * |
|
33 | + * @return void |
|
34 | + */ |
|
35 | + public function __construct(ReportService $reportService) |
|
36 | + { |
|
37 | + $this->reportService = $reportService; |
|
38 | + parent::__construct(); |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * Execute the console command. |
|
43 | + * |
|
44 | + * @return mixed |
|
45 | + */ |
|
46 | + public function handle() |
|
47 | + { |
|
48 | + $docData = []; |
|
49 | + $docData['models'] = []; |
|
50 | + $routes = $this->getRoutes(); |
|
51 | + foreach ($routes as $route) { |
|
52 | + if ($route) { |
|
53 | + $actoinArray = explode('@', $route['action']); |
|
54 | + if (Arr::get($actoinArray, 1, false)) { |
|
55 | + $prefix = $route['prefix']; |
|
56 | + $module = \Str::camel(str_replace('/', '_', str_replace('api', '', $prefix))); |
|
57 | + if ($prefix === 'telescope') { |
|
58 | + continue; |
|
59 | + } |
|
60 | + |
|
61 | + $controller = $actoinArray[0]; |
|
62 | + $method = $actoinArray[1]; |
|
63 | + $route['name'] = $method; |
|
64 | + $reflectionClass = new \ReflectionClass($controller); |
|
65 | + $reflectionMethod = $reflectionClass->getMethod($method); |
|
66 | + $classProperties = $reflectionClass->getDefaultProperties(); |
|
67 | + $skipLoginCheck = Arr::get($classProperties, 'skipLoginCheck', false); |
|
68 | + $modelName = explode('\\', $controller); |
|
69 | + $modelName = lcfirst(str_replace('Controller', '', end($modelName))); |
|
70 | + |
|
71 | + $this->processDocBlock($route, $reflectionMethod); |
|
72 | + $this->getHeaders($route, $method, $skipLoginCheck); |
|
73 | + $this->getPostData($route, $reflectionMethod, explode('\\', $reflectionClass->getName())[2], $modelName); |
|
74 | + |
|
75 | + $route['response'] = $this->getResponseObject($modelName, $route['name'], $route['returnDocBlock']); |
|
76 | + $docData['modules'][$module][] = $route; |
|
77 | + |
|
78 | + $this->getModels($modelName, $docData, $reflectionClass); |
|
79 | + } |
|
80 | + } |
|
81 | + } |
|
82 | 82 | |
83 | - $docData['errors'] = $this->getErrors(); |
|
84 | - $docData['reports'] = $this->reportService->all(); |
|
85 | - \File::put(app_path('Modules/Core/Resources/api.json'), json_encode($docData)); |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * Get list of all registered routes. |
|
90 | - * |
|
91 | - * @return collection |
|
92 | - */ |
|
93 | - protected function getRoutes() |
|
94 | - { |
|
95 | - return collect(\Route::getRoutes())->map(function ($route) { |
|
96 | - if (strpos($route->uri(), 'api/') !== false) { |
|
97 | - return [ |
|
98 | - 'method' => $route->methods()[0], |
|
99 | - 'uri' => $route->uri(), |
|
100 | - 'action' => $route->getActionName(), |
|
101 | - 'prefix' => $route->getPrefix() |
|
102 | - ]; |
|
103 | - } |
|
104 | - return false; |
|
105 | - })->all(); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Generate headers for the given route. |
|
110 | - * |
|
111 | - * @param array &$route |
|
112 | - * @param string $method |
|
113 | - * @param array $skipLoginCheck |
|
114 | - * @return void |
|
115 | - */ |
|
116 | - protected function getHeaders(&$route, $method, $skipLoginCheck) |
|
117 | - { |
|
118 | - $route['headers'] = [ |
|
119 | - 'Accept' => 'application/json', |
|
120 | - 'Content-Type' => 'application/json', |
|
121 | - 'Accept-Language' => 'The language of the returned data: ar, en or all.', |
|
122 | - 'time-zone' => 'Your locale time zone', |
|
123 | - ]; |
|
124 | - |
|
125 | - |
|
126 | - if (! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) { |
|
127 | - $route['headers']['Authorization'] = 'Bearer {token}'; |
|
128 | - } |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Generate description and params for the given route |
|
133 | - * based on the docblock. |
|
134 | - * |
|
135 | - * @param array &$route |
|
136 | - * @param \ReflectionMethod $reflectionMethod |
|
137 | - * @return void |
|
138 | - */ |
|
139 | - protected function processDocBlock(&$route, $reflectionMethod) |
|
140 | - { |
|
141 | - $factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance(); |
|
142 | - $docblock = $factory->create($reflectionMethod->getDocComment()); |
|
143 | - $route['description'] = trim(preg_replace('/\s+/', ' ', $docblock->getSummary())); |
|
144 | - $params = $docblock->getTagsByName('param'); |
|
145 | - $route['returnDocBlock'] = $docblock->getTagsByName('return')[0]->getType()->getFqsen()->getName(); |
|
146 | - |
|
147 | - foreach ($params as $param) { |
|
148 | - $name = $param->getVariableName(); |
|
149 | - if ($name == 'perPage') { |
|
150 | - $route['parametars'][$name] = 'perPage? number of records per page default 15'; |
|
151 | - } elseif ($name == 'sortBy') { |
|
152 | - $route['parametars'][$name] = 'sortBy? sort column default created_at'; |
|
153 | - } elseif ($name == 'desc') { |
|
154 | - $route['parametars'][$name] = 'desc? sort descending or ascending default is descending'; |
|
155 | - } elseif ($name !== 'request') { |
|
156 | - $route['parametars'][$name] = $param->getDescription()->render(); |
|
157 | - } |
|
158 | - } |
|
159 | - |
|
160 | - if ($route['name'] === 'index') { |
|
161 | - $route['parametars']['perPage'] = 'perPage? number of records per page default 15'; |
|
162 | - $route['parametars']['sortBy'] = 'sortBy? sort column default created_at'; |
|
163 | - $route['parametars']['desc'] = 'desc? sort descending or ascending default is descending'; |
|
164 | - $route['parametars']['trashed'] = 'trashed? retreive trashed or not default not'; |
|
165 | - } |
|
166 | - } |
|
167 | - |
|
168 | - /** |
|
169 | - * Generate post body for the given route. |
|
170 | - * |
|
171 | - * @param array &$route |
|
172 | - * @param \ReflectionMethod $reflectionMethod |
|
173 | - * @param string $module |
|
174 | - * @param string $modelName |
|
175 | - * @return void |
|
176 | - */ |
|
177 | - protected function getPostData(&$route, $reflectionMethod, $module, $modelName) |
|
178 | - { |
|
179 | - request()->setMethod($route['method']); |
|
180 | - $parameters = $reflectionMethod->getParameters(); |
|
181 | - $className = optional(optional(\Arr::get($parameters, 0))->getType())->getName(); |
|
182 | - if ($className) { |
|
183 | - $reflectionClass = new \ReflectionClass($className); |
|
184 | - } elseif (in_array($reflectionMethod->getName(), ['store', 'update'])) { |
|
185 | - $className = 'App\\Modules\\' . ucfirst($module) . '\\Http\\Requests\\Store' . ucfirst($modelName); |
|
186 | - $reflectionClass = new \ReflectionClass($className); |
|
187 | - } |
|
188 | - |
|
189 | - if (isset($reflectionClass) && $reflectionClass->hasMethod('rules')) { |
|
190 | - $reflectionMethod = $reflectionClass->getMethod('rules'); |
|
191 | - $route['body'] = $reflectionMethod->invoke(new $className); |
|
192 | - |
|
193 | - foreach ($route['body'] as &$rule) { |
|
194 | - if (strpos($rule, 'unique')) { |
|
195 | - $rule = substr($rule, 0, strpos($rule, 'unique') + 6); |
|
196 | - } elseif (strpos($rule, 'exists')) { |
|
197 | - $rule = substr($rule, 0, strpos($rule, 'exists') - 1); |
|
198 | - } |
|
199 | - } |
|
200 | - } |
|
201 | - } |
|
202 | - |
|
203 | - /** |
|
204 | - * Generate application errors. |
|
205 | - * |
|
206 | - * @return array |
|
207 | - */ |
|
208 | - protected function getErrors() |
|
209 | - { |
|
210 | - $errors = []; |
|
211 | - foreach (\Module::all() as $module) { |
|
212 | - $nameSpace = 'App\\Modules\\' . $module['basename']; |
|
213 | - $class = $nameSpace . '\\Errors\\' . $module['basename'] . 'Errors'; |
|
214 | - $reflectionClass = new \ReflectionClass($class); |
|
215 | - foreach ($reflectionClass->getMethods() as $method) { |
|
216 | - $methodName = $method->name; |
|
217 | - $reflectionMethod = $reflectionClass->getMethod($methodName); |
|
218 | - $body = $this->getMethodBody($reflectionMethod); |
|
219 | - |
|
220 | - preg_match('/abort\(([^#]+)\,/iU', $body, $match); |
|
221 | - |
|
222 | - if (count($match)) { |
|
223 | - $errors[$match[1]][] = $methodName; |
|
224 | - } |
|
225 | - } |
|
226 | - } |
|
227 | - |
|
228 | - return $errors; |
|
229 | - } |
|
230 | - |
|
231 | - /** |
|
232 | - * Get the given method body code. |
|
233 | - * |
|
234 | - * @param object $reflectionMethod |
|
235 | - * @return string |
|
236 | - */ |
|
237 | - protected function getMethodBody($reflectionMethod) |
|
238 | - { |
|
239 | - $filename = $reflectionMethod->getFileName(); |
|
240 | - $start_line = $reflectionMethod->getStartLine() - 1; |
|
241 | - $end_line = $reflectionMethod->getEndLine(); |
|
242 | - $length = $end_line - $start_line; |
|
243 | - $source = file($filename); |
|
244 | - $body = implode("", array_slice($source, $start_line, $length)); |
|
245 | - $body = trim(preg_replace('/\s+/', '', $body)); |
|
246 | - |
|
247 | - return $body; |
|
248 | - } |
|
249 | - |
|
250 | - /** |
|
251 | - * Get example object of all availble models. |
|
252 | - * |
|
253 | - * @param string $modelName |
|
254 | - * @param array $docData |
|
255 | - * @return string |
|
256 | - */ |
|
257 | - protected function getModels($modelName, &$docData, $reflectionClass) |
|
258 | - { |
|
259 | - if ($modelName && ! Arr::has($docData['models'], $modelName)) { |
|
260 | - $repo = call_user_func_array("\Core::{$modelName}", []); |
|
261 | - if (! $repo) { |
|
262 | - return; |
|
263 | - } |
|
83 | + $docData['errors'] = $this->getErrors(); |
|
84 | + $docData['reports'] = $this->reportService->all(); |
|
85 | + \File::put(app_path('Modules/Core/Resources/api.json'), json_encode($docData)); |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * Get list of all registered routes. |
|
90 | + * |
|
91 | + * @return collection |
|
92 | + */ |
|
93 | + protected function getRoutes() |
|
94 | + { |
|
95 | + return collect(\Route::getRoutes())->map(function ($route) { |
|
96 | + if (strpos($route->uri(), 'api/') !== false) { |
|
97 | + return [ |
|
98 | + 'method' => $route->methods()[0], |
|
99 | + 'uri' => $route->uri(), |
|
100 | + 'action' => $route->getActionName(), |
|
101 | + 'prefix' => $route->getPrefix() |
|
102 | + ]; |
|
103 | + } |
|
104 | + return false; |
|
105 | + })->all(); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Generate headers for the given route. |
|
110 | + * |
|
111 | + * @param array &$route |
|
112 | + * @param string $method |
|
113 | + * @param array $skipLoginCheck |
|
114 | + * @return void |
|
115 | + */ |
|
116 | + protected function getHeaders(&$route, $method, $skipLoginCheck) |
|
117 | + { |
|
118 | + $route['headers'] = [ |
|
119 | + 'Accept' => 'application/json', |
|
120 | + 'Content-Type' => 'application/json', |
|
121 | + 'Accept-Language' => 'The language of the returned data: ar, en or all.', |
|
122 | + 'time-zone' => 'Your locale time zone', |
|
123 | + ]; |
|
124 | + |
|
125 | + |
|
126 | + if (! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) { |
|
127 | + $route['headers']['Authorization'] = 'Bearer {token}'; |
|
128 | + } |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Generate description and params for the given route |
|
133 | + * based on the docblock. |
|
134 | + * |
|
135 | + * @param array &$route |
|
136 | + * @param \ReflectionMethod $reflectionMethod |
|
137 | + * @return void |
|
138 | + */ |
|
139 | + protected function processDocBlock(&$route, $reflectionMethod) |
|
140 | + { |
|
141 | + $factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance(); |
|
142 | + $docblock = $factory->create($reflectionMethod->getDocComment()); |
|
143 | + $route['description'] = trim(preg_replace('/\s+/', ' ', $docblock->getSummary())); |
|
144 | + $params = $docblock->getTagsByName('param'); |
|
145 | + $route['returnDocBlock'] = $docblock->getTagsByName('return')[0]->getType()->getFqsen()->getName(); |
|
146 | + |
|
147 | + foreach ($params as $param) { |
|
148 | + $name = $param->getVariableName(); |
|
149 | + if ($name == 'perPage') { |
|
150 | + $route['parametars'][$name] = 'perPage? number of records per page default 15'; |
|
151 | + } elseif ($name == 'sortBy') { |
|
152 | + $route['parametars'][$name] = 'sortBy? sort column default created_at'; |
|
153 | + } elseif ($name == 'desc') { |
|
154 | + $route['parametars'][$name] = 'desc? sort descending or ascending default is descending'; |
|
155 | + } elseif ($name !== 'request') { |
|
156 | + $route['parametars'][$name] = $param->getDescription()->render(); |
|
157 | + } |
|
158 | + } |
|
159 | + |
|
160 | + if ($route['name'] === 'index') { |
|
161 | + $route['parametars']['perPage'] = 'perPage? number of records per page default 15'; |
|
162 | + $route['parametars']['sortBy'] = 'sortBy? sort column default created_at'; |
|
163 | + $route['parametars']['desc'] = 'desc? sort descending or ascending default is descending'; |
|
164 | + $route['parametars']['trashed'] = 'trashed? retreive trashed or not default not'; |
|
165 | + } |
|
166 | + } |
|
167 | + |
|
168 | + /** |
|
169 | + * Generate post body for the given route. |
|
170 | + * |
|
171 | + * @param array &$route |
|
172 | + * @param \ReflectionMethod $reflectionMethod |
|
173 | + * @param string $module |
|
174 | + * @param string $modelName |
|
175 | + * @return void |
|
176 | + */ |
|
177 | + protected function getPostData(&$route, $reflectionMethod, $module, $modelName) |
|
178 | + { |
|
179 | + request()->setMethod($route['method']); |
|
180 | + $parameters = $reflectionMethod->getParameters(); |
|
181 | + $className = optional(optional(\Arr::get($parameters, 0))->getType())->getName(); |
|
182 | + if ($className) { |
|
183 | + $reflectionClass = new \ReflectionClass($className); |
|
184 | + } elseif (in_array($reflectionMethod->getName(), ['store', 'update'])) { |
|
185 | + $className = 'App\\Modules\\' . ucfirst($module) . '\\Http\\Requests\\Store' . ucfirst($modelName); |
|
186 | + $reflectionClass = new \ReflectionClass($className); |
|
187 | + } |
|
188 | + |
|
189 | + if (isset($reflectionClass) && $reflectionClass->hasMethod('rules')) { |
|
190 | + $reflectionMethod = $reflectionClass->getMethod('rules'); |
|
191 | + $route['body'] = $reflectionMethod->invoke(new $className); |
|
192 | + |
|
193 | + foreach ($route['body'] as &$rule) { |
|
194 | + if (strpos($rule, 'unique')) { |
|
195 | + $rule = substr($rule, 0, strpos($rule, 'unique') + 6); |
|
196 | + } elseif (strpos($rule, 'exists')) { |
|
197 | + $rule = substr($rule, 0, strpos($rule, 'exists') - 1); |
|
198 | + } |
|
199 | + } |
|
200 | + } |
|
201 | + } |
|
202 | + |
|
203 | + /** |
|
204 | + * Generate application errors. |
|
205 | + * |
|
206 | + * @return array |
|
207 | + */ |
|
208 | + protected function getErrors() |
|
209 | + { |
|
210 | + $errors = []; |
|
211 | + foreach (\Module::all() as $module) { |
|
212 | + $nameSpace = 'App\\Modules\\' . $module['basename']; |
|
213 | + $class = $nameSpace . '\\Errors\\' . $module['basename'] . 'Errors'; |
|
214 | + $reflectionClass = new \ReflectionClass($class); |
|
215 | + foreach ($reflectionClass->getMethods() as $method) { |
|
216 | + $methodName = $method->name; |
|
217 | + $reflectionMethod = $reflectionClass->getMethod($methodName); |
|
218 | + $body = $this->getMethodBody($reflectionMethod); |
|
219 | + |
|
220 | + preg_match('/abort\(([^#]+)\,/iU', $body, $match); |
|
221 | + |
|
222 | + if (count($match)) { |
|
223 | + $errors[$match[1]][] = $methodName; |
|
224 | + } |
|
225 | + } |
|
226 | + } |
|
227 | + |
|
228 | + return $errors; |
|
229 | + } |
|
230 | + |
|
231 | + /** |
|
232 | + * Get the given method body code. |
|
233 | + * |
|
234 | + * @param object $reflectionMethod |
|
235 | + * @return string |
|
236 | + */ |
|
237 | + protected function getMethodBody($reflectionMethod) |
|
238 | + { |
|
239 | + $filename = $reflectionMethod->getFileName(); |
|
240 | + $start_line = $reflectionMethod->getStartLine() - 1; |
|
241 | + $end_line = $reflectionMethod->getEndLine(); |
|
242 | + $length = $end_line - $start_line; |
|
243 | + $source = file($filename); |
|
244 | + $body = implode("", array_slice($source, $start_line, $length)); |
|
245 | + $body = trim(preg_replace('/\s+/', '', $body)); |
|
246 | + |
|
247 | + return $body; |
|
248 | + } |
|
249 | + |
|
250 | + /** |
|
251 | + * Get example object of all availble models. |
|
252 | + * |
|
253 | + * @param string $modelName |
|
254 | + * @param array $docData |
|
255 | + * @return string |
|
256 | + */ |
|
257 | + protected function getModels($modelName, &$docData, $reflectionClass) |
|
258 | + { |
|
259 | + if ($modelName && ! Arr::has($docData['models'], $modelName)) { |
|
260 | + $repo = call_user_func_array("\Core::{$modelName}", []); |
|
261 | + if (! $repo) { |
|
262 | + return; |
|
263 | + } |
|
264 | 264 | |
265 | - $modelClass = get_class($repo->model); |
|
266 | - $model = factory($modelClass)->make(); |
|
267 | - |
|
268 | - $property = $reflectionClass->getProperty('modelResource'); |
|
269 | - $property->setAccessible(true); |
|
270 | - $modelResource = $property->getValue(\App::make($reflectionClass->getName())); |
|
271 | - |
|
272 | - $relations = []; |
|
273 | - $relationMethods = ['hasOne', 'hasMany', 'belongsTo', 'belongsToMany', 'morphToMany', 'morphTo']; |
|
274 | - $reflector = new \ReflectionClass($model); |
|
275 | - foreach ($reflector->getMethods() as $reflectionMethod) { |
|
276 | - $body = $this->getMethodBody($reflectionMethod); |
|
277 | - foreach ($relationMethods as $relationMethod) { |
|
278 | - $relation = $reflectionMethod->getName(); |
|
279 | - if (strpos($body, '$this->' . $relationMethod) && $relation !== 'morphedByMany') { |
|
280 | - $relations[] = $relation; |
|
281 | - break; |
|
282 | - } |
|
283 | - } |
|
284 | - } |
|
285 | - |
|
286 | - $modelResource = new $modelResource($model->load($relations)); |
|
287 | - $modelArr = $modelResource->toArray([]); |
|
288 | - |
|
289 | - foreach ($modelArr as $key => $attr) { |
|
290 | - if (is_object($attr) && property_exists($attr, 'resource') && $attr->resource instanceof \Illuminate\Http\Resources\MissingValue) { |
|
291 | - unset($modelArr[$key]); |
|
292 | - } |
|
293 | - } |
|
294 | - |
|
295 | - $docData['models'][$modelName] = json_encode($modelArr, JSON_PRETTY_PRINT); |
|
296 | - } |
|
297 | - } |
|
298 | - |
|
299 | - /** |
|
300 | - * Get the route response object type. |
|
301 | - * |
|
302 | - * @param string $modelName |
|
303 | - * @param string $method |
|
304 | - * @param string $returnDocBlock |
|
305 | - * @return array |
|
306 | - */ |
|
307 | - protected function getResponseObject($modelName, $method, $returnDocBlock) |
|
308 | - { |
|
309 | - $relations = config('core.relations'); |
|
310 | - $relations = Arr::has($relations, $modelName) ? Arr::has($relations[$modelName], $method) ? $relations[$modelName] : false : false; |
|
311 | - $modelName = call_user_func_array("\Core::{$returnDocBlock}", []) ? $returnDocBlock : $modelName; |
|
312 | - |
|
313 | - return $relations ? [$modelName => $relations && $relations[$method] ? $relations[$method] : []] : false; |
|
314 | - } |
|
265 | + $modelClass = get_class($repo->model); |
|
266 | + $model = factory($modelClass)->make(); |
|
267 | + |
|
268 | + $property = $reflectionClass->getProperty('modelResource'); |
|
269 | + $property->setAccessible(true); |
|
270 | + $modelResource = $property->getValue(\App::make($reflectionClass->getName())); |
|
271 | + |
|
272 | + $relations = []; |
|
273 | + $relationMethods = ['hasOne', 'hasMany', 'belongsTo', 'belongsToMany', 'morphToMany', 'morphTo']; |
|
274 | + $reflector = new \ReflectionClass($model); |
|
275 | + foreach ($reflector->getMethods() as $reflectionMethod) { |
|
276 | + $body = $this->getMethodBody($reflectionMethod); |
|
277 | + foreach ($relationMethods as $relationMethod) { |
|
278 | + $relation = $reflectionMethod->getName(); |
|
279 | + if (strpos($body, '$this->' . $relationMethod) && $relation !== 'morphedByMany') { |
|
280 | + $relations[] = $relation; |
|
281 | + break; |
|
282 | + } |
|
283 | + } |
|
284 | + } |
|
285 | + |
|
286 | + $modelResource = new $modelResource($model->load($relations)); |
|
287 | + $modelArr = $modelResource->toArray([]); |
|
288 | + |
|
289 | + foreach ($modelArr as $key => $attr) { |
|
290 | + if (is_object($attr) && property_exists($attr, 'resource') && $attr->resource instanceof \Illuminate\Http\Resources\MissingValue) { |
|
291 | + unset($modelArr[$key]); |
|
292 | + } |
|
293 | + } |
|
294 | + |
|
295 | + $docData['models'][$modelName] = json_encode($modelArr, JSON_PRETTY_PRINT); |
|
296 | + } |
|
297 | + } |
|
298 | + |
|
299 | + /** |
|
300 | + * Get the route response object type. |
|
301 | + * |
|
302 | + * @param string $modelName |
|
303 | + * @param string $method |
|
304 | + * @param string $returnDocBlock |
|
305 | + * @return array |
|
306 | + */ |
|
307 | + protected function getResponseObject($modelName, $method, $returnDocBlock) |
|
308 | + { |
|
309 | + $relations = config('core.relations'); |
|
310 | + $relations = Arr::has($relations, $modelName) ? Arr::has($relations[$modelName], $method) ? $relations[$modelName] : false : false; |
|
311 | + $modelName = call_user_func_array("\Core::{$returnDocBlock}", []) ? $returnDocBlock : $modelName; |
|
312 | + |
|
313 | + return $relations ? [$modelName => $relations && $relations[$method] ? $relations[$method] : []] : false; |
|
314 | + } |
|
315 | 315 | } |
@@ -6,26 +6,26 @@ |
||
6 | 6 | |
7 | 7 | class StoreRole extends FormRequest |
8 | 8 | { |
9 | - /** |
|
10 | - * Determine if the user is authorized to make this request. |
|
11 | - * |
|
12 | - * @return bool |
|
13 | - */ |
|
14 | - public function authorize() |
|
15 | - { |
|
16 | - return true; |
|
17 | - } |
|
9 | + /** |
|
10 | + * Determine if the user is authorized to make this request. |
|
11 | + * |
|
12 | + * @return bool |
|
13 | + */ |
|
14 | + public function authorize() |
|
15 | + { |
|
16 | + return true; |
|
17 | + } |
|
18 | 18 | |
19 | - /** |
|
20 | - * Get the validation rules that apply to the request. |
|
21 | - * |
|
22 | - * @return array |
|
23 | - */ |
|
24 | - public function rules() |
|
25 | - { |
|
26 | - $requiredOrNullable = request()->isMethod('PATCH') ? 'nullable' : 'required' . ''; |
|
27 | - return [ |
|
28 | - 'name' => $requiredOrNullable . '|string|max:100|unique:roles,name,' . $this->route('id') |
|
29 | - ]; |
|
30 | - } |
|
19 | + /** |
|
20 | + * Get the validation rules that apply to the request. |
|
21 | + * |
|
22 | + * @return array |
|
23 | + */ |
|
24 | + public function rules() |
|
25 | + { |
|
26 | + $requiredOrNullable = request()->isMethod('PATCH') ? 'nullable' : 'required' . ''; |
|
27 | + return [ |
|
28 | + 'name' => $requiredOrNullable . '|string|max:100|unique:roles,name,' . $this->route('id') |
|
29 | + ]; |
|
30 | + } |
|
31 | 31 | } |
@@ -23,9 +23,9 @@ |
||
23 | 23 | */ |
24 | 24 | public function rules() |
25 | 25 | { |
26 | - $requiredOrNullable = request()->isMethod('PATCH') ? 'nullable' : 'required' . ''; |
|
26 | + $requiredOrNullable = request()->isMethod('PATCH') ? 'nullable' : 'required'.''; |
|
27 | 27 | return [ |
28 | - 'name' => $requiredOrNullable . '|string|max:100|unique:roles,name,' . $this->route('id') |
|
28 | + 'name' => $requiredOrNullable.'|string|max:100|unique:roles,name,'.$this->route('id') |
|
29 | 29 | ]; |
30 | 30 | } |
31 | 31 | } |
@@ -6,29 +6,29 @@ |
||
6 | 6 | |
7 | 7 | class StoreOauthClient extends FormRequest |
8 | 8 | { |
9 | - /** |
|
10 | - * Determine if the user is authorized to make this request. |
|
11 | - * |
|
12 | - * @return bool |
|
13 | - */ |
|
14 | - public function authorize() |
|
15 | - { |
|
16 | - return true; |
|
17 | - } |
|
9 | + /** |
|
10 | + * Determine if the user is authorized to make this request. |
|
11 | + * |
|
12 | + * @return bool |
|
13 | + */ |
|
14 | + public function authorize() |
|
15 | + { |
|
16 | + return true; |
|
17 | + } |
|
18 | 18 | |
19 | - /** |
|
20 | - * Get the validation rules that apply to the request. |
|
21 | - * |
|
22 | - * @return array |
|
23 | - */ |
|
24 | - public function rules() |
|
25 | - { |
|
26 | - $requiredOrNullable = request()->isMethod('PATCH') ? 'nullable' : 'required' . ''; |
|
27 | - return [ |
|
28 | - 'name' => $requiredOrNullable . '|max:255', |
|
29 | - 'redirect' => $requiredOrNullable . '|url', |
|
30 | - 'user_id' => $requiredOrNullable . '|exists:users,id', |
|
31 | - 'revoked' => 'boolean' |
|
32 | - ]; |
|
33 | - } |
|
19 | + /** |
|
20 | + * Get the validation rules that apply to the request. |
|
21 | + * |
|
22 | + * @return array |
|
23 | + */ |
|
24 | + public function rules() |
|
25 | + { |
|
26 | + $requiredOrNullable = request()->isMethod('PATCH') ? 'nullable' : 'required' . ''; |
|
27 | + return [ |
|
28 | + 'name' => $requiredOrNullable . '|max:255', |
|
29 | + 'redirect' => $requiredOrNullable . '|url', |
|
30 | + 'user_id' => $requiredOrNullable . '|exists:users,id', |
|
31 | + 'revoked' => 'boolean' |
|
32 | + ]; |
|
33 | + } |
|
34 | 34 | } |
@@ -23,11 +23,11 @@ |
||
23 | 23 | */ |
24 | 24 | public function rules() |
25 | 25 | { |
26 | - $requiredOrNullable = request()->isMethod('PATCH') ? 'nullable' : 'required' . ''; |
|
26 | + $requiredOrNullable = request()->isMethod('PATCH') ? 'nullable' : 'required'.''; |
|
27 | 27 | return [ |
28 | - 'name' => $requiredOrNullable . '|max:255', |
|
29 | - 'redirect' => $requiredOrNullable . '|url', |
|
30 | - 'user_id' => $requiredOrNullable . '|exists:users,id', |
|
28 | + 'name' => $requiredOrNullable.'|max:255', |
|
29 | + 'redirect' => $requiredOrNullable.'|url', |
|
30 | + 'user_id' => $requiredOrNullable.'|exists:users,id', |
|
31 | 31 | 'revoked' => 'boolean' |
32 | 32 | ]; |
33 | 33 | } |
@@ -6,28 +6,28 @@ |
||
6 | 6 | |
7 | 7 | class StoreUser extends FormRequest |
8 | 8 | { |
9 | - /** |
|
10 | - * Determine if the user is authorized to make this request. |
|
11 | - * |
|
12 | - * @return bool |
|
13 | - */ |
|
14 | - public function authorize() |
|
15 | - { |
|
16 | - return true; |
|
17 | - } |
|
9 | + /** |
|
10 | + * Determine if the user is authorized to make this request. |
|
11 | + * |
|
12 | + * @return bool |
|
13 | + */ |
|
14 | + public function authorize() |
|
15 | + { |
|
16 | + return true; |
|
17 | + } |
|
18 | 18 | |
19 | - /** |
|
20 | - * Get the validation rules that apply to the request. |
|
21 | - * |
|
22 | - * @return array |
|
23 | - */ |
|
24 | - public function rules() |
|
25 | - { |
|
26 | - $requiredOrNullable = request()->isMethod('PATCH') ? 'nullable' : 'required' . ''; |
|
27 | - return [ |
|
28 | - 'name' => 'nullable|string', |
|
29 | - 'email' => $requiredOrNullable . '|email|unique:users,email,' . $this->route('id'), |
|
30 | - 'password' => 'nullable|min:6' |
|
31 | - ]; |
|
32 | - } |
|
19 | + /** |
|
20 | + * Get the validation rules that apply to the request. |
|
21 | + * |
|
22 | + * @return array |
|
23 | + */ |
|
24 | + public function rules() |
|
25 | + { |
|
26 | + $requiredOrNullable = request()->isMethod('PATCH') ? 'nullable' : 'required' . ''; |
|
27 | + return [ |
|
28 | + 'name' => 'nullable|string', |
|
29 | + 'email' => $requiredOrNullable . '|email|unique:users,email,' . $this->route('id'), |
|
30 | + 'password' => 'nullable|min:6' |
|
31 | + ]; |
|
32 | + } |
|
33 | 33 | } |
@@ -23,10 +23,10 @@ |
||
23 | 23 | */ |
24 | 24 | public function rules() |
25 | 25 | { |
26 | - $requiredOrNullable = request()->isMethod('PATCH') ? 'nullable' : 'required' . ''; |
|
26 | + $requiredOrNullable = request()->isMethod('PATCH') ? 'nullable' : 'required'.''; |
|
27 | 27 | return [ |
28 | 28 | 'name' => 'nullable|string', |
29 | - 'email' => $requiredOrNullable . '|email|unique:users,email,' . $this->route('id'), |
|
29 | + 'email' => $requiredOrNullable.'|email|unique:users,email,'.$this->route('id'), |
|
30 | 30 | 'password' => 'nullable|min:6' |
31 | 31 | ]; |
32 | 32 | } |
@@ -6,27 +6,27 @@ |
||
6 | 6 | |
7 | 7 | class StorePushNotificationDevice extends FormRequest |
8 | 8 | { |
9 | - /** |
|
10 | - * Determine if the user is authorized to make this request. |
|
11 | - * |
|
12 | - * @return bool |
|
13 | - */ |
|
14 | - public function authorize() |
|
15 | - { |
|
16 | - return true; |
|
17 | - } |
|
9 | + /** |
|
10 | + * Determine if the user is authorized to make this request. |
|
11 | + * |
|
12 | + * @return bool |
|
13 | + */ |
|
14 | + public function authorize() |
|
15 | + { |
|
16 | + return true; |
|
17 | + } |
|
18 | 18 | |
19 | - /** |
|
20 | - * Get the validation rules that apply to the request. |
|
21 | - * |
|
22 | - * @return array |
|
23 | - */ |
|
24 | - public function rules() |
|
25 | - { |
|
26 | - $requiredOrNullable = request()->isMethod('PATCH') ? 'nullable' : 'required' . ''; |
|
27 | - return [ |
|
28 | - 'device_token' => $requiredOrNullable . '|string|max:255', |
|
29 | - 'user_id' => $requiredOrNullable . '|exists:users,id' |
|
30 | - ]; |
|
31 | - } |
|
19 | + /** |
|
20 | + * Get the validation rules that apply to the request. |
|
21 | + * |
|
22 | + * @return array |
|
23 | + */ |
|
24 | + public function rules() |
|
25 | + { |
|
26 | + $requiredOrNullable = request()->isMethod('PATCH') ? 'nullable' : 'required' . ''; |
|
27 | + return [ |
|
28 | + 'device_token' => $requiredOrNullable . '|string|max:255', |
|
29 | + 'user_id' => $requiredOrNullable . '|exists:users,id' |
|
30 | + ]; |
|
31 | + } |
|
32 | 32 | } |
@@ -23,10 +23,10 @@ |
||
23 | 23 | */ |
24 | 24 | public function rules() |
25 | 25 | { |
26 | - $requiredOrNullable = request()->isMethod('PATCH') ? 'nullable' : 'required' . ''; |
|
26 | + $requiredOrNullable = request()->isMethod('PATCH') ? 'nullable' : 'required'.''; |
|
27 | 27 | return [ |
28 | - 'device_token' => $requiredOrNullable . '|string|max:255', |
|
29 | - 'user_id' => $requiredOrNullable . '|exists:users,id' |
|
28 | + 'device_token' => $requiredOrNullable.'|string|max:255', |
|
29 | + 'user_id' => $requiredOrNullable.'|exists:users,id' |
|
30 | 30 | ]; |
31 | 31 | } |
32 | 32 | } |