@@ -2,105 +2,105 @@ |
||
2 | 2 | |
3 | 3 | class ErrorHandler |
4 | 4 | { |
5 | - public function unAuthorized() |
|
6 | - { |
|
7 | - $error = ['status' => 401, 'message' => trans('errors.unAuthorized')]; |
|
8 | - abort($error['status'], $error['message']); |
|
9 | - } |
|
10 | - |
|
11 | - public function invalidRefreshToken() |
|
12 | - { |
|
13 | - $error = ['status' => 401, 'message' => trans('errors.invalidRefreshToken')]; |
|
14 | - abort($error['status'], $error['message']); |
|
15 | - } |
|
16 | - |
|
17 | - public function noPermissions() |
|
18 | - { |
|
19 | - $error = ['status' => 403, 'message' => trans('errors.noPermissions')]; |
|
20 | - abort($error['status'], $error['message']); |
|
21 | - } |
|
22 | - |
|
23 | - public function loginFailed() |
|
24 | - { |
|
25 | - $error = ['status' => 400, 'message' => trans('errors.loginFailed')]; |
|
26 | - abort($error['status'], $error['message']); |
|
27 | - } |
|
28 | - |
|
29 | - public function noSocialEmail() |
|
30 | - { |
|
31 | - $error = ['status' => 400, 'message' => trans('errors.noSocialEmail')]; |
|
32 | - abort($error['status'], $error['message']); |
|
33 | - } |
|
34 | - |
|
35 | - public function userAlreadyRegistered() |
|
36 | - { |
|
37 | - $error = ['status' => 400, 'message' => trans('errors.userAlreadyRegistered')]; |
|
38 | - abort($error['status'], $error['message']); |
|
39 | - } |
|
40 | - |
|
41 | - public function connectionError() |
|
42 | - { |
|
43 | - $error = ['status' => 400, 'message' => trans('errors.connectionError')]; |
|
44 | - abort($error['status'], $error['message']); |
|
45 | - } |
|
46 | - |
|
47 | - public function redisNotRunning() |
|
48 | - { |
|
49 | - $error = ['status' => 400, 'message' => trans('errors.redisNotRunning')]; |
|
50 | - abort($error['status'], $error['message']); |
|
51 | - } |
|
52 | - |
|
53 | - public function dbQueryError() |
|
54 | - { |
|
55 | - $error = ['status' => 400, 'message' => trans('errors.dbQueryError')]; |
|
56 | - abort($error['status'], $error['message']); |
|
57 | - } |
|
58 | - |
|
59 | - public function cannotCreateSetting() |
|
60 | - { |
|
61 | - $error = ['status' => 400, 'message' => trans('errors.cannotCreateSetting')]; |
|
62 | - abort($error['status'], $error['message']); |
|
63 | - } |
|
64 | - |
|
65 | - public function cannotUpdateSettingKey() |
|
66 | - { |
|
67 | - $error = ['status' => 400, 'message' => trans('errors.cannotUpdateSettingKey')]; |
|
68 | - abort($error['status'], $error['message']); |
|
69 | - } |
|
70 | - |
|
71 | - public function userIsBlocked() |
|
72 | - { |
|
73 | - $error = ['status' => 403, 'message' => trans('errors.userIsBlocked')]; |
|
74 | - abort($error['status'], $error['message']); |
|
75 | - } |
|
76 | - |
|
77 | - public function invalidResetToken() |
|
78 | - { |
|
79 | - $error = ['status' => 400, 'message' => trans('errors.invalidResetToken')]; |
|
80 | - abort($error['status'], $error['message']); |
|
81 | - } |
|
82 | - |
|
83 | - public function invalidResetPassword() |
|
84 | - { |
|
85 | - $error = ['status' => 400, 'message' => trans('errors.invalidResetPassword')]; |
|
86 | - abort($error['status'], $error['message']); |
|
87 | - } |
|
88 | - |
|
89 | - public function invalidOldPassword() |
|
90 | - { |
|
91 | - $error = ['status' => 400, 'message' => trans('errors.invalidOldPassword')]; |
|
92 | - abort($error['status'], $error['message']); |
|
93 | - } |
|
94 | - |
|
95 | - public function notFound($text) |
|
96 | - { |
|
97 | - $error = ['status' => 404, 'message' => trans('errors.notFound', ['replace' => $text])]; |
|
98 | - abort($error['status'], $error['message']); |
|
99 | - } |
|
100 | - |
|
101 | - public function generalError() |
|
102 | - { |
|
103 | - $error = ['status' => 400, 'message' => trans('errors.generalError')]; |
|
104 | - abort($error['status'], $error['message']); |
|
105 | - } |
|
5 | + public function unAuthorized() |
|
6 | + { |
|
7 | + $error = ['status' => 401, 'message' => trans('errors.unAuthorized')]; |
|
8 | + abort($error['status'], $error['message']); |
|
9 | + } |
|
10 | + |
|
11 | + public function invalidRefreshToken() |
|
12 | + { |
|
13 | + $error = ['status' => 401, 'message' => trans('errors.invalidRefreshToken')]; |
|
14 | + abort($error['status'], $error['message']); |
|
15 | + } |
|
16 | + |
|
17 | + public function noPermissions() |
|
18 | + { |
|
19 | + $error = ['status' => 403, 'message' => trans('errors.noPermissions')]; |
|
20 | + abort($error['status'], $error['message']); |
|
21 | + } |
|
22 | + |
|
23 | + public function loginFailed() |
|
24 | + { |
|
25 | + $error = ['status' => 400, 'message' => trans('errors.loginFailed')]; |
|
26 | + abort($error['status'], $error['message']); |
|
27 | + } |
|
28 | + |
|
29 | + public function noSocialEmail() |
|
30 | + { |
|
31 | + $error = ['status' => 400, 'message' => trans('errors.noSocialEmail')]; |
|
32 | + abort($error['status'], $error['message']); |
|
33 | + } |
|
34 | + |
|
35 | + public function userAlreadyRegistered() |
|
36 | + { |
|
37 | + $error = ['status' => 400, 'message' => trans('errors.userAlreadyRegistered')]; |
|
38 | + abort($error['status'], $error['message']); |
|
39 | + } |
|
40 | + |
|
41 | + public function connectionError() |
|
42 | + { |
|
43 | + $error = ['status' => 400, 'message' => trans('errors.connectionError')]; |
|
44 | + abort($error['status'], $error['message']); |
|
45 | + } |
|
46 | + |
|
47 | + public function redisNotRunning() |
|
48 | + { |
|
49 | + $error = ['status' => 400, 'message' => trans('errors.redisNotRunning')]; |
|
50 | + abort($error['status'], $error['message']); |
|
51 | + } |
|
52 | + |
|
53 | + public function dbQueryError() |
|
54 | + { |
|
55 | + $error = ['status' => 400, 'message' => trans('errors.dbQueryError')]; |
|
56 | + abort($error['status'], $error['message']); |
|
57 | + } |
|
58 | + |
|
59 | + public function cannotCreateSetting() |
|
60 | + { |
|
61 | + $error = ['status' => 400, 'message' => trans('errors.cannotCreateSetting')]; |
|
62 | + abort($error['status'], $error['message']); |
|
63 | + } |
|
64 | + |
|
65 | + public function cannotUpdateSettingKey() |
|
66 | + { |
|
67 | + $error = ['status' => 400, 'message' => trans('errors.cannotUpdateSettingKey')]; |
|
68 | + abort($error['status'], $error['message']); |
|
69 | + } |
|
70 | + |
|
71 | + public function userIsBlocked() |
|
72 | + { |
|
73 | + $error = ['status' => 403, 'message' => trans('errors.userIsBlocked')]; |
|
74 | + abort($error['status'], $error['message']); |
|
75 | + } |
|
76 | + |
|
77 | + public function invalidResetToken() |
|
78 | + { |
|
79 | + $error = ['status' => 400, 'message' => trans('errors.invalidResetToken')]; |
|
80 | + abort($error['status'], $error['message']); |
|
81 | + } |
|
82 | + |
|
83 | + public function invalidResetPassword() |
|
84 | + { |
|
85 | + $error = ['status' => 400, 'message' => trans('errors.invalidResetPassword')]; |
|
86 | + abort($error['status'], $error['message']); |
|
87 | + } |
|
88 | + |
|
89 | + public function invalidOldPassword() |
|
90 | + { |
|
91 | + $error = ['status' => 400, 'message' => trans('errors.invalidOldPassword')]; |
|
92 | + abort($error['status'], $error['message']); |
|
93 | + } |
|
94 | + |
|
95 | + public function notFound($text) |
|
96 | + { |
|
97 | + $error = ['status' => 404, 'message' => trans('errors.notFound', ['replace' => $text])]; |
|
98 | + abort($error['status'], $error['message']); |
|
99 | + } |
|
100 | + |
|
101 | + public function generalError() |
|
102 | + { |
|
103 | + $error = ['status' => 400, 'message' => trans('errors.generalError')]; |
|
104 | + abort($error['status'], $error['message']); |
|
105 | + } |
|
106 | 106 | } |
107 | 107 | \ No newline at end of file |
@@ -63,11 +63,11 @@ discard block |
||
63 | 63 | /** |
64 | 64 | * Construct the select conditions for the model. |
65 | 65 | */ |
66 | - $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
66 | + $model->where(function($q) use ($query, $conditionColumns, $relations){ |
|
67 | 67 | |
68 | 68 | if (count($conditionColumns)) |
69 | 69 | { |
70 | - $column = 'LOWER(' . array_shift($conditionColumns) . ')'; |
|
70 | + $column = 'LOWER('.array_shift($conditionColumns).')'; |
|
71 | 71 | if (str_contains($column, '->')) |
72 | 72 | { |
73 | 73 | $column = $this->wrapJsonSelector($column); |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | /** |
77 | 77 | * Use the first element in the model columns to construct the first condition. |
78 | 78 | */ |
79 | - $q->where(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
79 | + $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
80 | 80 | } |
81 | 81 | |
82 | 82 | /** |
@@ -84,13 +84,13 @@ discard block |
||
84 | 84 | */ |
85 | 85 | foreach ($conditionColumns as $column) |
86 | 86 | { |
87 | - $column = 'LOWER(' . $column . ')'; |
|
87 | + $column = 'LOWER('.$column.')'; |
|
88 | 88 | if (str_contains($column, '->')) |
89 | 89 | { |
90 | 90 | $column = $this->wrapJsonSelector($column); |
91 | 91 | } |
92 | 92 | |
93 | - $q->orWhere(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
93 | + $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
94 | 94 | } |
95 | 95 | |
96 | 96 | /** |
@@ -111,9 +111,9 @@ discard block |
||
111 | 111 | /** |
112 | 112 | * Construct the relation condition. |
113 | 113 | */ |
114 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
114 | + $q->orWhereHas($relation, function($subModel) use ($query, $relation){ |
|
115 | 115 | |
116 | - $subModel->where(function ($q) use ($query, $relation){ |
|
116 | + $subModel->where(function($q) use ($query, $relation){ |
|
117 | 117 | |
118 | 118 | /** |
119 | 119 | * Get columns of the relation. |
@@ -123,7 +123,7 @@ discard block |
||
123 | 123 | if (count($subConditionColumns)) |
124 | 124 | { |
125 | 125 | |
126 | - $column = 'LOWER(' . array_shift($subConditionColumns) . ')'; |
|
126 | + $column = 'LOWER('.array_shift($subConditionColumns).')'; |
|
127 | 127 | if (str_contains($column, '->')) |
128 | 128 | { |
129 | 129 | $column = $this->wrapJsonSelector($column); |
@@ -132,7 +132,7 @@ discard block |
||
132 | 132 | /** |
133 | 133 | * Use the first element in the relation model columns to construct the first condition. |
134 | 134 | */ |
135 | - $q->where(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
135 | + $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
136 | 136 | } |
137 | 137 | |
138 | 138 | /** |
@@ -140,13 +140,13 @@ discard block |
||
140 | 140 | */ |
141 | 141 | foreach ($subConditionColumns as $subConditionColumn) |
142 | 142 | { |
143 | - $column = 'LOWER(' . $subConditionColumn . ')'; |
|
143 | + $column = 'LOWER('.$subConditionColumn.')'; |
|
144 | 144 | if (str_contains($column, '->')) |
145 | 145 | { |
146 | 146 | $column = $this->wrapJsonSelector($column); |
147 | 147 | } |
148 | 148 | |
149 | - $q->orWhere(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
149 | + $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
150 | 150 | } |
151 | 151 | }); |
152 | 152 | |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | $modelClass = $this->model; |
207 | 207 | $relations = []; |
208 | 208 | |
209 | - \DB::transaction(function () use (&$model, &$relations, $data, $modelClass) { |
|
209 | + \DB::transaction(function() use (&$model, &$relations, $data, $modelClass) { |
|
210 | 210 | /** |
211 | 211 | * If the id is present in the data then select the model for updating, |
212 | 212 | * else create new model. |
@@ -215,7 +215,7 @@ discard block |
||
215 | 215 | $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
216 | 216 | if ( ! $model) |
217 | 217 | { |
218 | - \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
218 | + \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
219 | 219 | } |
220 | 220 | |
221 | 221 | /** |
@@ -273,7 +273,7 @@ discard block |
||
273 | 273 | */ |
274 | 274 | if ( ! $relationModel) |
275 | 275 | { |
276 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
276 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
277 | 277 | } |
278 | 278 | |
279 | 279 | /** |
@@ -284,7 +284,7 @@ discard block |
||
284 | 284 | /** |
285 | 285 | * Prevent the sub relations or attributes not in the fillable. |
286 | 286 | */ |
287 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
287 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
288 | 288 | { |
289 | 289 | $relationModel->$attr = $val; |
290 | 290 | } |
@@ -314,7 +314,7 @@ discard block |
||
314 | 314 | */ |
315 | 315 | if ( ! $relationModel) |
316 | 316 | { |
317 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
317 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
318 | 318 | } |
319 | 319 | |
320 | 320 | foreach ($value as $relationAttribute => $relationValue) |
@@ -456,7 +456,7 @@ discard block |
||
456 | 456 | } |
457 | 457 | else |
458 | 458 | { |
459 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data){ |
|
459 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function($model) use ($data){ |
|
460 | 460 | $model->update($data); |
461 | 461 | }); |
462 | 462 | } |
@@ -474,11 +474,11 @@ discard block |
||
474 | 474 | { |
475 | 475 | if ($attribute == 'id') |
476 | 476 | { |
477 | - \DB::transaction(function () use ($value, $attribute, &$result) { |
|
477 | + \DB::transaction(function() use ($value, $attribute, &$result) { |
|
478 | 478 | $model = $this->model->lockForUpdate()->find($value); |
479 | 479 | if ( ! $model) |
480 | 480 | { |
481 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
481 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$value); |
|
482 | 482 | } |
483 | 483 | |
484 | 484 | $model->delete(); |
@@ -486,8 +486,8 @@ discard block |
||
486 | 486 | } |
487 | 487 | else |
488 | 488 | { |
489 | - \DB::transaction(function () use ($value, $attribute, &$result) { |
|
490 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
489 | + \DB::transaction(function() use ($value, $attribute, &$result) { |
|
490 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function($model) { |
|
491 | 491 | $model->delete(); |
492 | 492 | }); |
493 | 493 | }); |
@@ -523,7 +523,7 @@ discard block |
||
523 | 523 | { |
524 | 524 | $conditions = $this->constructConditions($conditions, $this->model); |
525 | 525 | $sort = $desc ? 'desc' : 'asc'; |
526 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
526 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
527 | 527 | } |
528 | 528 | |
529 | 529 | /** |
@@ -563,7 +563,7 @@ discard block |
||
563 | 563 | $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
564 | 564 | } |
565 | 565 | |
566 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
566 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); ; |
|
567 | 567 | } |
568 | 568 | |
569 | 569 | /** |
@@ -578,7 +578,7 @@ discard block |
||
578 | 578 | |
579 | 579 | if ( ! $model) |
580 | 580 | { |
581 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
581 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$id); |
|
582 | 582 | } |
583 | 583 | |
584 | 584 | $model->restore(); |
@@ -603,13 +603,13 @@ discard block |
||
603 | 603 | if ($key == 'and') |
604 | 604 | { |
605 | 605 | $conditions = $this->constructConditions($value, $model); |
606 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
606 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
607 | 607 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
608 | 608 | } |
609 | 609 | else if ($key == 'or') |
610 | 610 | { |
611 | 611 | $conditions = $this->constructConditions($value, $model); |
612 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
612 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
613 | 613 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
614 | 614 | } |
615 | 615 | else |
@@ -634,41 +634,41 @@ discard block |
||
634 | 634 | |
635 | 635 | if (strtolower($operator) == 'between') |
636 | 636 | { |
637 | - $conditionString .= $key . ' >= ? and '; |
|
637 | + $conditionString .= $key.' >= ? and '; |
|
638 | 638 | $conditionValues[] = $value1; |
639 | 639 | |
640 | - $conditionString .= $key . ' <= ? {op} '; |
|
640 | + $conditionString .= $key.' <= ? {op} '; |
|
641 | 641 | $conditionValues[] = $value2; |
642 | 642 | } |
643 | 643 | elseif (strtolower($operator) == 'in') |
644 | 644 | { |
645 | 645 | $conditionValues = array_merge($conditionValues, $value); |
646 | 646 | $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
647 | - $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
647 | + $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
648 | 648 | } |
649 | 649 | elseif (strtolower($operator) == 'null') |
650 | 650 | { |
651 | - $conditionString .= $key . ' is null {op} '; |
|
651 | + $conditionString .= $key.' is null {op} '; |
|
652 | 652 | } |
653 | 653 | elseif (strtolower($operator) == 'not null') |
654 | 654 | { |
655 | - $conditionString .= $key . ' is not null {op} '; |
|
655 | + $conditionString .= $key.' is not null {op} '; |
|
656 | 656 | } |
657 | 657 | elseif (strtolower($operator) == 'has') |
658 | 658 | { |
659 | 659 | $sql = $model->withTrashed()->has($key)->toSql(); |
660 | 660 | $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
661 | - $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
661 | + $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')').' and '.$conditions['conditionString'].') {op} '; |
|
662 | 662 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
663 | 663 | } |
664 | 664 | else |
665 | 665 | { |
666 | - $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
666 | + $conditionString .= $key.' '.$operator.' ? {op} '; |
|
667 | 667 | $conditionValues[] = $value; |
668 | 668 | } |
669 | 669 | } |
670 | 670 | } |
671 | - $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
671 | + $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
672 | 672 | return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
673 | 673 | } |
674 | 674 | |
@@ -684,11 +684,11 @@ discard block |
||
684 | 684 | $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
685 | 685 | $path = explode('->', $value); |
686 | 686 | $field = array_shift($path); |
687 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
687 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function($part) { |
|
688 | 688 | return '"'.$part.'"'; |
689 | 689 | })->implode('.')); |
690 | 690 | |
691 | - return $removeLast === false ? $result : $result . ')'; |
|
691 | + return $removeLast === false ? $result : $result.')'; |
|
692 | 692 | } |
693 | 693 | |
694 | 694 | /** |
@@ -453,8 +453,7 @@ discard block |
||
453 | 453 | { |
454 | 454 | $model = $this->model->lockForUpdate()->find($value); |
455 | 455 | $model ? $model->update($data) : 0; |
456 | - } |
|
457 | - else |
|
456 | + } else |
|
458 | 457 | { |
459 | 458 | call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data){ |
460 | 459 | $model->update($data); |
@@ -483,8 +482,7 @@ discard block |
||
483 | 482 | |
484 | 483 | $model->delete(); |
485 | 484 | }); |
486 | - } |
|
487 | - else |
|
485 | + } else |
|
488 | 486 | { |
489 | 487 | \DB::transaction(function () use ($value, $attribute, &$result) { |
490 | 488 | call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
@@ -605,14 +603,12 @@ discard block |
||
605 | 603 | $conditions = $this->constructConditions($value, $model); |
606 | 604 | $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
607 | 605 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
608 | - } |
|
609 | - else if ($key == 'or') |
|
606 | + } else if ($key == 'or') |
|
610 | 607 | { |
611 | 608 | $conditions = $this->constructConditions($value, $model); |
612 | 609 | $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
613 | 610 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
614 | - } |
|
615 | - else |
|
611 | + } else |
|
616 | 612 | { |
617 | 613 | if (is_array($value)) |
618 | 614 | { |
@@ -621,13 +617,11 @@ discard block |
||
621 | 617 | { |
622 | 618 | $value1 = $value['val1']; |
623 | 619 | $value2 = $value['val2']; |
624 | - } |
|
625 | - else |
|
620 | + } else |
|
626 | 621 | { |
627 | 622 | $value = array_key_exists('val', $value) ? $value['val'] : ''; |
628 | 623 | } |
629 | - } |
|
630 | - else |
|
624 | + } else |
|
631 | 625 | { |
632 | 626 | $operator = '='; |
633 | 627 | } |
@@ -639,29 +633,24 @@ discard block |
||
639 | 633 | |
640 | 634 | $conditionString .= $key . ' <= ? {op} '; |
641 | 635 | $conditionValues[] = $value2; |
642 | - } |
|
643 | - elseif (strtolower($operator) == 'in') |
|
636 | + } elseif (strtolower($operator) == 'in') |
|
644 | 637 | { |
645 | 638 | $conditionValues = array_merge($conditionValues, $value); |
646 | 639 | $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
647 | 640 | $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
648 | - } |
|
649 | - elseif (strtolower($operator) == 'null') |
|
641 | + } elseif (strtolower($operator) == 'null') |
|
650 | 642 | { |
651 | 643 | $conditionString .= $key . ' is null {op} '; |
652 | - } |
|
653 | - elseif (strtolower($operator) == 'not null') |
|
644 | + } elseif (strtolower($operator) == 'not null') |
|
654 | 645 | { |
655 | 646 | $conditionString .= $key . ' is not null {op} '; |
656 | - } |
|
657 | - elseif (strtolower($operator) == 'has') |
|
647 | + } elseif (strtolower($operator) == 'has') |
|
658 | 648 | { |
659 | 649 | $sql = $model->withTrashed()->has($key)->toSql(); |
660 | 650 | $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
661 | 651 | $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
662 | 652 | $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
663 | - } |
|
664 | - else |
|
653 | + } else |
|
665 | 654 | { |
666 | 655 | $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
667 | 656 | $conditionValues[] = $value; |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | * |
33 | 33 | * @param array $relations |
34 | 34 | * @param string $sortBy |
35 | - * @param boolean $desc |
|
35 | + * @param integer $desc |
|
36 | 36 | * @param array $columns |
37 | 37 | * @return collection |
38 | 38 | */ |
@@ -50,7 +50,7 @@ discard block |
||
50 | 50 | * @param integer $perPage |
51 | 51 | * @param array $relations |
52 | 52 | * @param string $sortBy |
53 | - * @param boolean $desc |
|
53 | + * @param integer $desc |
|
54 | 54 | * @param array $columns |
55 | 55 | * @return collection |
56 | 56 | */ |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | * @param integer $perPage |
165 | 165 | * @param array $relations |
166 | 166 | * @param string $sortBy |
167 | - * @param boolean $desc |
|
167 | + * @param integer $desc |
|
168 | 168 | * @param array $columns |
169 | 169 | * @return collection |
170 | 170 | */ |
@@ -182,7 +182,7 @@ discard block |
||
182 | 182 | * @param integer $perPage |
183 | 183 | * @param array $relations |
184 | 184 | * @param string $sortBy |
185 | - * @param boolean $desc |
|
185 | + * @param integer $desc |
|
186 | 186 | * @param array $columns |
187 | 187 | * @return collection |
188 | 188 | */ |
@@ -198,7 +198,7 @@ discard block |
||
198 | 198 | * Save the given model to the storage. |
199 | 199 | * |
200 | 200 | * @param array $data |
201 | - * @return object |
|
201 | + * @return boolean |
|
202 | 202 | */ |
203 | 203 | public function save(array $data) |
204 | 204 | { |
@@ -516,7 +516,7 @@ discard block |
||
516 | 516 | * id. |
517 | 517 | * |
518 | 518 | * @param integer $id |
519 | - * @param array $relations |
|
519 | + * @param string[] $relations |
|
520 | 520 | * @param array $columns |
521 | 521 | * @return object |
522 | 522 | */ |
@@ -532,7 +532,7 @@ discard block |
||
532 | 532 | * @param array $conditions array of conditions |
533 | 533 | * @param array $relations |
534 | 534 | * @param string $sortBy |
535 | - * @param boolean $desc |
|
535 | + * @param integer $desc |
|
536 | 536 | * @param array $columns |
537 | 537 | * @return collection |
538 | 538 | */ |
@@ -564,7 +564,7 @@ discard block |
||
564 | 564 | * @param array $conditions array of conditions |
565 | 565 | * @param integer $perPage |
566 | 566 | * @param string $sortBy |
567 | - * @param boolean $desc |
|
567 | + * @param integer $desc |
|
568 | 568 | * @param array $columns |
569 | 569 | * @return collection |
570 | 570 | */ |
@@ -4,716 +4,716 @@ |
||
4 | 4 | |
5 | 5 | abstract class AbstractRepository implements RepositoryInterface |
6 | 6 | { |
7 | - /** |
|
8 | - * The model implementation. |
|
9 | - * |
|
10 | - * @var model |
|
11 | - */ |
|
12 | - public $model; |
|
7 | + /** |
|
8 | + * The model implementation. |
|
9 | + * |
|
10 | + * @var model |
|
11 | + */ |
|
12 | + public $model; |
|
13 | 13 | |
14 | - /** |
|
15 | - * The config implementation. |
|
16 | - * |
|
17 | - * @var config |
|
18 | - */ |
|
19 | - protected $config; |
|
14 | + /** |
|
15 | + * The config implementation. |
|
16 | + * |
|
17 | + * @var config |
|
18 | + */ |
|
19 | + protected $config; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Create new AbstractRepository instance. |
|
23 | - */ |
|
24 | - public function __construct() |
|
25 | - { |
|
26 | - $this->config = \CoreConfig::getConfig(); |
|
27 | - $this->model = \App::make($this->getModel()); |
|
28 | - } |
|
29 | - |
|
30 | - /** |
|
31 | - * Fetch all records with relations from the storage. |
|
32 | - * |
|
33 | - * @param array $relations |
|
34 | - * @param string $sortBy |
|
35 | - * @param boolean $desc |
|
36 | - * @param array $columns |
|
37 | - * @return collection |
|
38 | - */ |
|
39 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
40 | - { |
|
41 | - $sort = $desc ? 'desc' : 'asc'; |
|
42 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * Fetch all records with relations from storage in pages |
|
47 | - * that matche the given query. |
|
48 | - * |
|
49 | - * @param string $query |
|
50 | - * @param integer $perPage |
|
51 | - * @param array $relations |
|
52 | - * @param string $sortBy |
|
53 | - * @param boolean $desc |
|
54 | - * @param array $columns |
|
55 | - * @return collection |
|
56 | - */ |
|
57 | - public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
58 | - { |
|
59 | - $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
60 | - $conditionColumns = $this->model->searchable; |
|
61 | - $sort = $desc ? 'desc' : 'asc'; |
|
62 | - |
|
63 | - /** |
|
64 | - * Construct the select conditions for the model. |
|
65 | - */ |
|
66 | - $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
67 | - |
|
68 | - if (count($conditionColumns)) |
|
69 | - { |
|
70 | - $column = 'LOWER(' . array_shift($conditionColumns) . ')'; |
|
71 | - if (str_contains($column, '->')) |
|
72 | - { |
|
73 | - $column = $this->wrapJsonSelector($column); |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Use the first element in the model columns to construct the first condition. |
|
78 | - */ |
|
79 | - $q->where(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Loop through the rest of the columns to construct or where conditions. |
|
84 | - */ |
|
85 | - foreach ($conditionColumns as $column) |
|
86 | - { |
|
87 | - $column = 'LOWER(' . $column . ')'; |
|
88 | - if (str_contains($column, '->')) |
|
89 | - { |
|
90 | - $column = $this->wrapJsonSelector($column); |
|
91 | - } |
|
92 | - |
|
93 | - $q->orWhere(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Loop through the model relations. |
|
98 | - */ |
|
99 | - foreach ($relations as $relation) |
|
100 | - { |
|
101 | - /** |
|
102 | - * Remove the sub relation if exists. |
|
103 | - */ |
|
104 | - $relation = explode('.', $relation)[0]; |
|
105 | - |
|
106 | - /** |
|
107 | - * Try to fetch the relation repository from the core. |
|
108 | - */ |
|
109 | - if (\Core::$relation()) |
|
110 | - { |
|
111 | - /** |
|
112 | - * Construct the relation condition. |
|
113 | - */ |
|
114 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
115 | - |
|
116 | - $subModel->where(function ($q) use ($query, $relation){ |
|
117 | - |
|
118 | - /** |
|
119 | - * Get columns of the relation. |
|
120 | - */ |
|
121 | - $subConditionColumns = \Core::$relation()->model->searchable; |
|
122 | - |
|
123 | - if (count($subConditionColumns)) |
|
124 | - { |
|
125 | - |
|
126 | - $column = 'LOWER(' . array_shift($subConditionColumns) . ')'; |
|
127 | - if (str_contains($column, '->')) |
|
128 | - { |
|
129 | - $column = $this->wrapJsonSelector($column); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Use the first element in the relation model columns to construct the first condition. |
|
134 | - */ |
|
135 | - $q->where(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
136 | - } |
|
137 | - |
|
138 | - /** |
|
139 | - * Loop through the rest of the columns to construct or where conditions. |
|
140 | - */ |
|
141 | - foreach ($subConditionColumns as $subConditionColumn) |
|
142 | - { |
|
143 | - $column = 'LOWER(' . $subConditionColumn . ')'; |
|
144 | - if (str_contains($column, '->')) |
|
145 | - { |
|
146 | - $column = $this->wrapJsonSelector($column); |
|
147 | - } |
|
21 | + /** |
|
22 | + * Create new AbstractRepository instance. |
|
23 | + */ |
|
24 | + public function __construct() |
|
25 | + { |
|
26 | + $this->config = \CoreConfig::getConfig(); |
|
27 | + $this->model = \App::make($this->getModel()); |
|
28 | + } |
|
29 | + |
|
30 | + /** |
|
31 | + * Fetch all records with relations from the storage. |
|
32 | + * |
|
33 | + * @param array $relations |
|
34 | + * @param string $sortBy |
|
35 | + * @param boolean $desc |
|
36 | + * @param array $columns |
|
37 | + * @return collection |
|
38 | + */ |
|
39 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
40 | + { |
|
41 | + $sort = $desc ? 'desc' : 'asc'; |
|
42 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->get($columns); |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * Fetch all records with relations from storage in pages |
|
47 | + * that matche the given query. |
|
48 | + * |
|
49 | + * @param string $query |
|
50 | + * @param integer $perPage |
|
51 | + * @param array $relations |
|
52 | + * @param string $sortBy |
|
53 | + * @param boolean $desc |
|
54 | + * @param array $columns |
|
55 | + * @return collection |
|
56 | + */ |
|
57 | + public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
58 | + { |
|
59 | + $model = call_user_func_array("{$this->getModel()}::with", array($relations)); |
|
60 | + $conditionColumns = $this->model->searchable; |
|
61 | + $sort = $desc ? 'desc' : 'asc'; |
|
62 | + |
|
63 | + /** |
|
64 | + * Construct the select conditions for the model. |
|
65 | + */ |
|
66 | + $model->where(function ($q) use ($query, $conditionColumns, $relations){ |
|
67 | + |
|
68 | + if (count($conditionColumns)) |
|
69 | + { |
|
70 | + $column = 'LOWER(' . array_shift($conditionColumns) . ')'; |
|
71 | + if (str_contains($column, '->')) |
|
72 | + { |
|
73 | + $column = $this->wrapJsonSelector($column); |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Use the first element in the model columns to construct the first condition. |
|
78 | + */ |
|
79 | + $q->where(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Loop through the rest of the columns to construct or where conditions. |
|
84 | + */ |
|
85 | + foreach ($conditionColumns as $column) |
|
86 | + { |
|
87 | + $column = 'LOWER(' . $column . ')'; |
|
88 | + if (str_contains($column, '->')) |
|
89 | + { |
|
90 | + $column = $this->wrapJsonSelector($column); |
|
91 | + } |
|
92 | + |
|
93 | + $q->orWhere(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Loop through the model relations. |
|
98 | + */ |
|
99 | + foreach ($relations as $relation) |
|
100 | + { |
|
101 | + /** |
|
102 | + * Remove the sub relation if exists. |
|
103 | + */ |
|
104 | + $relation = explode('.', $relation)[0]; |
|
105 | + |
|
106 | + /** |
|
107 | + * Try to fetch the relation repository from the core. |
|
108 | + */ |
|
109 | + if (\Core::$relation()) |
|
110 | + { |
|
111 | + /** |
|
112 | + * Construct the relation condition. |
|
113 | + */ |
|
114 | + $q->orWhereHas($relation, function ($subModel) use ($query, $relation){ |
|
115 | + |
|
116 | + $subModel->where(function ($q) use ($query, $relation){ |
|
117 | + |
|
118 | + /** |
|
119 | + * Get columns of the relation. |
|
120 | + */ |
|
121 | + $subConditionColumns = \Core::$relation()->model->searchable; |
|
122 | + |
|
123 | + if (count($subConditionColumns)) |
|
124 | + { |
|
125 | + |
|
126 | + $column = 'LOWER(' . array_shift($subConditionColumns) . ')'; |
|
127 | + if (str_contains($column, '->')) |
|
128 | + { |
|
129 | + $column = $this->wrapJsonSelector($column); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Use the first element in the relation model columns to construct the first condition. |
|
134 | + */ |
|
135 | + $q->where(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
136 | + } |
|
137 | + |
|
138 | + /** |
|
139 | + * Loop through the rest of the columns to construct or where conditions. |
|
140 | + */ |
|
141 | + foreach ($subConditionColumns as $subConditionColumn) |
|
142 | + { |
|
143 | + $column = 'LOWER(' . $subConditionColumn . ')'; |
|
144 | + if (str_contains($column, '->')) |
|
145 | + { |
|
146 | + $column = $this->wrapJsonSelector($column); |
|
147 | + } |
|
148 | 148 | |
149 | - $q->orWhere(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
150 | - } |
|
151 | - }); |
|
152 | - |
|
153 | - }); |
|
154 | - } |
|
155 | - } |
|
156 | - }); |
|
149 | + $q->orWhere(\DB::raw($column), 'LIKE', '%' . strtolower($query) . '%'); |
|
150 | + } |
|
151 | + }); |
|
152 | + |
|
153 | + }); |
|
154 | + } |
|
155 | + } |
|
156 | + }); |
|
157 | 157 | |
158 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
159 | - } |
|
158 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
159 | + } |
|
160 | 160 | |
161 | - /** |
|
162 | - * Fetch all records with relations from storage in pages. |
|
163 | - * |
|
164 | - * @param integer $perPage |
|
165 | - * @param array $relations |
|
166 | - * @param string $sortBy |
|
167 | - * @param boolean $desc |
|
168 | - * @param array $columns |
|
169 | - * @return collection |
|
170 | - */ |
|
171 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
172 | - { |
|
173 | - $sort = $desc ? 'desc' : 'asc'; |
|
174 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Fetch all records with relations based on |
|
179 | - * the given condition from storage in pages. |
|
180 | - * |
|
181 | - * @param array $conditions array of conditions |
|
182 | - * @param integer $perPage |
|
183 | - * @param array $relations |
|
184 | - * @param string $sortBy |
|
185 | - * @param boolean $desc |
|
186 | - * @param array $columns |
|
187 | - * @return collection |
|
188 | - */ |
|
189 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
190 | - { |
|
191 | - unset($conditions['page']); |
|
192 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
193 | - $sort = $desc ? 'desc' : 'asc'; |
|
194 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
195 | - } |
|
161 | + /** |
|
162 | + * Fetch all records with relations from storage in pages. |
|
163 | + * |
|
164 | + * @param integer $perPage |
|
165 | + * @param array $relations |
|
166 | + * @param string $sortBy |
|
167 | + * @param boolean $desc |
|
168 | + * @param array $columns |
|
169 | + * @return collection |
|
170 | + */ |
|
171 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
172 | + { |
|
173 | + $sort = $desc ? 'desc' : 'asc'; |
|
174 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Fetch all records with relations based on |
|
179 | + * the given condition from storage in pages. |
|
180 | + * |
|
181 | + * @param array $conditions array of conditions |
|
182 | + * @param integer $perPage |
|
183 | + * @param array $relations |
|
184 | + * @param string $sortBy |
|
185 | + * @param boolean $desc |
|
186 | + * @param array $columns |
|
187 | + * @return collection |
|
188 | + */ |
|
189 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
190 | + { |
|
191 | + unset($conditions['page']); |
|
192 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
193 | + $sort = $desc ? 'desc' : 'asc'; |
|
194 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
195 | + } |
|
196 | 196 | |
197 | - /** |
|
198 | - * Save the given model to the storage. |
|
199 | - * |
|
200 | - * @param array $data |
|
201 | - * @return object |
|
202 | - */ |
|
203 | - public function save(array $data) |
|
204 | - { |
|
205 | - $model = false; |
|
206 | - $modelClass = $this->model; |
|
207 | - $relations = []; |
|
208 | - |
|
209 | - \DB::transaction(function () use (&$model, &$relations, $data, $modelClass) { |
|
210 | - /** |
|
211 | - * If the id is present in the data then select the model for updating, |
|
212 | - * else create new model. |
|
213 | - * @var array |
|
214 | - */ |
|
215 | - $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
216 | - if ( ! $model) |
|
217 | - { |
|
218 | - \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
219 | - } |
|
220 | - |
|
221 | - /** |
|
222 | - * Construct the model object with the given data, |
|
223 | - * and if there is a relation add it to relations array, |
|
224 | - * then save the model. |
|
225 | - */ |
|
226 | - foreach ($data as $key => $value) |
|
227 | - { |
|
228 | - /** |
|
229 | - * If the attribute is a relation. |
|
230 | - */ |
|
231 | - $relation = camel_case($key); |
|
232 | - if (method_exists($model, $relation) && \Core::$relation()) |
|
233 | - { |
|
234 | - /** |
|
235 | - * Check if the relation is a collection. |
|
236 | - */ |
|
237 | - if (class_basename($model->$relation) == 'Collection') |
|
238 | - { |
|
239 | - /** |
|
240 | - * If the relation has no value then marke the relation data |
|
241 | - * related to the model to be deleted. |
|
242 | - */ |
|
243 | - if ( ! $value || ! count($value)) |
|
244 | - { |
|
245 | - $relations[$relation] = 'delete'; |
|
246 | - } |
|
247 | - } |
|
248 | - if (is_array($value)) |
|
249 | - { |
|
250 | - /** |
|
251 | - * Loop through the relation data. |
|
252 | - */ |
|
253 | - foreach ($value as $attr => $val) |
|
254 | - { |
|
255 | - /** |
|
256 | - * Get the relation model. |
|
257 | - */ |
|
258 | - $relationBaseModel = \Core::$relation()->model; |
|
259 | - |
|
260 | - /** |
|
261 | - * Check if the relation is a collection. |
|
262 | - */ |
|
263 | - if (class_basename($model->$relation) == 'Collection') |
|
264 | - { |
|
265 | - /** |
|
266 | - * If the id is present in the data then select the relation model for updating, |
|
267 | - * else create new model. |
|
268 | - */ |
|
269 | - $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
270 | - |
|
271 | - /** |
|
272 | - * If model doesn't exists. |
|
273 | - */ |
|
274 | - if ( ! $relationModel) |
|
275 | - { |
|
276 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Loop through the relation attributes. |
|
281 | - */ |
|
282 | - foreach ($val as $attr => $val) |
|
283 | - { |
|
284 | - /** |
|
285 | - * Prevent the sub relations or attributes not in the fillable. |
|
286 | - */ |
|
287 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
288 | - { |
|
289 | - $relationModel->$attr = $val; |
|
290 | - } |
|
291 | - } |
|
292 | - |
|
293 | - $relations[$relation][] = $relationModel; |
|
294 | - } |
|
295 | - /** |
|
296 | - * If not collection. |
|
297 | - */ |
|
298 | - else |
|
299 | - { |
|
300 | - /** |
|
301 | - * Prevent the sub relations. |
|
302 | - */ |
|
303 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
304 | - { |
|
305 | - |
|
306 | - /** |
|
307 | - * If the id is present in the data then select the relation model for updating, |
|
308 | - * else create new model. |
|
309 | - */ |
|
310 | - $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
311 | - |
|
312 | - /** |
|
313 | - * If model doesn't exists. |
|
314 | - */ |
|
315 | - if ( ! $relationModel) |
|
316 | - { |
|
317 | - \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
318 | - } |
|
319 | - |
|
320 | - foreach ($value as $relationAttribute => $relationValue) |
|
321 | - { |
|
322 | - /** |
|
323 | - * Prevent attributes not in the fillable. |
|
324 | - */ |
|
325 | - if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) |
|
326 | - { |
|
327 | - $relationModel->$relationAttribute = $relationValue; |
|
328 | - } |
|
329 | - } |
|
330 | - |
|
331 | - $relations[$relation] = $relationModel; |
|
332 | - } |
|
333 | - } |
|
334 | - } |
|
335 | - } |
|
336 | - } |
|
337 | - /** |
|
338 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
339 | - */ |
|
340 | - else if (array_search($key, $model->getFillable(), true) !== false) |
|
341 | - { |
|
342 | - $model->$key = $value; |
|
343 | - } |
|
344 | - } |
|
345 | - |
|
346 | - /** |
|
347 | - * Loop through the relations array. |
|
348 | - */ |
|
349 | - foreach ($relations as $key => $value) |
|
350 | - { |
|
351 | - /** |
|
352 | - * If the relation is marked for delete then delete it. |
|
353 | - */ |
|
354 | - if ($value == 'delete' && $model->$key()->count()) |
|
355 | - { |
|
356 | - $model->$key()->delete(); |
|
357 | - } |
|
358 | - /** |
|
359 | - * If the relation is an array. |
|
360 | - */ |
|
361 | - else if (gettype($value) == 'array') |
|
362 | - { |
|
363 | - /** |
|
364 | - * Save the model. |
|
365 | - */ |
|
366 | - $model->save(); |
|
367 | - $ids = []; |
|
368 | - |
|
369 | - /** |
|
370 | - * Loop through the relations. |
|
371 | - */ |
|
372 | - foreach ($value as $val) |
|
373 | - { |
|
374 | - switch (class_basename($model->$key())) |
|
375 | - { |
|
376 | - /** |
|
377 | - * If the relation is one to many then update it's foreign key with |
|
378 | - * the model id and save it then add its id to ids array to delete all |
|
379 | - * relations who's id isn't in the ids array. |
|
380 | - */ |
|
381 | - case 'HasMany': |
|
382 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
383 | - $val->$foreignKeyName = $model->id; |
|
384 | - $val->save(); |
|
385 | - $ids[] = $val->id; |
|
386 | - break; |
|
387 | - |
|
388 | - /** |
|
389 | - * If the relation is many to many then add it's id to the ids array to |
|
390 | - * attache these ids to the model. |
|
391 | - */ |
|
392 | - case 'BelongsToMany': |
|
393 | - $val->save(); |
|
394 | - $ids[] = $val->id; |
|
395 | - break; |
|
396 | - } |
|
397 | - } |
|
398 | - switch (class_basename($model->$key())) |
|
399 | - { |
|
400 | - /** |
|
401 | - * If the relation is one to many then delete all |
|
402 | - * relations who's id isn't in the ids array. |
|
403 | - */ |
|
404 | - case 'HasMany': |
|
405 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
406 | - break; |
|
407 | - |
|
408 | - /** |
|
409 | - * If the relation is many to many then |
|
410 | - * detach the previous data and attach |
|
411 | - * the ids array to the model. |
|
412 | - */ |
|
413 | - case 'BelongsToMany': |
|
414 | - $model->$key()->detach(); |
|
415 | - $model->$key()->attach($ids); |
|
416 | - break; |
|
417 | - } |
|
418 | - } |
|
419 | - /** |
|
420 | - * If the relation isn't array. |
|
421 | - */ |
|
422 | - else |
|
423 | - { |
|
424 | - switch (class_basename($model->$key())) |
|
425 | - { |
|
426 | - /** |
|
427 | - * If the relation is one to one. |
|
428 | - */ |
|
429 | - case 'HasOne': |
|
430 | - /** |
|
431 | - * Save the model. |
|
432 | - */ |
|
433 | - $model->save(); |
|
434 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
435 | - $value->$foreignKeyName = $model->id; |
|
436 | - $value->save(); |
|
437 | - break; |
|
438 | - case 'BelongsTo': |
|
439 | - /** |
|
440 | - * Save the model. |
|
441 | - */ |
|
442 | - $value->save(); |
|
443 | - $model->$key()->associate($value); |
|
444 | - break; |
|
445 | - } |
|
446 | - } |
|
447 | - } |
|
448 | - |
|
449 | - /** |
|
450 | - * Save the model. |
|
451 | - */ |
|
452 | - $model->save(); |
|
453 | - }); |
|
197 | + /** |
|
198 | + * Save the given model to the storage. |
|
199 | + * |
|
200 | + * @param array $data |
|
201 | + * @return object |
|
202 | + */ |
|
203 | + public function save(array $data) |
|
204 | + { |
|
205 | + $model = false; |
|
206 | + $modelClass = $this->model; |
|
207 | + $relations = []; |
|
208 | + |
|
209 | + \DB::transaction(function () use (&$model, &$relations, $data, $modelClass) { |
|
210 | + /** |
|
211 | + * If the id is present in the data then select the model for updating, |
|
212 | + * else create new model. |
|
213 | + * @var array |
|
214 | + */ |
|
215 | + $model = array_key_exists('id', $data) ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
216 | + if ( ! $model) |
|
217 | + { |
|
218 | + \ErrorHandler::notFound(class_basename($modelClass) . ' with id : ' . $data['id']); |
|
219 | + } |
|
220 | + |
|
221 | + /** |
|
222 | + * Construct the model object with the given data, |
|
223 | + * and if there is a relation add it to relations array, |
|
224 | + * then save the model. |
|
225 | + */ |
|
226 | + foreach ($data as $key => $value) |
|
227 | + { |
|
228 | + /** |
|
229 | + * If the attribute is a relation. |
|
230 | + */ |
|
231 | + $relation = camel_case($key); |
|
232 | + if (method_exists($model, $relation) && \Core::$relation()) |
|
233 | + { |
|
234 | + /** |
|
235 | + * Check if the relation is a collection. |
|
236 | + */ |
|
237 | + if (class_basename($model->$relation) == 'Collection') |
|
238 | + { |
|
239 | + /** |
|
240 | + * If the relation has no value then marke the relation data |
|
241 | + * related to the model to be deleted. |
|
242 | + */ |
|
243 | + if ( ! $value || ! count($value)) |
|
244 | + { |
|
245 | + $relations[$relation] = 'delete'; |
|
246 | + } |
|
247 | + } |
|
248 | + if (is_array($value)) |
|
249 | + { |
|
250 | + /** |
|
251 | + * Loop through the relation data. |
|
252 | + */ |
|
253 | + foreach ($value as $attr => $val) |
|
254 | + { |
|
255 | + /** |
|
256 | + * Get the relation model. |
|
257 | + */ |
|
258 | + $relationBaseModel = \Core::$relation()->model; |
|
259 | + |
|
260 | + /** |
|
261 | + * Check if the relation is a collection. |
|
262 | + */ |
|
263 | + if (class_basename($model->$relation) == 'Collection') |
|
264 | + { |
|
265 | + /** |
|
266 | + * If the id is present in the data then select the relation model for updating, |
|
267 | + * else create new model. |
|
268 | + */ |
|
269 | + $relationModel = array_key_exists('id', $val) ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
270 | + |
|
271 | + /** |
|
272 | + * If model doesn't exists. |
|
273 | + */ |
|
274 | + if ( ! $relationModel) |
|
275 | + { |
|
276 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $val['id']); |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Loop through the relation attributes. |
|
281 | + */ |
|
282 | + foreach ($val as $attr => $val) |
|
283 | + { |
|
284 | + /** |
|
285 | + * Prevent the sub relations or attributes not in the fillable. |
|
286 | + */ |
|
287 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) |
|
288 | + { |
|
289 | + $relationModel->$attr = $val; |
|
290 | + } |
|
291 | + } |
|
292 | + |
|
293 | + $relations[$relation][] = $relationModel; |
|
294 | + } |
|
295 | + /** |
|
296 | + * If not collection. |
|
297 | + */ |
|
298 | + else |
|
299 | + { |
|
300 | + /** |
|
301 | + * Prevent the sub relations. |
|
302 | + */ |
|
303 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') |
|
304 | + { |
|
305 | + |
|
306 | + /** |
|
307 | + * If the id is present in the data then select the relation model for updating, |
|
308 | + * else create new model. |
|
309 | + */ |
|
310 | + $relationModel = array_key_exists('id', $value) ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
311 | + |
|
312 | + /** |
|
313 | + * If model doesn't exists. |
|
314 | + */ |
|
315 | + if ( ! $relationModel) |
|
316 | + { |
|
317 | + \ErrorHandler::notFound(class_basename($relationBaseModel) . ' with id : ' . $value['id']); |
|
318 | + } |
|
319 | + |
|
320 | + foreach ($value as $relationAttribute => $relationValue) |
|
321 | + { |
|
322 | + /** |
|
323 | + * Prevent attributes not in the fillable. |
|
324 | + */ |
|
325 | + if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) |
|
326 | + { |
|
327 | + $relationModel->$relationAttribute = $relationValue; |
|
328 | + } |
|
329 | + } |
|
330 | + |
|
331 | + $relations[$relation] = $relationModel; |
|
332 | + } |
|
333 | + } |
|
334 | + } |
|
335 | + } |
|
336 | + } |
|
337 | + /** |
|
338 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
339 | + */ |
|
340 | + else if (array_search($key, $model->getFillable(), true) !== false) |
|
341 | + { |
|
342 | + $model->$key = $value; |
|
343 | + } |
|
344 | + } |
|
345 | + |
|
346 | + /** |
|
347 | + * Loop through the relations array. |
|
348 | + */ |
|
349 | + foreach ($relations as $key => $value) |
|
350 | + { |
|
351 | + /** |
|
352 | + * If the relation is marked for delete then delete it. |
|
353 | + */ |
|
354 | + if ($value == 'delete' && $model->$key()->count()) |
|
355 | + { |
|
356 | + $model->$key()->delete(); |
|
357 | + } |
|
358 | + /** |
|
359 | + * If the relation is an array. |
|
360 | + */ |
|
361 | + else if (gettype($value) == 'array') |
|
362 | + { |
|
363 | + /** |
|
364 | + * Save the model. |
|
365 | + */ |
|
366 | + $model->save(); |
|
367 | + $ids = []; |
|
368 | + |
|
369 | + /** |
|
370 | + * Loop through the relations. |
|
371 | + */ |
|
372 | + foreach ($value as $val) |
|
373 | + { |
|
374 | + switch (class_basename($model->$key())) |
|
375 | + { |
|
376 | + /** |
|
377 | + * If the relation is one to many then update it's foreign key with |
|
378 | + * the model id and save it then add its id to ids array to delete all |
|
379 | + * relations who's id isn't in the ids array. |
|
380 | + */ |
|
381 | + case 'HasMany': |
|
382 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
383 | + $val->$foreignKeyName = $model->id; |
|
384 | + $val->save(); |
|
385 | + $ids[] = $val->id; |
|
386 | + break; |
|
387 | + |
|
388 | + /** |
|
389 | + * If the relation is many to many then add it's id to the ids array to |
|
390 | + * attache these ids to the model. |
|
391 | + */ |
|
392 | + case 'BelongsToMany': |
|
393 | + $val->save(); |
|
394 | + $ids[] = $val->id; |
|
395 | + break; |
|
396 | + } |
|
397 | + } |
|
398 | + switch (class_basename($model->$key())) |
|
399 | + { |
|
400 | + /** |
|
401 | + * If the relation is one to many then delete all |
|
402 | + * relations who's id isn't in the ids array. |
|
403 | + */ |
|
404 | + case 'HasMany': |
|
405 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
406 | + break; |
|
407 | + |
|
408 | + /** |
|
409 | + * If the relation is many to many then |
|
410 | + * detach the previous data and attach |
|
411 | + * the ids array to the model. |
|
412 | + */ |
|
413 | + case 'BelongsToMany': |
|
414 | + $model->$key()->detach(); |
|
415 | + $model->$key()->attach($ids); |
|
416 | + break; |
|
417 | + } |
|
418 | + } |
|
419 | + /** |
|
420 | + * If the relation isn't array. |
|
421 | + */ |
|
422 | + else |
|
423 | + { |
|
424 | + switch (class_basename($model->$key())) |
|
425 | + { |
|
426 | + /** |
|
427 | + * If the relation is one to one. |
|
428 | + */ |
|
429 | + case 'HasOne': |
|
430 | + /** |
|
431 | + * Save the model. |
|
432 | + */ |
|
433 | + $model->save(); |
|
434 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
435 | + $value->$foreignKeyName = $model->id; |
|
436 | + $value->save(); |
|
437 | + break; |
|
438 | + case 'BelongsTo': |
|
439 | + /** |
|
440 | + * Save the model. |
|
441 | + */ |
|
442 | + $value->save(); |
|
443 | + $model->$key()->associate($value); |
|
444 | + break; |
|
445 | + } |
|
446 | + } |
|
447 | + } |
|
448 | + |
|
449 | + /** |
|
450 | + * Save the model. |
|
451 | + */ |
|
452 | + $model->save(); |
|
453 | + }); |
|
454 | 454 | |
455 | - return $model; |
|
456 | - } |
|
455 | + return $model; |
|
456 | + } |
|
457 | 457 | |
458 | - /** |
|
459 | - * Update record in the storage based on the given |
|
460 | - * condition. |
|
461 | - * |
|
462 | - * @param var $value condition value |
|
463 | - * @param array $data |
|
464 | - * @param string $attribute condition column name |
|
465 | - * @return void |
|
466 | - */ |
|
467 | - public function update($value, array $data, $attribute = 'id') |
|
468 | - { |
|
469 | - if ($attribute == 'id') |
|
470 | - { |
|
471 | - $model = $this->model->lockForUpdate()->find($value); |
|
472 | - $model ? $model->update($data) : 0; |
|
473 | - } |
|
474 | - else |
|
475 | - { |
|
476 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data){ |
|
477 | - $model->update($data); |
|
478 | - }); |
|
479 | - } |
|
480 | - } |
|
481 | - |
|
482 | - /** |
|
483 | - * Delete record from the storage based on the given |
|
484 | - * condition. |
|
485 | - * |
|
486 | - * @param var $value condition value |
|
487 | - * @param string $attribute condition column name |
|
488 | - * @return void |
|
489 | - */ |
|
490 | - public function delete($value, $attribute = 'id') |
|
491 | - { |
|
492 | - if ($attribute == 'id') |
|
493 | - { |
|
494 | - \DB::transaction(function () use ($value, $attribute, &$result) { |
|
495 | - $model = $this->model->lockForUpdate()->find($value); |
|
496 | - if ( ! $model) |
|
497 | - { |
|
498 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
499 | - } |
|
458 | + /** |
|
459 | + * Update record in the storage based on the given |
|
460 | + * condition. |
|
461 | + * |
|
462 | + * @param var $value condition value |
|
463 | + * @param array $data |
|
464 | + * @param string $attribute condition column name |
|
465 | + * @return void |
|
466 | + */ |
|
467 | + public function update($value, array $data, $attribute = 'id') |
|
468 | + { |
|
469 | + if ($attribute == 'id') |
|
470 | + { |
|
471 | + $model = $this->model->lockForUpdate()->find($value); |
|
472 | + $model ? $model->update($data) : 0; |
|
473 | + } |
|
474 | + else |
|
475 | + { |
|
476 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model) use ($data){ |
|
477 | + $model->update($data); |
|
478 | + }); |
|
479 | + } |
|
480 | + } |
|
481 | + |
|
482 | + /** |
|
483 | + * Delete record from the storage based on the given |
|
484 | + * condition. |
|
485 | + * |
|
486 | + * @param var $value condition value |
|
487 | + * @param string $attribute condition column name |
|
488 | + * @return void |
|
489 | + */ |
|
490 | + public function delete($value, $attribute = 'id') |
|
491 | + { |
|
492 | + if ($attribute == 'id') |
|
493 | + { |
|
494 | + \DB::transaction(function () use ($value, $attribute, &$result) { |
|
495 | + $model = $this->model->lockForUpdate()->find($value); |
|
496 | + if ( ! $model) |
|
497 | + { |
|
498 | + \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $value); |
|
499 | + } |
|
500 | 500 | |
501 | - $model->delete(); |
|
502 | - }); |
|
503 | - } |
|
504 | - else |
|
505 | - { |
|
506 | - \DB::transaction(function () use ($value, $attribute, &$result) { |
|
507 | - call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
508 | - $model->delete(); |
|
509 | - }); |
|
510 | - }); |
|
511 | - } |
|
512 | - } |
|
501 | + $model->delete(); |
|
502 | + }); |
|
503 | + } |
|
504 | + else |
|
505 | + { |
|
506 | + \DB::transaction(function () use ($value, $attribute, &$result) { |
|
507 | + call_user_func_array("{$this->getModel()}::where", array($attribute, '=', $value))->lockForUpdate()->get()->each(function ($model){ |
|
508 | + $model->delete(); |
|
509 | + }); |
|
510 | + }); |
|
511 | + } |
|
512 | + } |
|
513 | 513 | |
514 | - /** |
|
515 | - * Fetch records from the storage based on the given |
|
516 | - * id. |
|
517 | - * |
|
518 | - * @param integer $id |
|
519 | - * @param array $relations |
|
520 | - * @param array $columns |
|
521 | - * @return object |
|
522 | - */ |
|
523 | - public function find($id, $relations = [], $columns = array('*')) |
|
524 | - { |
|
525 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
526 | - } |
|
514 | + /** |
|
515 | + * Fetch records from the storage based on the given |
|
516 | + * id. |
|
517 | + * |
|
518 | + * @param integer $id |
|
519 | + * @param array $relations |
|
520 | + * @param array $columns |
|
521 | + * @return object |
|
522 | + */ |
|
523 | + public function find($id, $relations = [], $columns = array('*')) |
|
524 | + { |
|
525 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->find($id, $columns); |
|
526 | + } |
|
527 | 527 | |
528 | - /** |
|
529 | - * Fetch records from the storage based on the given |
|
530 | - * condition. |
|
531 | - * |
|
532 | - * @param array $conditions array of conditions |
|
533 | - * @param array $relations |
|
534 | - * @param string $sortBy |
|
535 | - * @param boolean $desc |
|
536 | - * @param array $columns |
|
537 | - * @return collection |
|
538 | - */ |
|
539 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
540 | - { |
|
541 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
542 | - $sort = $desc ? 'desc' : 'asc'; |
|
543 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
544 | - } |
|
545 | - |
|
546 | - /** |
|
547 | - * Fetch the first record from the storage based on the given |
|
548 | - * condition. |
|
549 | - * |
|
550 | - * @param array $conditions array of conditions |
|
551 | - * @param array $relations |
|
552 | - * @param array $columns |
|
553 | - * @return object |
|
554 | - */ |
|
555 | - public function first($conditions, $relations = [], $columns = array('*')) |
|
556 | - { |
|
557 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
558 | - return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
559 | - } |
|
560 | - |
|
561 | - /** |
|
562 | - * Return the deleted models in pages based on the given conditions. |
|
563 | - * |
|
564 | - * @param array $conditions array of conditions |
|
565 | - * @param integer $perPage |
|
566 | - * @param string $sortBy |
|
567 | - * @param boolean $desc |
|
568 | - * @param array $columns |
|
569 | - * @return collection |
|
570 | - */ |
|
571 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
572 | - { |
|
573 | - unset($conditions['page']); |
|
574 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
575 | - $sort = $desc ? 'desc' : 'asc'; |
|
576 | - $model = $this->model->onlyTrashed(); |
|
577 | - |
|
578 | - if (count($conditions['conditionValues'])) |
|
579 | - { |
|
580 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
581 | - } |
|
582 | - |
|
583 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
584 | - } |
|
585 | - |
|
586 | - /** |
|
587 | - * Restore the deleted model. |
|
588 | - * |
|
589 | - * @param integer $id |
|
590 | - * @return void |
|
591 | - */ |
|
592 | - public function restore($id) |
|
593 | - { |
|
594 | - $model = $this->model->onlyTrashed()->find($id); |
|
595 | - |
|
596 | - if ( ! $model) |
|
597 | - { |
|
598 | - \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
599 | - } |
|
600 | - |
|
601 | - $model->restore(); |
|
602 | - } |
|
603 | - |
|
604 | - /** |
|
605 | - * Build the conditions recursively for the retrieving methods. |
|
606 | - * @param array $conditions |
|
607 | - * @return array |
|
608 | - */ |
|
609 | - protected function constructConditions($conditions, $model) |
|
610 | - { |
|
611 | - $conditionString = ''; |
|
612 | - $conditionValues = []; |
|
613 | - foreach ($conditions as $key => $value) |
|
614 | - { |
|
615 | - if (str_contains($key, '->')) |
|
616 | - { |
|
617 | - $key = $this->wrapJsonSelector($key); |
|
618 | - } |
|
619 | - |
|
620 | - if ($key == 'and') |
|
621 | - { |
|
622 | - $conditions = $this->constructConditions($value, $model); |
|
623 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
624 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
625 | - } |
|
626 | - else if ($key == 'or') |
|
627 | - { |
|
628 | - $conditions = $this->constructConditions($value, $model); |
|
629 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
630 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
631 | - } |
|
632 | - else |
|
633 | - { |
|
634 | - if (is_array($value)) |
|
635 | - { |
|
636 | - $operator = $value['op']; |
|
637 | - if (strtolower($operator) == 'between') |
|
638 | - { |
|
639 | - $value1 = $value['val1']; |
|
640 | - $value2 = $value['val2']; |
|
641 | - } |
|
642 | - else |
|
643 | - { |
|
644 | - $value = array_key_exists('val', $value) ? $value['val'] : ''; |
|
645 | - } |
|
646 | - } |
|
647 | - else |
|
648 | - { |
|
649 | - $operator = '='; |
|
650 | - } |
|
528 | + /** |
|
529 | + * Fetch records from the storage based on the given |
|
530 | + * condition. |
|
531 | + * |
|
532 | + * @param array $conditions array of conditions |
|
533 | + * @param array $relations |
|
534 | + * @param string $sortBy |
|
535 | + * @param boolean $desc |
|
536 | + * @param array $columns |
|
537 | + * @return collection |
|
538 | + */ |
|
539 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
540 | + { |
|
541 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
542 | + $sort = $desc ? 'desc' : 'asc'; |
|
543 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
544 | + } |
|
545 | + |
|
546 | + /** |
|
547 | + * Fetch the first record from the storage based on the given |
|
548 | + * condition. |
|
549 | + * |
|
550 | + * @param array $conditions array of conditions |
|
551 | + * @param array $relations |
|
552 | + * @param array $columns |
|
553 | + * @return object |
|
554 | + */ |
|
555 | + public function first($conditions, $relations = [], $columns = array('*')) |
|
556 | + { |
|
557 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
558 | + return call_user_func_array("{$this->getModel()}::with", array($relations))->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
559 | + } |
|
560 | + |
|
561 | + /** |
|
562 | + * Return the deleted models in pages based on the given conditions. |
|
563 | + * |
|
564 | + * @param array $conditions array of conditions |
|
565 | + * @param integer $perPage |
|
566 | + * @param string $sortBy |
|
567 | + * @param boolean $desc |
|
568 | + * @param array $columns |
|
569 | + * @return collection |
|
570 | + */ |
|
571 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = array('*')) |
|
572 | + { |
|
573 | + unset($conditions['page']); |
|
574 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
575 | + $sort = $desc ? 'desc' : 'asc'; |
|
576 | + $model = $this->model->onlyTrashed(); |
|
577 | + |
|
578 | + if (count($conditions['conditionValues'])) |
|
579 | + { |
|
580 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
581 | + } |
|
582 | + |
|
583 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns);; |
|
584 | + } |
|
585 | + |
|
586 | + /** |
|
587 | + * Restore the deleted model. |
|
588 | + * |
|
589 | + * @param integer $id |
|
590 | + * @return void |
|
591 | + */ |
|
592 | + public function restore($id) |
|
593 | + { |
|
594 | + $model = $this->model->onlyTrashed()->find($id); |
|
595 | + |
|
596 | + if ( ! $model) |
|
597 | + { |
|
598 | + \ErrorHandler::notFound(class_basename($this->model) . ' with id : ' . $id); |
|
599 | + } |
|
600 | + |
|
601 | + $model->restore(); |
|
602 | + } |
|
603 | + |
|
604 | + /** |
|
605 | + * Build the conditions recursively for the retrieving methods. |
|
606 | + * @param array $conditions |
|
607 | + * @return array |
|
608 | + */ |
|
609 | + protected function constructConditions($conditions, $model) |
|
610 | + { |
|
611 | + $conditionString = ''; |
|
612 | + $conditionValues = []; |
|
613 | + foreach ($conditions as $key => $value) |
|
614 | + { |
|
615 | + if (str_contains($key, '->')) |
|
616 | + { |
|
617 | + $key = $this->wrapJsonSelector($key); |
|
618 | + } |
|
619 | + |
|
620 | + if ($key == 'and') |
|
621 | + { |
|
622 | + $conditions = $this->constructConditions($value, $model); |
|
623 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']) . ' {op} '; |
|
624 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
625 | + } |
|
626 | + else if ($key == 'or') |
|
627 | + { |
|
628 | + $conditions = $this->constructConditions($value, $model); |
|
629 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']) . ' {op} '; |
|
630 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
631 | + } |
|
632 | + else |
|
633 | + { |
|
634 | + if (is_array($value)) |
|
635 | + { |
|
636 | + $operator = $value['op']; |
|
637 | + if (strtolower($operator) == 'between') |
|
638 | + { |
|
639 | + $value1 = $value['val1']; |
|
640 | + $value2 = $value['val2']; |
|
641 | + } |
|
642 | + else |
|
643 | + { |
|
644 | + $value = array_key_exists('val', $value) ? $value['val'] : ''; |
|
645 | + } |
|
646 | + } |
|
647 | + else |
|
648 | + { |
|
649 | + $operator = '='; |
|
650 | + } |
|
651 | 651 | |
652 | - if (strtolower($operator) == 'between') |
|
653 | - { |
|
654 | - $conditionString .= $key . ' >= ? and '; |
|
655 | - $conditionValues[] = $value1; |
|
656 | - |
|
657 | - $conditionString .= $key . ' <= ? {op} '; |
|
658 | - $conditionValues[] = $value2; |
|
659 | - } |
|
660 | - elseif (strtolower($operator) == 'in') |
|
661 | - { |
|
662 | - $conditionValues = array_merge($conditionValues, $value); |
|
663 | - $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
664 | - $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
665 | - } |
|
666 | - elseif (strtolower($operator) == 'null') |
|
667 | - { |
|
668 | - $conditionString .= $key . ' is null {op} '; |
|
669 | - } |
|
670 | - elseif (strtolower($operator) == 'not null') |
|
671 | - { |
|
672 | - $conditionString .= $key . ' is not null {op} '; |
|
673 | - } |
|
674 | - elseif (strtolower($operator) == 'has') |
|
675 | - { |
|
676 | - $sql = $model->withTrashed()->has($key)->toSql(); |
|
677 | - $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
678 | - $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
679 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
680 | - } |
|
681 | - else |
|
682 | - { |
|
683 | - $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
684 | - $conditionValues[] = $value; |
|
685 | - } |
|
686 | - } |
|
687 | - } |
|
688 | - $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
689 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
690 | - } |
|
691 | - |
|
692 | - /** |
|
693 | - * Wrap the given JSON selector. |
|
694 | - * |
|
695 | - * @param string $value |
|
696 | - * @return string |
|
697 | - */ |
|
698 | - protected function wrapJsonSelector($value) |
|
699 | - { |
|
700 | - $removeLast = strpos($value, ')'); |
|
701 | - $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
702 | - $path = explode('->', $value); |
|
703 | - $field = array_shift($path); |
|
704 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
705 | - return '"'.$part.'"'; |
|
706 | - })->implode('.')); |
|
652 | + if (strtolower($operator) == 'between') |
|
653 | + { |
|
654 | + $conditionString .= $key . ' >= ? and '; |
|
655 | + $conditionValues[] = $value1; |
|
656 | + |
|
657 | + $conditionString .= $key . ' <= ? {op} '; |
|
658 | + $conditionValues[] = $value2; |
|
659 | + } |
|
660 | + elseif (strtolower($operator) == 'in') |
|
661 | + { |
|
662 | + $conditionValues = array_merge($conditionValues, $value); |
|
663 | + $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
664 | + $conditionString .= $key . ' in (' . rtrim($inBindingsString, ',') . ') {op} '; |
|
665 | + } |
|
666 | + elseif (strtolower($operator) == 'null') |
|
667 | + { |
|
668 | + $conditionString .= $key . ' is null {op} '; |
|
669 | + } |
|
670 | + elseif (strtolower($operator) == 'not null') |
|
671 | + { |
|
672 | + $conditionString .= $key . ' is not null {op} '; |
|
673 | + } |
|
674 | + elseif (strtolower($operator) == 'has') |
|
675 | + { |
|
676 | + $sql = $model->withTrashed()->has($key)->toSql(); |
|
677 | + $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
678 | + $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')') . ' and ' . $conditions['conditionString'] . ') {op} '; |
|
679 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
680 | + } |
|
681 | + else |
|
682 | + { |
|
683 | + $conditionString .= $key . ' ' . $operator . ' ? {op} '; |
|
684 | + $conditionValues[] = $value; |
|
685 | + } |
|
686 | + } |
|
687 | + } |
|
688 | + $conditionString = '(' . rtrim($conditionString, '{op} ') . ')'; |
|
689 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
690 | + } |
|
691 | + |
|
692 | + /** |
|
693 | + * Wrap the given JSON selector. |
|
694 | + * |
|
695 | + * @param string $value |
|
696 | + * @return string |
|
697 | + */ |
|
698 | + protected function wrapJsonSelector($value) |
|
699 | + { |
|
700 | + $removeLast = strpos($value, ')'); |
|
701 | + $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
702 | + $path = explode('->', $value); |
|
703 | + $field = array_shift($path); |
|
704 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
705 | + return '"'.$part.'"'; |
|
706 | + })->implode('.')); |
|
707 | 707 | |
708 | - return $removeLast === false ? $result : $result . ')'; |
|
709 | - } |
|
710 | - |
|
711 | - /** |
|
712 | - * Abstract method that return the necessary |
|
713 | - * information (full model namespace) |
|
714 | - * needed to preform the previous actions. |
|
715 | - * |
|
716 | - * @return string |
|
717 | - */ |
|
718 | - abstract protected function getModel(); |
|
708 | + return $removeLast === false ? $result : $result . ')'; |
|
709 | + } |
|
710 | + |
|
711 | + /** |
|
712 | + * Abstract method that return the necessary |
|
713 | + * information (full model namespace) |
|
714 | + * needed to preform the previous actions. |
|
715 | + * |
|
716 | + * @return string |
|
717 | + */ |
|
718 | + abstract protected function getModel(); |
|
719 | 719 | } |
720 | 720 | \ No newline at end of file |
@@ -6,20 +6,20 @@ discard block |
||
6 | 6 | |
7 | 7 | class AssignRelationsSeeder extends Seeder |
8 | 8 | { |
9 | - /** |
|
10 | - * Run the database seeds. |
|
11 | - * |
|
12 | - * @return void |
|
13 | - */ |
|
14 | - public function run() |
|
15 | - { |
|
16 | - $adminGroupId = \DB::table('groups')->where('name', 'admin')->select('id')->first()->id; |
|
9 | + /** |
|
10 | + * Run the database seeds. |
|
11 | + * |
|
12 | + * @return void |
|
13 | + */ |
|
14 | + public function run() |
|
15 | + { |
|
16 | + $adminGroupId = \DB::table('groups')->where('name', 'admin')->select('id')->first()->id; |
|
17 | 17 | |
18 | - /** |
|
19 | - * Assign the permissions to the admin group. |
|
20 | - */ |
|
21 | - \DB::table('permissions')->orderBy('created_at', 'asc')->whereIn('model', ['settings'])->each(function ($permission) use ($adminGroupId) { |
|
22 | - \DB::table('groups_permissions')->insert( |
|
18 | + /** |
|
19 | + * Assign the permissions to the admin group. |
|
20 | + */ |
|
21 | + \DB::table('permissions')->orderBy('created_at', 'asc')->whereIn('model', ['settings'])->each(function ($permission) use ($adminGroupId) { |
|
22 | + \DB::table('groups_permissions')->insert( |
|
23 | 23 | [ |
24 | 24 | 'permission_id' => $permission->id, |
25 | 25 | 'group_id' => $adminGroupId, |
@@ -27,6 +27,6 @@ discard block |
||
27 | 27 | 'updated_at' => \DB::raw('NOW()') |
28 | 28 | ] |
29 | 29 | ); |
30 | - }); |
|
31 | - } |
|
30 | + }); |
|
31 | + } |
|
32 | 32 | } |
@@ -18,7 +18,7 @@ |
||
18 | 18 | /** |
19 | 19 | * Assign the permissions to the admin group. |
20 | 20 | */ |
21 | - \DB::table('permissions')->orderBy('created_at', 'asc')->whereIn('model', ['settings'])->each(function ($permission) use ($adminGroupId) { |
|
21 | + \DB::table('permissions')->orderBy('created_at', 'asc')->whereIn('model', ['settings'])->each(function($permission) use ($adminGroupId) { |
|
22 | 22 | \DB::table('groups_permissions')->insert( |
23 | 23 | [ |
24 | 24 | 'permission_id' => $permission->id, |
@@ -6,15 +6,15 @@ |
||
6 | 6 | |
7 | 7 | class ClearDataSeeder extends Seeder |
8 | 8 | { |
9 | - /** |
|
10 | - * Run the database seeds. |
|
11 | - * |
|
12 | - * @return void |
|
13 | - */ |
|
14 | - public function run() |
|
15 | - { |
|
16 | - $permissions = \DB::table('permissions')->whereIn('model', ['settings']); |
|
17 | - \DB::table('groups_permissions')->whereIn('permission_id', $permissions->pluck('id'))->delete(); |
|
18 | - $permissions->delete(); |
|
19 | - } |
|
9 | + /** |
|
10 | + * Run the database seeds. |
|
11 | + * |
|
12 | + * @return void |
|
13 | + */ |
|
14 | + public function run() |
|
15 | + { |
|
16 | + $permissions = \DB::table('permissions')->whereIn('model', ['settings']); |
|
17 | + \DB::table('groups_permissions')->whereIn('permission_id', $permissions->pluck('id'))->delete(); |
|
18 | + $permissions->delete(); |
|
19 | + } |
|
20 | 20 | } |
@@ -6,15 +6,15 @@ |
||
6 | 6 | |
7 | 7 | class CoreDatabaseSeeder extends Seeder |
8 | 8 | { |
9 | - /** |
|
10 | - * Run the database seeds. |
|
11 | - * |
|
12 | - * @return void |
|
13 | - */ |
|
14 | - public function run() |
|
15 | - { |
|
16 | - $this->call(ClearDataSeeder::class); |
|
17 | - $this->call(SettingsTableSeeder::class); |
|
18 | - $this->call(AssignRelationsSeeder::class); |
|
19 | - } |
|
9 | + /** |
|
10 | + * Run the database seeds. |
|
11 | + * |
|
12 | + * @return void |
|
13 | + */ |
|
14 | + public function run() |
|
15 | + { |
|
16 | + $this->call(ClearDataSeeder::class); |
|
17 | + $this->call(SettingsTableSeeder::class); |
|
18 | + $this->call(AssignRelationsSeeder::class); |
|
19 | + } |
|
20 | 20 | } |
@@ -7,42 +7,42 @@ |
||
7 | 7 | |
8 | 8 | class PushNotificationsDevicesController extends BaseApiController |
9 | 9 | { |
10 | - /** |
|
11 | - * The name of the model that is used by the base api controller |
|
12 | - * to preform actions like (add, edit ... etc). |
|
13 | - * @var string |
|
14 | - */ |
|
15 | - protected $model = 'pushNotificationDevices'; |
|
10 | + /** |
|
11 | + * The name of the model that is used by the base api controller |
|
12 | + * to preform actions like (add, edit ... etc). |
|
13 | + * @var string |
|
14 | + */ |
|
15 | + protected $model = 'pushNotificationDevices'; |
|
16 | 16 | |
17 | - /** |
|
18 | - * List of all route actions that the base api controller |
|
19 | - * will skip permissions check for them. |
|
20 | - * @var array |
|
21 | - */ |
|
22 | - protected $skipPermissionCheck = ['registerDevice']; |
|
17 | + /** |
|
18 | + * List of all route actions that the base api controller |
|
19 | + * will skip permissions check for them. |
|
20 | + * @var array |
|
21 | + */ |
|
22 | + protected $skipPermissionCheck = ['registerDevice']; |
|
23 | 23 | |
24 | - /** |
|
25 | - * The validations rules used by the base api controller |
|
26 | - * to check before add. |
|
27 | - * @var array |
|
28 | - */ |
|
29 | - protected $validationRules = [ |
|
30 | - 'device_token' => 'required|string|max:255', |
|
31 | - 'user_id' => 'required|exists:users,id' |
|
32 | - ]; |
|
24 | + /** |
|
25 | + * The validations rules used by the base api controller |
|
26 | + * to check before add. |
|
27 | + * @var array |
|
28 | + */ |
|
29 | + protected $validationRules = [ |
|
30 | + 'device_token' => 'required|string|max:255', |
|
31 | + 'user_id' => 'required|exists:users,id' |
|
32 | + ]; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Register the given device to the logged in user. |
|
36 | - * |
|
37 | - * @param \Illuminate\Http\Request $request |
|
38 | - * @return \Illuminate\Http\Response |
|
39 | - */ |
|
40 | - public function registerDevice(Request $request) |
|
41 | - { |
|
42 | - $this->validate($request, [ |
|
43 | - 'device_token' => 'required|string|max:255' |
|
44 | - ]); |
|
34 | + /** |
|
35 | + * Register the given device to the logged in user. |
|
36 | + * |
|
37 | + * @param \Illuminate\Http\Request $request |
|
38 | + * @return \Illuminate\Http\Response |
|
39 | + */ |
|
40 | + public function registerDevice(Request $request) |
|
41 | + { |
|
42 | + $this->validate($request, [ |
|
43 | + 'device_token' => 'required|string|max:255' |
|
44 | + ]); |
|
45 | 45 | |
46 | - return \Response::json($this->repo->registerDevice($request->all()), 200); |
|
47 | - } |
|
46 | + return \Response::json($this->repo->registerDevice($request->all()), 200); |
|
47 | + } |
|
48 | 48 | } |
@@ -12,7 +12,7 @@ discard block |
||
12 | 12 | * to preform actions like (add, edit ... etc). |
13 | 13 | * @var string |
14 | 14 | */ |
15 | - protected $model = 'pushNotificationDevices'; |
|
15 | + protected $model = 'pushNotificationDevices'; |
|
16 | 16 | |
17 | 17 | /** |
18 | 18 | * List of all route actions that the base api controller |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | * to check before add. |
27 | 27 | * @var array |
28 | 28 | */ |
29 | - protected $validationRules = [ |
|
29 | + protected $validationRules = [ |
|
30 | 30 | 'device_token' => 'required|string|max:255', |
31 | 31 | 'user_id' => 'required|exists:users,id' |
32 | 32 | ]; |
@@ -8,59 +8,59 @@ |
||
8 | 8 | class NotificationsController extends BaseApiController |
9 | 9 | { |
10 | 10 | /** |
11 | - * The name of the model that is used by the base api controller |
|
12 | - * to preform actions like (add, edit ... etc). |
|
13 | - * @var string |
|
14 | - */ |
|
15 | - protected $model = 'notifications'; |
|
11 | + * The name of the model that is used by the base api controller |
|
12 | + * to preform actions like (add, edit ... etc). |
|
13 | + * @var string |
|
14 | + */ |
|
15 | + protected $model = 'notifications'; |
|
16 | 16 | |
17 | - /** |
|
18 | - * List of all route actions that the base api controller |
|
19 | - * will skip permissions check for them. |
|
20 | - * @var array |
|
21 | - */ |
|
22 | - protected $skipPermissionCheck = ['markAsRead', 'markAllAsRead']; |
|
17 | + /** |
|
18 | + * List of all route actions that the base api controller |
|
19 | + * will skip permissions check for them. |
|
20 | + * @var array |
|
21 | + */ |
|
22 | + protected $skipPermissionCheck = ['markAsRead', 'markAllAsRead']; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Retrieve all notifications of the logged in user. |
|
26 | - * |
|
27 | - * @param integer $perPage |
|
28 | - * @return \Illuminate\Http\Response |
|
29 | - */ |
|
30 | - public function list($perPage = 0) |
|
31 | - { |
|
32 | - return \Response::json($this->repo->list($perPage), 200); |
|
33 | - } |
|
24 | + /** |
|
25 | + * Retrieve all notifications of the logged in user. |
|
26 | + * |
|
27 | + * @param integer $perPage |
|
28 | + * @return \Illuminate\Http\Response |
|
29 | + */ |
|
30 | + public function list($perPage = 0) |
|
31 | + { |
|
32 | + return \Response::json($this->repo->list($perPage), 200); |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * Retrieve unread notifications of the logged in user. |
|
37 | - * |
|
38 | - * @param integer $perPage |
|
39 | - * @return \Illuminate\Http\Response |
|
40 | - */ |
|
41 | - public function unread($perPage = 0) |
|
42 | - { |
|
43 | - return \Response::json($this->repo->unread($perPage), 200); |
|
44 | - } |
|
35 | + /** |
|
36 | + * Retrieve unread notifications of the logged in user. |
|
37 | + * |
|
38 | + * @param integer $perPage |
|
39 | + * @return \Illuminate\Http\Response |
|
40 | + */ |
|
41 | + public function unread($perPage = 0) |
|
42 | + { |
|
43 | + return \Response::json($this->repo->unread($perPage), 200); |
|
44 | + } |
|
45 | 45 | |
46 | - /** |
|
47 | - * Mark the notification as read. |
|
48 | - * |
|
49 | - * @param integer $id Id of the notification. |
|
50 | - * @return \Illuminate\Http\Response |
|
51 | - */ |
|
52 | - public function markAsRead($id) |
|
53 | - { |
|
54 | - return \Response::json($this->repo->markAsRead($id), 200); |
|
55 | - } |
|
46 | + /** |
|
47 | + * Mark the notification as read. |
|
48 | + * |
|
49 | + * @param integer $id Id of the notification. |
|
50 | + * @return \Illuminate\Http\Response |
|
51 | + */ |
|
52 | + public function markAsRead($id) |
|
53 | + { |
|
54 | + return \Response::json($this->repo->markAsRead($id), 200); |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * Mark all notifications as read. |
|
59 | - * |
|
60 | - * @return \Illuminate\Http\Response |
|
61 | - */ |
|
62 | - public function markAllAsRead() |
|
63 | - { |
|
64 | - return \Response::json($this->repo->markAllAsRead(), 200); |
|
65 | - } |
|
57 | + /** |
|
58 | + * Mark all notifications as read. |
|
59 | + * |
|
60 | + * @return \Illuminate\Http\Response |
|
61 | + */ |
|
62 | + public function markAllAsRead() |
|
63 | + { |
|
64 | + return \Response::json($this->repo->markAllAsRead(), 200); |
|
65 | + } |
|
66 | 66 | } |
@@ -12,7 +12,7 @@ |
||
12 | 12 | * to preform actions like (add, edit ... etc). |
13 | 13 | * @var string |
14 | 14 | */ |
15 | - protected $model = 'notifications'; |
|
15 | + protected $model = 'notifications'; |
|
16 | 16 | |
17 | 17 | /** |
18 | 18 | * List of all route actions that the base api controller |
@@ -4,73 +4,73 @@ |
||
4 | 4 | |
5 | 5 | class NotificationRepository extends AbstractRepository |
6 | 6 | { |
7 | - /** |
|
8 | - * Return the model full namespace. |
|
9 | - * |
|
10 | - * @return string |
|
11 | - */ |
|
12 | - protected function getModel() |
|
13 | - { |
|
14 | - return 'App\Modules\V1\Notifications\Notification'; |
|
15 | - } |
|
7 | + /** |
|
8 | + * Return the model full namespace. |
|
9 | + * |
|
10 | + * @return string |
|
11 | + */ |
|
12 | + protected function getModel() |
|
13 | + { |
|
14 | + return 'App\Modules\V1\Notifications\Notification'; |
|
15 | + } |
|
16 | 16 | |
17 | - /** |
|
18 | - * Retrieve all notifications of the logged in user. |
|
19 | - * |
|
20 | - * @param integer $perPage |
|
21 | - * @return Collection |
|
22 | - */ |
|
23 | - public function list($perPage) |
|
24 | - { |
|
25 | - return \Auth::user()->notifications()->paginate($perPage); |
|
26 | - } |
|
17 | + /** |
|
18 | + * Retrieve all notifications of the logged in user. |
|
19 | + * |
|
20 | + * @param integer $perPage |
|
21 | + * @return Collection |
|
22 | + */ |
|
23 | + public function list($perPage) |
|
24 | + { |
|
25 | + return \Auth::user()->notifications()->paginate($perPage); |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * Retrieve unread notifications of the logged in user. |
|
30 | - * |
|
31 | - * @param integer $perPage |
|
32 | - * @return Collection |
|
33 | - */ |
|
34 | - public function unread($perPage) |
|
35 | - { |
|
36 | - return \Auth::user()->unreadNotifications()->paginate($perPage); |
|
37 | - } |
|
28 | + /** |
|
29 | + * Retrieve unread notifications of the logged in user. |
|
30 | + * |
|
31 | + * @param integer $perPage |
|
32 | + * @return Collection |
|
33 | + */ |
|
34 | + public function unread($perPage) |
|
35 | + { |
|
36 | + return \Auth::user()->unreadNotifications()->paginate($perPage); |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * Mark the notification as read. |
|
41 | - * |
|
42 | - * @param integer $id |
|
43 | - * @return object |
|
44 | - */ |
|
45 | - public function markAsRead($id) |
|
46 | - { |
|
47 | - if ($notification = \Auth::user()->unreadNotifications()->where('id', $id)) |
|
48 | - { |
|
49 | - $notification->first()->markAsRead(); |
|
50 | - } |
|
51 | - } |
|
39 | + /** |
|
40 | + * Mark the notification as read. |
|
41 | + * |
|
42 | + * @param integer $id |
|
43 | + * @return object |
|
44 | + */ |
|
45 | + public function markAsRead($id) |
|
46 | + { |
|
47 | + if ($notification = \Auth::user()->unreadNotifications()->where('id', $id)) |
|
48 | + { |
|
49 | + $notification->first()->markAsRead(); |
|
50 | + } |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Mark all notifications as read. |
|
55 | - * |
|
56 | - * @return void |
|
57 | - */ |
|
58 | - public function markAllAsRead() |
|
59 | - { |
|
60 | - \Auth::user()->unreadNotifications()->update(['read_at' => now()]); |
|
61 | - } |
|
53 | + /** |
|
54 | + * Mark all notifications as read. |
|
55 | + * |
|
56 | + * @return void |
|
57 | + */ |
|
58 | + public function markAllAsRead() |
|
59 | + { |
|
60 | + \Auth::user()->unreadNotifications()->update(['read_at' => now()]); |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * Notify th given user with the given notification. |
|
65 | - * |
|
66 | - * @param collection $users |
|
67 | - * @param string $notification |
|
68 | - * @param object $notificationData |
|
69 | - * @return void |
|
70 | - */ |
|
71 | - public function notify($users, $notification, $notificationData = false) |
|
72 | - { |
|
73 | - $notification = 'App\Modules\V1\Notifications\Notifications\\' . $notification; |
|
74 | - \Notification::send($users, new $notification($notificationData)); |
|
75 | - } |
|
63 | + /** |
|
64 | + * Notify th given user with the given notification. |
|
65 | + * |
|
66 | + * @param collection $users |
|
67 | + * @param string $notification |
|
68 | + * @param object $notificationData |
|
69 | + * @return void |
|
70 | + */ |
|
71 | + public function notify($users, $notification, $notificationData = false) |
|
72 | + { |
|
73 | + $notification = 'App\Modules\V1\Notifications\Notifications\\' . $notification; |
|
74 | + \Notification::send($users, new $notification($notificationData)); |
|
75 | + } |
|
76 | 76 | } |
@@ -70,7 +70,7 @@ |
||
70 | 70 | */ |
71 | 71 | public function notify($users, $notification, $notificationData = false) |
72 | 72 | { |
73 | - $notification = 'App\Modules\V1\Notifications\Notifications\\' . $notification; |
|
73 | + $notification = 'App\Modules\V1\Notifications\Notifications\\'.$notification; |
|
74 | 74 | \Notification::send($users, new $notification($notificationData)); |
75 | 75 | } |
76 | 76 | } |
@@ -7,56 +7,56 @@ |
||
7 | 7 | |
8 | 8 | class PushNotificationDeviceRepository extends AbstractRepository |
9 | 9 | { |
10 | - /** |
|
11 | - * Return the model full namespace. |
|
12 | - * |
|
13 | - * @return string |
|
14 | - */ |
|
15 | - protected function getModel() |
|
16 | - { |
|
17 | - return 'App\Modules\V1\Notifications\PushNotificationDevice'; |
|
18 | - } |
|
19 | - |
|
20 | - /** |
|
21 | - * Register the given device to the logged in user. |
|
22 | - * |
|
23 | - * @param string $data |
|
24 | - * @return void |
|
25 | - */ |
|
26 | - public function registerDevice($data) |
|
27 | - { |
|
28 | - $data['access_token'] = \Auth::user()->token(); |
|
29 | - $data['user_id'] = \Auth::id(); |
|
30 | - if ($device = $this->model->where('device_token', $data['device_token'])->where('user_id', $data['user_id'])->first()) |
|
31 | - { |
|
32 | - $data['id'] = $device->id; |
|
33 | - } |
|
34 | - |
|
35 | - return $this->save($data); |
|
36 | - } |
|
37 | - |
|
38 | - /** |
|
39 | - * Generate the given message data. |
|
40 | - * |
|
41 | - * @param string $title |
|
42 | - * @param string $message |
|
43 | - * @param string $data |
|
44 | - * @return void |
|
45 | - */ |
|
46 | - public function generateMessageData($title, $message, $data = []) |
|
47 | - { |
|
48 | - $optionBuilder = new OptionsBuilder(); |
|
49 | - $notificationBuilder = new PayloadNotificationBuilder($title); |
|
50 | - $dataBuilder = new PayloadDataBuilder(); |
|
51 | - |
|
52 | - $optionBuilder->setTimeToLive(60*20); |
|
53 | - $notificationBuilder->setBody($message); |
|
54 | - $dataBuilder->addData($data); |
|
55 | - |
|
56 | - $options = $optionBuilder->build(); |
|
57 | - $notification = $notificationBuilder->build(); |
|
58 | - $data = $dataBuilder->build(); |
|
59 | - |
|
60 | - return compact('options', 'notification', 'data'); |
|
61 | - } |
|
10 | + /** |
|
11 | + * Return the model full namespace. |
|
12 | + * |
|
13 | + * @return string |
|
14 | + */ |
|
15 | + protected function getModel() |
|
16 | + { |
|
17 | + return 'App\Modules\V1\Notifications\PushNotificationDevice'; |
|
18 | + } |
|
19 | + |
|
20 | + /** |
|
21 | + * Register the given device to the logged in user. |
|
22 | + * |
|
23 | + * @param string $data |
|
24 | + * @return void |
|
25 | + */ |
|
26 | + public function registerDevice($data) |
|
27 | + { |
|
28 | + $data['access_token'] = \Auth::user()->token(); |
|
29 | + $data['user_id'] = \Auth::id(); |
|
30 | + if ($device = $this->model->where('device_token', $data['device_token'])->where('user_id', $data['user_id'])->first()) |
|
31 | + { |
|
32 | + $data['id'] = $device->id; |
|
33 | + } |
|
34 | + |
|
35 | + return $this->save($data); |
|
36 | + } |
|
37 | + |
|
38 | + /** |
|
39 | + * Generate the given message data. |
|
40 | + * |
|
41 | + * @param string $title |
|
42 | + * @param string $message |
|
43 | + * @param string $data |
|
44 | + * @return void |
|
45 | + */ |
|
46 | + public function generateMessageData($title, $message, $data = []) |
|
47 | + { |
|
48 | + $optionBuilder = new OptionsBuilder(); |
|
49 | + $notificationBuilder = new PayloadNotificationBuilder($title); |
|
50 | + $dataBuilder = new PayloadDataBuilder(); |
|
51 | + |
|
52 | + $optionBuilder->setTimeToLive(60*20); |
|
53 | + $notificationBuilder->setBody($message); |
|
54 | + $dataBuilder->addData($data); |
|
55 | + |
|
56 | + $options = $optionBuilder->build(); |
|
57 | + $notification = $notificationBuilder->build(); |
|
58 | + $data = $dataBuilder->build(); |
|
59 | + |
|
60 | + return compact('options', 'notification', 'data'); |
|
61 | + } |
|
62 | 62 | } |
@@ -49,7 +49,7 @@ |
||
49 | 49 | $notificationBuilder = new PayloadNotificationBuilder($title); |
50 | 50 | $dataBuilder = new PayloadDataBuilder(); |
51 | 51 | |
52 | - $optionBuilder->setTimeToLive(60*20); |
|
52 | + $optionBuilder->setTimeToLive(60 * 20); |
|
53 | 53 | $notificationBuilder->setBody($message); |
54 | 54 | $dataBuilder->addData($data); |
55 | 55 |
@@ -21,7 +21,7 @@ |
||
21 | 21 | * Register the given device to the logged in user. |
22 | 22 | * |
23 | 23 | * @param string $data |
24 | - * @return void |
|
24 | + * @return boolean |
|
25 | 25 | */ |
26 | 26 | public function registerDevice($data) |
27 | 27 | { |