@@ -9,88 +9,88 @@ |
||
9 | 9 | |
10 | 10 | class Handler extends ExceptionHandler |
11 | 11 | { |
12 | - /** |
|
13 | - * A list of the exception types that are not reported. |
|
14 | - * |
|
15 | - * @var array |
|
16 | - */ |
|
17 | - protected $dontReport = [ |
|
18 | - \League\OAuth2\Server\Exception\OAuthServerException::class, |
|
19 | - ]; |
|
12 | + /** |
|
13 | + * A list of the exception types that are not reported. |
|
14 | + * |
|
15 | + * @var array |
|
16 | + */ |
|
17 | + protected $dontReport = [ |
|
18 | + \League\OAuth2\Server\Exception\OAuthServerException::class, |
|
19 | + ]; |
|
20 | 20 | |
21 | - /** |
|
22 | - * A list of the inputs that are never flashed for validation exceptions. |
|
23 | - * |
|
24 | - * @var array |
|
25 | - */ |
|
26 | - protected $dontFlash = [ |
|
27 | - 'password', |
|
28 | - 'password_confirmation', |
|
29 | - ]; |
|
21 | + /** |
|
22 | + * A list of the inputs that are never flashed for validation exceptions. |
|
23 | + * |
|
24 | + * @var array |
|
25 | + */ |
|
26 | + protected $dontFlash = [ |
|
27 | + 'password', |
|
28 | + 'password_confirmation', |
|
29 | + ]; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Report or log an exception. |
|
33 | - * |
|
34 | - * @param \Throwable $exception |
|
35 | - * @return void |
|
36 | - * |
|
37 | - * @throws \Exception |
|
38 | - */ |
|
39 | - public function report(Throwable $exception) |
|
40 | - { |
|
41 | - parent::report($exception); |
|
42 | - } |
|
31 | + /** |
|
32 | + * Report or log an exception. |
|
33 | + * |
|
34 | + * @param \Throwable $exception |
|
35 | + * @return void |
|
36 | + * |
|
37 | + * @throws \Exception |
|
38 | + */ |
|
39 | + public function report(Throwable $exception) |
|
40 | + { |
|
41 | + parent::report($exception); |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * Render an exception into an HTTP response. |
|
46 | - * |
|
47 | - * @param \Illuminate\Http\Request $request |
|
48 | - * @param \Throwable $exception |
|
49 | - * @return \Symfony\Component\HttpFoundation\Response |
|
50 | - * |
|
51 | - * @throws \Throwable |
|
52 | - */ |
|
53 | - public function render($request, Throwable $exception) |
|
54 | - { |
|
55 | - $errors = $this->getErrorHandlers(); |
|
44 | + /** |
|
45 | + * Render an exception into an HTTP response. |
|
46 | + * |
|
47 | + * @param \Illuminate\Http\Request $request |
|
48 | + * @param \Throwable $exception |
|
49 | + * @return \Symfony\Component\HttpFoundation\Response |
|
50 | + * |
|
51 | + * @throws \Throwable |
|
52 | + */ |
|
53 | + public function render($request, Throwable $exception) |
|
54 | + { |
|
55 | + $errors = $this->getErrorHandlers(); |
|
56 | 56 | |
57 | - if ($request->wantsJson()) { |
|
58 | - $exceptionClass = get_class($exception); |
|
59 | - return Arr::has($errors, $exceptionClass) ? $errors[$exceptionClass]($request, $exception) : parent::render($request, $exception); |
|
60 | - } |
|
57 | + if ($request->wantsJson()) { |
|
58 | + $exceptionClass = get_class($exception); |
|
59 | + return Arr::has($errors, $exceptionClass) ? $errors[$exceptionClass]($request, $exception) : parent::render($request, $exception); |
|
60 | + } |
|
61 | 61 | |
62 | - return parent::render($request, $exception); |
|
63 | - } |
|
62 | + return parent::render($request, $exception); |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * Return handler for defined error types |
|
67 | - * |
|
68 | - * @return array |
|
69 | - */ |
|
70 | - protected function getErrorHandlers() |
|
71 | - { |
|
72 | - return [ |
|
73 | - \Illuminate\Auth\AuthenticationException::class => function ($request, $exception) { |
|
74 | - Errors::unAuthorized(); |
|
75 | - }, |
|
76 | - \Illuminate\Database\QueryException::class => function ($request, $exception) { |
|
77 | - Errors::dbQueryError(); |
|
78 | - }, |
|
79 | - \predis\connection\connectionexception::class => function ($request, $exception) { |
|
80 | - Errors::redisNotRunning(); |
|
81 | - }, |
|
82 | - \RedisException::class => function ($request, $exception) { |
|
83 | - Errors::redisNotRunning(); |
|
84 | - }, |
|
85 | - \GuzzleHttp\Exception\ClientException::class => function ($request, $exception) { |
|
86 | - Errors::connectionError(); |
|
87 | - }, |
|
88 | - \Symfony\Component\HttpKernel\Exception\HttpException::class => function ($request, $exception) { |
|
89 | - return response()->json(['errors' => [$exception->getStatusCode() === 404 ? ($exception->getMessage() ?: 'not found') : $exception->getMessage()]], $exception->getStatusCode()); |
|
90 | - }, |
|
91 | - \Illuminate\Validation\ValidationException::class => function ($request, $exception) { |
|
92 | - return response()->json(['errors' => $exception->errors()], 422); |
|
93 | - } |
|
94 | - ]; |
|
95 | - } |
|
65 | + /** |
|
66 | + * Return handler for defined error types |
|
67 | + * |
|
68 | + * @return array |
|
69 | + */ |
|
70 | + protected function getErrorHandlers() |
|
71 | + { |
|
72 | + return [ |
|
73 | + \Illuminate\Auth\AuthenticationException::class => function ($request, $exception) { |
|
74 | + Errors::unAuthorized(); |
|
75 | + }, |
|
76 | + \Illuminate\Database\QueryException::class => function ($request, $exception) { |
|
77 | + Errors::dbQueryError(); |
|
78 | + }, |
|
79 | + \predis\connection\connectionexception::class => function ($request, $exception) { |
|
80 | + Errors::redisNotRunning(); |
|
81 | + }, |
|
82 | + \RedisException::class => function ($request, $exception) { |
|
83 | + Errors::redisNotRunning(); |
|
84 | + }, |
|
85 | + \GuzzleHttp\Exception\ClientException::class => function ($request, $exception) { |
|
86 | + Errors::connectionError(); |
|
87 | + }, |
|
88 | + \Symfony\Component\HttpKernel\Exception\HttpException::class => function ($request, $exception) { |
|
89 | + return response()->json(['errors' => [$exception->getStatusCode() === 404 ? ($exception->getMessage() ?: 'not found') : $exception->getMessage()]], $exception->getStatusCode()); |
|
90 | + }, |
|
91 | + \Illuminate\Validation\ValidationException::class => function ($request, $exception) { |
|
92 | + return response()->json(['errors' => $exception->errors()], 422); |
|
93 | + } |
|
94 | + ]; |
|
95 | + } |
|
96 | 96 | } |
@@ -70,25 +70,25 @@ |
||
70 | 70 | protected function getErrorHandlers() |
71 | 71 | { |
72 | 72 | return [ |
73 | - \Illuminate\Auth\AuthenticationException::class => function ($request, $exception) { |
|
73 | + \Illuminate\Auth\AuthenticationException::class => function($request, $exception) { |
|
74 | 74 | Errors::unAuthorized(); |
75 | 75 | }, |
76 | - \Illuminate\Database\QueryException::class => function ($request, $exception) { |
|
76 | + \Illuminate\Database\QueryException::class => function($request, $exception) { |
|
77 | 77 | Errors::dbQueryError(); |
78 | 78 | }, |
79 | - \predis\connection\connectionexception::class => function ($request, $exception) { |
|
79 | + \predis\connection\connectionexception::class => function($request, $exception) { |
|
80 | 80 | Errors::redisNotRunning(); |
81 | 81 | }, |
82 | - \RedisException::class => function ($request, $exception) { |
|
82 | + \RedisException::class => function($request, $exception) { |
|
83 | 83 | Errors::redisNotRunning(); |
84 | 84 | }, |
85 | - \GuzzleHttp\Exception\ClientException::class => function ($request, $exception) { |
|
85 | + \GuzzleHttp\Exception\ClientException::class => function($request, $exception) { |
|
86 | 86 | Errors::connectionError(); |
87 | 87 | }, |
88 | - \Symfony\Component\HttpKernel\Exception\HttpException::class => function ($request, $exception) { |
|
88 | + \Symfony\Component\HttpKernel\Exception\HttpException::class => function($request, $exception) { |
|
89 | 89 | return response()->json(['errors' => [$exception->getStatusCode() === 404 ? ($exception->getMessage() ?: 'not found') : $exception->getMessage()]], $exception->getStatusCode()); |
90 | 90 | }, |
91 | - \Illuminate\Validation\ValidationException::class => function ($request, $exception) { |
|
91 | + \Illuminate\Validation\ValidationException::class => function($request, $exception) { |
|
92 | 92 | return response()->json(['errors' => $exception->errors()], 422); |
93 | 93 | } |
94 | 94 | ]; |
@@ -8,681 +8,681 @@ |
||
8 | 8 | |
9 | 9 | abstract class BaseRepository implements BaseRepositoryInterface |
10 | 10 | { |
11 | - /** |
|
12 | - * @var object |
|
13 | - */ |
|
14 | - public $model; |
|
11 | + /** |
|
12 | + * @var object |
|
13 | + */ |
|
14 | + public $model; |
|
15 | 15 | |
16 | - /** |
|
17 | - * Init new object. |
|
18 | - * |
|
19 | - * @var mixed model |
|
20 | - * @return void |
|
21 | - */ |
|
22 | - public function __construct($model) |
|
23 | - { |
|
24 | - $this->model = $model; |
|
25 | - } |
|
26 | - |
|
27 | - /** |
|
28 | - * Fetch all records with relations from the storage. |
|
29 | - * |
|
30 | - * @param array $relations |
|
31 | - * @param string $sortBy |
|
32 | - * @param boolean $desc |
|
33 | - * @param array $columns |
|
34 | - * @return collection |
|
35 | - */ |
|
36 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
37 | - { |
|
38 | - $sort = $desc ? 'desc' : 'asc'; |
|
39 | - return $this->model->with($relations)->orderBy($sortBy, $sort)->get($columns); |
|
40 | - } |
|
16 | + /** |
|
17 | + * Init new object. |
|
18 | + * |
|
19 | + * @var mixed model |
|
20 | + * @return void |
|
21 | + */ |
|
22 | + public function __construct($model) |
|
23 | + { |
|
24 | + $this->model = $model; |
|
25 | + } |
|
26 | + |
|
27 | + /** |
|
28 | + * Fetch all records with relations from the storage. |
|
29 | + * |
|
30 | + * @param array $relations |
|
31 | + * @param string $sortBy |
|
32 | + * @param boolean $desc |
|
33 | + * @param array $columns |
|
34 | + * @return collection |
|
35 | + */ |
|
36 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
37 | + { |
|
38 | + $sort = $desc ? 'desc' : 'asc'; |
|
39 | + return $this->model->with($relations)->orderBy($sortBy, $sort)->get($columns); |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * Fetch all records with relations from storage in pages. |
|
44 | - * |
|
45 | - * @param integer $perPage |
|
46 | - * @param array $relations |
|
47 | - * @param string $sortBy |
|
48 | - * @param boolean $desc |
|
49 | - * @param array $columns |
|
50 | - * @return collection |
|
51 | - */ |
|
52 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
53 | - { |
|
54 | - $sort = $desc ? 'desc' : 'asc'; |
|
55 | - return $this->model->with($relations)->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Fetch all records with relations based on |
|
60 | - * the given condition from storage in pages. |
|
61 | - * |
|
62 | - * @param array $conditions array of conditions |
|
63 | - * @param integer $perPage |
|
64 | - * @param array $relations |
|
65 | - * @param string $sortBy |
|
66 | - * @param boolean $desc |
|
67 | - * @param array $columns |
|
68 | - * @return collection |
|
69 | - */ |
|
70 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
71 | - { |
|
72 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
73 | - $sort = $desc ? 'desc' : 'asc'; |
|
74 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * Count all records based on the given condition from storage. |
|
79 | - * |
|
80 | - * @param array $conditions array of conditions |
|
81 | - * @return collection |
|
82 | - */ |
|
83 | - public function count($conditions = false) |
|
84 | - { |
|
85 | - if($conditions) { |
|
86 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
87 | - return $this->model->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->count(); |
|
88 | - } |
|
42 | + /** |
|
43 | + * Fetch all records with relations from storage in pages. |
|
44 | + * |
|
45 | + * @param integer $perPage |
|
46 | + * @param array $relations |
|
47 | + * @param string $sortBy |
|
48 | + * @param boolean $desc |
|
49 | + * @param array $columns |
|
50 | + * @return collection |
|
51 | + */ |
|
52 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
53 | + { |
|
54 | + $sort = $desc ? 'desc' : 'asc'; |
|
55 | + return $this->model->with($relations)->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Fetch all records with relations based on |
|
60 | + * the given condition from storage in pages. |
|
61 | + * |
|
62 | + * @param array $conditions array of conditions |
|
63 | + * @param integer $perPage |
|
64 | + * @param array $relations |
|
65 | + * @param string $sortBy |
|
66 | + * @param boolean $desc |
|
67 | + * @param array $columns |
|
68 | + * @return collection |
|
69 | + */ |
|
70 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
71 | + { |
|
72 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
73 | + $sort = $desc ? 'desc' : 'asc'; |
|
74 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * Count all records based on the given condition from storage. |
|
79 | + * |
|
80 | + * @param array $conditions array of conditions |
|
81 | + * @return collection |
|
82 | + */ |
|
83 | + public function count($conditions = false) |
|
84 | + { |
|
85 | + if($conditions) { |
|
86 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
87 | + return $this->model->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->count(); |
|
88 | + } |
|
89 | 89 | |
90 | - return $this->model->count(); |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Return max value for the given column. |
|
95 | - * |
|
96 | - * @param column |
|
97 | - * @return integer |
|
98 | - */ |
|
99 | - public function max($column) |
|
100 | - { |
|
101 | - return $this->model->max($column); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Pluck column based on the given condition from storage. |
|
106 | - * |
|
107 | - * @param array $conditions array of conditions |
|
108 | - * @param string $column |
|
109 | - * @return collection |
|
110 | - */ |
|
111 | - public function pluck($conditions, $column) |
|
112 | - { |
|
113 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
114 | - return $this->model->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->pluck($column); |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Update the given data based on the given conditions. |
|
119 | - * |
|
120 | - * @param array $conditions array of conditions |
|
121 | - * @param array $data |
|
122 | - * @return collection |
|
123 | - */ |
|
124 | - public function update($conditions, $data) |
|
125 | - { |
|
126 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
127 | - return $this->model->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->update($data); |
|
128 | - } |
|
90 | + return $this->model->count(); |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Return max value for the given column. |
|
95 | + * |
|
96 | + * @param column |
|
97 | + * @return integer |
|
98 | + */ |
|
99 | + public function max($column) |
|
100 | + { |
|
101 | + return $this->model->max($column); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Pluck column based on the given condition from storage. |
|
106 | + * |
|
107 | + * @param array $conditions array of conditions |
|
108 | + * @param string $column |
|
109 | + * @return collection |
|
110 | + */ |
|
111 | + public function pluck($conditions, $column) |
|
112 | + { |
|
113 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
114 | + return $this->model->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->pluck($column); |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Update the given data based on the given conditions. |
|
119 | + * |
|
120 | + * @param array $conditions array of conditions |
|
121 | + * @param array $data |
|
122 | + * @return collection |
|
123 | + */ |
|
124 | + public function update($conditions, $data) |
|
125 | + { |
|
126 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
127 | + return $this->model->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->update($data); |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * Save the given model to the storage. |
|
132 | - * |
|
133 | - * @param array $data |
|
134 | - * @return mixed |
|
135 | - */ |
|
136 | - public function save(array $data) |
|
137 | - { |
|
138 | - $local = \Session::get('locale'); |
|
139 | - \Session::put('locale', 'all'); |
|
140 | - $model = false; |
|
141 | - $relations = []; |
|
142 | - |
|
143 | - \DB::transaction(function () use (&$model, &$relations, $data) { |
|
130 | + /** |
|
131 | + * Save the given model to the storage. |
|
132 | + * |
|
133 | + * @param array $data |
|
134 | + * @return mixed |
|
135 | + */ |
|
136 | + public function save(array $data) |
|
137 | + { |
|
138 | + $local = \Session::get('locale'); |
|
139 | + \Session::put('locale', 'all'); |
|
140 | + $model = false; |
|
141 | + $relations = []; |
|
142 | + |
|
143 | + \DB::transaction(function () use (&$model, &$relations, $data) { |
|
144 | 144 | |
145 | - $model = $this->prepareModel($data); |
|
146 | - $relations = $this->prepareRelations($data, $model); |
|
147 | - $model = $this->saveModel($model, $relations); |
|
148 | - }); |
|
149 | - \Session::put('locale', $local); |
|
145 | + $model = $this->prepareModel($data); |
|
146 | + $relations = $this->prepareRelations($data, $model); |
|
147 | + $model = $this->saveModel($model, $relations); |
|
148 | + }); |
|
149 | + \Session::put('locale', $local); |
|
150 | 150 | |
151 | - if (count($relations)) { |
|
152 | - $model->load(...array_keys($relations)); |
|
153 | - } |
|
151 | + if (count($relations)) { |
|
152 | + $model->load(...array_keys($relations)); |
|
153 | + } |
|
154 | 154 | |
155 | - return $model; |
|
156 | - } |
|
155 | + return $model; |
|
156 | + } |
|
157 | 157 | |
158 | - /** |
|
159 | - * Insert the given model/models to the storage. |
|
160 | - * |
|
161 | - * @param array $data |
|
162 | - * @return mixed |
|
163 | - */ |
|
164 | - public function insert(array $data) |
|
165 | - { |
|
166 | - return $this->model->insert($data); |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Delete record from the storage based on the given |
|
171 | - * condition. |
|
172 | - * |
|
173 | - * @param var $value condition value |
|
174 | - * @param string $attribute condition column name |
|
175 | - * @return void |
|
176 | - */ |
|
177 | - public function delete($value, $attribute = 'id') |
|
178 | - { |
|
179 | - \DB::transaction(function () use ($value, $attribute) { |
|
180 | - $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
181 | - $model->delete(); |
|
182 | - }); |
|
183 | - }); |
|
184 | - } |
|
158 | + /** |
|
159 | + * Insert the given model/models to the storage. |
|
160 | + * |
|
161 | + * @param array $data |
|
162 | + * @return mixed |
|
163 | + */ |
|
164 | + public function insert(array $data) |
|
165 | + { |
|
166 | + return $this->model->insert($data); |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Delete record from the storage based on the given |
|
171 | + * condition. |
|
172 | + * |
|
173 | + * @param var $value condition value |
|
174 | + * @param string $attribute condition column name |
|
175 | + * @return void |
|
176 | + */ |
|
177 | + public function delete($value, $attribute = 'id') |
|
178 | + { |
|
179 | + \DB::transaction(function () use ($value, $attribute) { |
|
180 | + $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
181 | + $model->delete(); |
|
182 | + }); |
|
183 | + }); |
|
184 | + } |
|
185 | 185 | |
186 | - /** |
|
187 | - * Fetch records from the storage based on the given |
|
188 | - * id. |
|
189 | - * |
|
190 | - * @param integer $id |
|
191 | - * @param string[] $relations |
|
192 | - * @param array $columns |
|
193 | - * @return object |
|
194 | - */ |
|
195 | - public function find($id, $relations = [], $columns = ['*']) |
|
196 | - { |
|
197 | - return $this->model->with($relations)->find($id, $columns); |
|
198 | - } |
|
186 | + /** |
|
187 | + * Fetch records from the storage based on the given |
|
188 | + * id. |
|
189 | + * |
|
190 | + * @param integer $id |
|
191 | + * @param string[] $relations |
|
192 | + * @param array $columns |
|
193 | + * @return object |
|
194 | + */ |
|
195 | + public function find($id, $relations = [], $columns = ['*']) |
|
196 | + { |
|
197 | + return $this->model->with($relations)->find($id, $columns); |
|
198 | + } |
|
199 | 199 | |
200 | - /** |
|
201 | - * Fetch records from the storage based on the given |
|
202 | - * condition. |
|
203 | - * |
|
204 | - * @param array $conditions array of conditions |
|
205 | - * @param array $relations |
|
206 | - * @param string $sortBy |
|
207 | - * @param boolean $desc |
|
208 | - * @param array $columns |
|
209 | - * @return collection |
|
210 | - */ |
|
211 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
212 | - { |
|
213 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
214 | - $sort = $desc ? 'desc' : 'asc'; |
|
215 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
216 | - } |
|
217 | - |
|
218 | - /** |
|
219 | - * Fetch the first record from the storage based on the given |
|
220 | - * condition. |
|
221 | - * |
|
222 | - * @param array $conditions array of conditions |
|
223 | - * @param array $relations |
|
224 | - * @param array $columns |
|
225 | - * @return object |
|
226 | - */ |
|
227 | - public function first($conditions, $relations = [], $columns = ['*']) |
|
228 | - { |
|
229 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
230 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
231 | - } |
|
232 | - |
|
233 | - /** |
|
234 | - * Return the deleted models in pages based on the given conditions. |
|
235 | - * |
|
236 | - * @param array $conditions array of conditions |
|
237 | - * @param integer $perPage |
|
238 | - * @param string $sortBy |
|
239 | - * @param boolean $desc |
|
240 | - * @param array $columns |
|
241 | - * @return collection |
|
242 | - */ |
|
243 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
244 | - { |
|
245 | - unset($conditions['page']); |
|
246 | - unset($conditions['perPage']); |
|
247 | - unset($conditions['sortBy']); |
|
248 | - unset($conditions['sort']); |
|
249 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
250 | - $sort = $desc ? 'desc' : 'asc'; |
|
251 | - $model = $this->model->onlyTrashed(); |
|
252 | - |
|
253 | - if (count($conditions['conditionValues'])) { |
|
254 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
255 | - } |
|
256 | - |
|
257 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * Restore the deleted model. |
|
262 | - * |
|
263 | - * @param integer $id |
|
264 | - * @return void |
|
265 | - */ |
|
266 | - public function restore($id) |
|
267 | - { |
|
268 | - $model = $this->model->onlyTrashed()->find($id); |
|
269 | - |
|
270 | - if (! $model) { |
|
271 | - \Errors::notFound(class_basename($this->model).' with id : '.$id); |
|
272 | - } |
|
273 | - |
|
274 | - $model->restore(); |
|
275 | - } |
|
276 | - |
|
277 | - /** |
|
278 | - * Fill the model with the given data. |
|
279 | - * |
|
280 | - * @param array $data |
|
281 | - * |
|
282 | - * @return object |
|
283 | - */ |
|
284 | - public function prepareModel($data) |
|
285 | - { |
|
286 | - $modelClass = $this->model; |
|
287 | - |
|
288 | - /** |
|
289 | - * If the id is present in the data then select the model for updating, |
|
290 | - * else create new model. |
|
291 | - * @var array |
|
292 | - */ |
|
293 | - $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
294 | - if (! $model) { |
|
295 | - \Errors::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
296 | - } |
|
297 | - |
|
298 | - /** |
|
299 | - * Construct the model object with the given data, |
|
300 | - * and if there is a relation add it to relations array, |
|
301 | - * then save the model. |
|
302 | - */ |
|
303 | - foreach ($data as $key => $value) { |
|
304 | - if (array_search($key, $model->getFillable(), true) !== false) { |
|
305 | - /** |
|
306 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
307 | - */ |
|
308 | - $model->$key = $value; |
|
309 | - } |
|
310 | - } |
|
311 | - |
|
312 | - return $model; |
|
313 | - } |
|
200 | + /** |
|
201 | + * Fetch records from the storage based on the given |
|
202 | + * condition. |
|
203 | + * |
|
204 | + * @param array $conditions array of conditions |
|
205 | + * @param array $relations |
|
206 | + * @param string $sortBy |
|
207 | + * @param boolean $desc |
|
208 | + * @param array $columns |
|
209 | + * @return collection |
|
210 | + */ |
|
211 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
212 | + { |
|
213 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
214 | + $sort = $desc ? 'desc' : 'asc'; |
|
215 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
216 | + } |
|
217 | + |
|
218 | + /** |
|
219 | + * Fetch the first record from the storage based on the given |
|
220 | + * condition. |
|
221 | + * |
|
222 | + * @param array $conditions array of conditions |
|
223 | + * @param array $relations |
|
224 | + * @param array $columns |
|
225 | + * @return object |
|
226 | + */ |
|
227 | + public function first($conditions, $relations = [], $columns = ['*']) |
|
228 | + { |
|
229 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
230 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
231 | + } |
|
232 | + |
|
233 | + /** |
|
234 | + * Return the deleted models in pages based on the given conditions. |
|
235 | + * |
|
236 | + * @param array $conditions array of conditions |
|
237 | + * @param integer $perPage |
|
238 | + * @param string $sortBy |
|
239 | + * @param boolean $desc |
|
240 | + * @param array $columns |
|
241 | + * @return collection |
|
242 | + */ |
|
243 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
244 | + { |
|
245 | + unset($conditions['page']); |
|
246 | + unset($conditions['perPage']); |
|
247 | + unset($conditions['sortBy']); |
|
248 | + unset($conditions['sort']); |
|
249 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
250 | + $sort = $desc ? 'desc' : 'asc'; |
|
251 | + $model = $this->model->onlyTrashed(); |
|
252 | + |
|
253 | + if (count($conditions['conditionValues'])) { |
|
254 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
255 | + } |
|
256 | + |
|
257 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * Restore the deleted model. |
|
262 | + * |
|
263 | + * @param integer $id |
|
264 | + * @return void |
|
265 | + */ |
|
266 | + public function restore($id) |
|
267 | + { |
|
268 | + $model = $this->model->onlyTrashed()->find($id); |
|
269 | + |
|
270 | + if (! $model) { |
|
271 | + \Errors::notFound(class_basename($this->model).' with id : '.$id); |
|
272 | + } |
|
273 | + |
|
274 | + $model->restore(); |
|
275 | + } |
|
276 | + |
|
277 | + /** |
|
278 | + * Fill the model with the given data. |
|
279 | + * |
|
280 | + * @param array $data |
|
281 | + * |
|
282 | + * @return object |
|
283 | + */ |
|
284 | + public function prepareModel($data) |
|
285 | + { |
|
286 | + $modelClass = $this->model; |
|
287 | + |
|
288 | + /** |
|
289 | + * If the id is present in the data then select the model for updating, |
|
290 | + * else create new model. |
|
291 | + * @var array |
|
292 | + */ |
|
293 | + $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
294 | + if (! $model) { |
|
295 | + \Errors::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
296 | + } |
|
297 | + |
|
298 | + /** |
|
299 | + * Construct the model object with the given data, |
|
300 | + * and if there is a relation add it to relations array, |
|
301 | + * then save the model. |
|
302 | + */ |
|
303 | + foreach ($data as $key => $value) { |
|
304 | + if (array_search($key, $model->getFillable(), true) !== false) { |
|
305 | + /** |
|
306 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
307 | + */ |
|
308 | + $model->$key = $value; |
|
309 | + } |
|
310 | + } |
|
311 | + |
|
312 | + return $model; |
|
313 | + } |
|
314 | 314 | |
315 | - /** |
|
316 | - * Prepare related models based on the given data for the given model. |
|
317 | - * |
|
318 | - * @param array $data |
|
319 | - * @param object $model |
|
320 | - * |
|
321 | - * @return array |
|
322 | - */ |
|
323 | - public function prepareRelations($data, $model) |
|
324 | - { |
|
325 | - /** |
|
326 | - * Init the relation array |
|
327 | - * |
|
328 | - * @var array |
|
329 | - */ |
|
330 | - $relations = []; |
|
331 | - |
|
332 | - /** |
|
333 | - * Construct the model object with the given data, |
|
334 | - * and if there is a relation add it to relations array, |
|
335 | - * then save the model. |
|
336 | - */ |
|
337 | - foreach ($data as $key => $value) { |
|
338 | - /** |
|
339 | - * If the attribute is a relation. |
|
340 | - */ |
|
341 | - $relation = \Str::camel($key); |
|
342 | - if (method_exists($model, $relation) && \Core::$relation()) { |
|
343 | - /** |
|
344 | - * Check if the relation is a collection. |
|
345 | - */ |
|
346 | - if (class_basename($model->$relation) == 'Collection') { |
|
347 | - /** |
|
348 | - * If the relation has no value then marke the relation data |
|
349 | - * related to the model to be deleted. |
|
350 | - */ |
|
351 | - if (! $value || ! count($value)) { |
|
352 | - $relations[$relation] = 'delete'; |
|
353 | - } |
|
354 | - } |
|
355 | - if (is_array($value)) { |
|
356 | - /** |
|
357 | - * Loop through the relation data. |
|
358 | - */ |
|
359 | - foreach ($value as $attr => $val) { |
|
360 | - /** |
|
361 | - * Get the relation model. |
|
362 | - */ |
|
363 | - $relationBaseModel = \Core::$relation()->model; |
|
364 | - |
|
365 | - /** |
|
366 | - * Check if the relation is a collection. |
|
367 | - */ |
|
368 | - if (class_basename($model->$relation) == 'Collection') { |
|
369 | - if (! is_array($val)) { |
|
370 | - $relationModel = $relationBaseModel->lockForUpdate()->find($val); |
|
371 | - } else { |
|
372 | - /** |
|
373 | - * If the id is present in the data then select the relation model for updating, |
|
374 | - * else create new model. |
|
375 | - */ |
|
376 | - $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
377 | - } |
|
378 | - |
|
379 | - /** |
|
380 | - * If model doesn't exists. |
|
381 | - */ |
|
382 | - if (! $relationModel) { |
|
383 | - \Errors::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
384 | - } |
|
385 | - |
|
386 | - if (is_array($val)) { |
|
387 | - /** |
|
388 | - * Loop through the relation attributes. |
|
389 | - */ |
|
390 | - foreach ($val as $attr => $val) { |
|
391 | - /** |
|
392 | - * Prevent the sub relations or attributes not in the fillable. |
|
393 | - */ |
|
394 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false && class_basename($model->$key()) !== 'BelongsToMany') { |
|
395 | - $relationModel->$attr = $val; |
|
396 | - } elseif (gettype($val) !== 'object' && gettype($val) !== 'array' && $attr !== 'id') { |
|
397 | - $extra[$attr] = $val; |
|
398 | - } |
|
399 | - } |
|
400 | - } |
|
401 | - |
|
402 | - if (isset($extra)) { |
|
403 | - $relationModel->extra = $extra; |
|
404 | - } |
|
405 | - $relations[$relation][] = $relationModel; |
|
406 | - } else { |
|
407 | - /** |
|
408 | - * Prevent the sub relations. |
|
409 | - */ |
|
410 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
411 | - /** |
|
412 | - * If the id is present in the data then select the relation model for updating, |
|
413 | - * else create new model. |
|
414 | - */ |
|
415 | - $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
416 | - |
|
417 | - /** |
|
418 | - * If model doesn't exists. |
|
419 | - */ |
|
420 | - if (! $relationModel) { |
|
421 | - \Errors::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
422 | - } |
|
423 | - |
|
424 | - foreach ($value as $relationAttribute => $relationValue) { |
|
425 | - /** |
|
426 | - * Prevent attributes not in the fillable. |
|
427 | - */ |
|
428 | - if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
429 | - $relationModel->$relationAttribute = $relationValue; |
|
430 | - } |
|
431 | - } |
|
432 | - |
|
433 | - $relations[$relation] = $relationModel; |
|
434 | - } |
|
435 | - } |
|
436 | - } |
|
437 | - } |
|
438 | - } |
|
439 | - } |
|
440 | - |
|
441 | - return $relations; |
|
442 | - } |
|
443 | - |
|
444 | - /** |
|
445 | - * Save the model with related models. |
|
446 | - * |
|
447 | - * @param object $model |
|
448 | - * @param array $relations |
|
449 | - * |
|
450 | - * @return object |
|
451 | - */ |
|
452 | - public function saveModel($model, $relations) |
|
453 | - { |
|
454 | - /** |
|
455 | - * Sort relations. |
|
456 | - */ |
|
457 | - $belongsTo = []; |
|
458 | - $hasOne = []; |
|
459 | - $array = []; |
|
460 | - foreach ($relations as $key => $value) { |
|
461 | - if (class_basename($model->$key()) === 'BelongsTo') { |
|
462 | - $belongsTo[$key] = $value; |
|
463 | - } elseif (class_basename($model->$key()) === 'HasOne') { |
|
464 | - $hasOne[$key] = $value; |
|
465 | - } else { |
|
466 | - $array[$key] = $value; |
|
467 | - } |
|
468 | - }; |
|
469 | - |
|
470 | - $relations = array_merge($belongsTo, $hasOne, $array); |
|
471 | - |
|
472 | - /** |
|
473 | - * Loop through the relations array. |
|
474 | - */ |
|
475 | - foreach ($relations as $key => $value) { |
|
476 | - /** |
|
477 | - * If the relation is marked for delete then delete it. |
|
478 | - */ |
|
479 | - if ($value == 'delete' && $model->$key()->count()) { |
|
480 | - switch (class_basename($model->$key())) { |
|
481 | - /** |
|
482 | - * If the relation is one to many then delete all |
|
483 | - * relations who's id isn't in the ids array. |
|
484 | - */ |
|
485 | - case 'HasMany': |
|
486 | - $model->$key()->delete(); |
|
487 | - break; |
|
488 | - |
|
489 | - /** |
|
490 | - * If the relation is many to many then |
|
491 | - * detach the previous data and attach |
|
492 | - * the ids array to the model. |
|
493 | - */ |
|
494 | - case 'BelongsToMany': |
|
495 | - $model->$key()->detach(); |
|
496 | - break; |
|
497 | - } |
|
498 | - } elseif (gettype($value) == 'array') { |
|
499 | - /** |
|
500 | - * Save the model. |
|
501 | - */ |
|
502 | - $model->save(); |
|
503 | - $ids = []; |
|
504 | - |
|
505 | - /** |
|
506 | - * Loop through the relations. |
|
507 | - */ |
|
508 | - foreach ($value as $val) { |
|
509 | - switch (class_basename($model->$key())) { |
|
510 | - /** |
|
511 | - * If the relation is one to many then update it's foreign key with |
|
512 | - * the model id and save it then add its id to ids array to delete all |
|
513 | - * relations who's id isn't in the ids array. |
|
514 | - */ |
|
515 | - case 'HasMany': |
|
516 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
517 | - $val->$foreignKeyName = $model->id; |
|
518 | - $val->save(); |
|
519 | - $ids[] = $val->id; |
|
520 | - break; |
|
521 | - |
|
522 | - /** |
|
523 | - * If the relation is many to many then add it's id to the ids array to |
|
524 | - * attache these ids to the model. |
|
525 | - */ |
|
526 | - case 'BelongsToMany': |
|
527 | - case 'MorphToMany': |
|
528 | - $extra = $val->extra; |
|
529 | - unset($val->extra); |
|
530 | - $val->save(); |
|
531 | - $ids[$val->id] = $extra ?? []; |
|
532 | - break; |
|
533 | - } |
|
534 | - } |
|
535 | - switch (class_basename($model->$key())) { |
|
536 | - /** |
|
537 | - * If the relation is one to many then delete all |
|
538 | - * relations who's id isn't in the ids array. |
|
539 | - */ |
|
540 | - case 'HasMany': |
|
541 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
542 | - break; |
|
543 | - |
|
544 | - /** |
|
545 | - * If the relation is many to many then |
|
546 | - * detach the previous data and attach |
|
547 | - * the ids array to the model. |
|
548 | - */ |
|
549 | - case 'BelongsToMany': |
|
550 | - case 'MorphToMany': |
|
551 | - $model->$key()->detach(); |
|
552 | - $model->$key()->attach($ids); |
|
553 | - break; |
|
554 | - } |
|
555 | - } else { |
|
556 | - switch (class_basename($model->$key())) { |
|
557 | - /** |
|
558 | - * If the relation is one to one. |
|
559 | - */ |
|
560 | - case 'HasOne': |
|
561 | - /** |
|
562 | - * Save the model. |
|
563 | - */ |
|
564 | - $model->save(); |
|
565 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
566 | - $value->$foreignKeyName = $model->id; |
|
567 | - $value->save(); |
|
568 | - break; |
|
569 | - case 'BelongsTo': |
|
570 | - /** |
|
571 | - * Save the model. |
|
572 | - */ |
|
573 | - $value->save(); |
|
574 | - $model->$key()->associate($value); |
|
575 | - break; |
|
576 | - } |
|
577 | - } |
|
578 | - } |
|
579 | - |
|
580 | - /** |
|
581 | - * Save the model. |
|
582 | - */ |
|
583 | - $model->save(); |
|
584 | - |
|
585 | - return $model; |
|
586 | - } |
|
587 | - |
|
588 | - /** |
|
589 | - * Build the conditions recursively for the retrieving methods. |
|
590 | - * @param array $conditions |
|
591 | - * @return array |
|
592 | - */ |
|
593 | - protected function constructConditions($conditions, $model) |
|
594 | - { |
|
595 | - $conditionString = ''; |
|
596 | - $conditionValues = []; |
|
597 | - foreach ($conditions as $key => $value) { |
|
598 | - if (Str::contains($key, '->')) { |
|
599 | - $key = $this->wrapJsonSelector($key); |
|
600 | - } |
|
601 | - |
|
602 | - if ($key == 'and') { |
|
603 | - $conditions = $this->constructConditions($value, $model); |
|
604 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
605 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
606 | - } elseif ($key == 'or') { |
|
607 | - $conditions = $this->constructConditions($value, $model); |
|
608 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
609 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
610 | - } else { |
|
611 | - if (is_array($value)) { |
|
612 | - $operator = $value['op']; |
|
613 | - if (strtolower($operator) == 'between') { |
|
614 | - $value1 = $value['val1']; |
|
615 | - $value2 = $value['val2']; |
|
616 | - } else { |
|
617 | - $value = Arr::get($value, 'val', ''); |
|
618 | - } |
|
619 | - } else { |
|
620 | - $operator = '='; |
|
621 | - } |
|
315 | + /** |
|
316 | + * Prepare related models based on the given data for the given model. |
|
317 | + * |
|
318 | + * @param array $data |
|
319 | + * @param object $model |
|
320 | + * |
|
321 | + * @return array |
|
322 | + */ |
|
323 | + public function prepareRelations($data, $model) |
|
324 | + { |
|
325 | + /** |
|
326 | + * Init the relation array |
|
327 | + * |
|
328 | + * @var array |
|
329 | + */ |
|
330 | + $relations = []; |
|
331 | + |
|
332 | + /** |
|
333 | + * Construct the model object with the given data, |
|
334 | + * and if there is a relation add it to relations array, |
|
335 | + * then save the model. |
|
336 | + */ |
|
337 | + foreach ($data as $key => $value) { |
|
338 | + /** |
|
339 | + * If the attribute is a relation. |
|
340 | + */ |
|
341 | + $relation = \Str::camel($key); |
|
342 | + if (method_exists($model, $relation) && \Core::$relation()) { |
|
343 | + /** |
|
344 | + * Check if the relation is a collection. |
|
345 | + */ |
|
346 | + if (class_basename($model->$relation) == 'Collection') { |
|
347 | + /** |
|
348 | + * If the relation has no value then marke the relation data |
|
349 | + * related to the model to be deleted. |
|
350 | + */ |
|
351 | + if (! $value || ! count($value)) { |
|
352 | + $relations[$relation] = 'delete'; |
|
353 | + } |
|
354 | + } |
|
355 | + if (is_array($value)) { |
|
356 | + /** |
|
357 | + * Loop through the relation data. |
|
358 | + */ |
|
359 | + foreach ($value as $attr => $val) { |
|
360 | + /** |
|
361 | + * Get the relation model. |
|
362 | + */ |
|
363 | + $relationBaseModel = \Core::$relation()->model; |
|
364 | + |
|
365 | + /** |
|
366 | + * Check if the relation is a collection. |
|
367 | + */ |
|
368 | + if (class_basename($model->$relation) == 'Collection') { |
|
369 | + if (! is_array($val)) { |
|
370 | + $relationModel = $relationBaseModel->lockForUpdate()->find($val); |
|
371 | + } else { |
|
372 | + /** |
|
373 | + * If the id is present in the data then select the relation model for updating, |
|
374 | + * else create new model. |
|
375 | + */ |
|
376 | + $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
377 | + } |
|
378 | + |
|
379 | + /** |
|
380 | + * If model doesn't exists. |
|
381 | + */ |
|
382 | + if (! $relationModel) { |
|
383 | + \Errors::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
384 | + } |
|
385 | + |
|
386 | + if (is_array($val)) { |
|
387 | + /** |
|
388 | + * Loop through the relation attributes. |
|
389 | + */ |
|
390 | + foreach ($val as $attr => $val) { |
|
391 | + /** |
|
392 | + * Prevent the sub relations or attributes not in the fillable. |
|
393 | + */ |
|
394 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false && class_basename($model->$key()) !== 'BelongsToMany') { |
|
395 | + $relationModel->$attr = $val; |
|
396 | + } elseif (gettype($val) !== 'object' && gettype($val) !== 'array' && $attr !== 'id') { |
|
397 | + $extra[$attr] = $val; |
|
398 | + } |
|
399 | + } |
|
400 | + } |
|
401 | + |
|
402 | + if (isset($extra)) { |
|
403 | + $relationModel->extra = $extra; |
|
404 | + } |
|
405 | + $relations[$relation][] = $relationModel; |
|
406 | + } else { |
|
407 | + /** |
|
408 | + * Prevent the sub relations. |
|
409 | + */ |
|
410 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
411 | + /** |
|
412 | + * If the id is present in the data then select the relation model for updating, |
|
413 | + * else create new model. |
|
414 | + */ |
|
415 | + $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
416 | + |
|
417 | + /** |
|
418 | + * If model doesn't exists. |
|
419 | + */ |
|
420 | + if (! $relationModel) { |
|
421 | + \Errors::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
422 | + } |
|
423 | + |
|
424 | + foreach ($value as $relationAttribute => $relationValue) { |
|
425 | + /** |
|
426 | + * Prevent attributes not in the fillable. |
|
427 | + */ |
|
428 | + if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
429 | + $relationModel->$relationAttribute = $relationValue; |
|
430 | + } |
|
431 | + } |
|
432 | + |
|
433 | + $relations[$relation] = $relationModel; |
|
434 | + } |
|
435 | + } |
|
436 | + } |
|
437 | + } |
|
438 | + } |
|
439 | + } |
|
440 | + |
|
441 | + return $relations; |
|
442 | + } |
|
443 | + |
|
444 | + /** |
|
445 | + * Save the model with related models. |
|
446 | + * |
|
447 | + * @param object $model |
|
448 | + * @param array $relations |
|
449 | + * |
|
450 | + * @return object |
|
451 | + */ |
|
452 | + public function saveModel($model, $relations) |
|
453 | + { |
|
454 | + /** |
|
455 | + * Sort relations. |
|
456 | + */ |
|
457 | + $belongsTo = []; |
|
458 | + $hasOne = []; |
|
459 | + $array = []; |
|
460 | + foreach ($relations as $key => $value) { |
|
461 | + if (class_basename($model->$key()) === 'BelongsTo') { |
|
462 | + $belongsTo[$key] = $value; |
|
463 | + } elseif (class_basename($model->$key()) === 'HasOne') { |
|
464 | + $hasOne[$key] = $value; |
|
465 | + } else { |
|
466 | + $array[$key] = $value; |
|
467 | + } |
|
468 | + }; |
|
469 | + |
|
470 | + $relations = array_merge($belongsTo, $hasOne, $array); |
|
471 | + |
|
472 | + /** |
|
473 | + * Loop through the relations array. |
|
474 | + */ |
|
475 | + foreach ($relations as $key => $value) { |
|
476 | + /** |
|
477 | + * If the relation is marked for delete then delete it. |
|
478 | + */ |
|
479 | + if ($value == 'delete' && $model->$key()->count()) { |
|
480 | + switch (class_basename($model->$key())) { |
|
481 | + /** |
|
482 | + * If the relation is one to many then delete all |
|
483 | + * relations who's id isn't in the ids array. |
|
484 | + */ |
|
485 | + case 'HasMany': |
|
486 | + $model->$key()->delete(); |
|
487 | + break; |
|
488 | + |
|
489 | + /** |
|
490 | + * If the relation is many to many then |
|
491 | + * detach the previous data and attach |
|
492 | + * the ids array to the model. |
|
493 | + */ |
|
494 | + case 'BelongsToMany': |
|
495 | + $model->$key()->detach(); |
|
496 | + break; |
|
497 | + } |
|
498 | + } elseif (gettype($value) == 'array') { |
|
499 | + /** |
|
500 | + * Save the model. |
|
501 | + */ |
|
502 | + $model->save(); |
|
503 | + $ids = []; |
|
504 | + |
|
505 | + /** |
|
506 | + * Loop through the relations. |
|
507 | + */ |
|
508 | + foreach ($value as $val) { |
|
509 | + switch (class_basename($model->$key())) { |
|
510 | + /** |
|
511 | + * If the relation is one to many then update it's foreign key with |
|
512 | + * the model id and save it then add its id to ids array to delete all |
|
513 | + * relations who's id isn't in the ids array. |
|
514 | + */ |
|
515 | + case 'HasMany': |
|
516 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
517 | + $val->$foreignKeyName = $model->id; |
|
518 | + $val->save(); |
|
519 | + $ids[] = $val->id; |
|
520 | + break; |
|
521 | + |
|
522 | + /** |
|
523 | + * If the relation is many to many then add it's id to the ids array to |
|
524 | + * attache these ids to the model. |
|
525 | + */ |
|
526 | + case 'BelongsToMany': |
|
527 | + case 'MorphToMany': |
|
528 | + $extra = $val->extra; |
|
529 | + unset($val->extra); |
|
530 | + $val->save(); |
|
531 | + $ids[$val->id] = $extra ?? []; |
|
532 | + break; |
|
533 | + } |
|
534 | + } |
|
535 | + switch (class_basename($model->$key())) { |
|
536 | + /** |
|
537 | + * If the relation is one to many then delete all |
|
538 | + * relations who's id isn't in the ids array. |
|
539 | + */ |
|
540 | + case 'HasMany': |
|
541 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
542 | + break; |
|
543 | + |
|
544 | + /** |
|
545 | + * If the relation is many to many then |
|
546 | + * detach the previous data and attach |
|
547 | + * the ids array to the model. |
|
548 | + */ |
|
549 | + case 'BelongsToMany': |
|
550 | + case 'MorphToMany': |
|
551 | + $model->$key()->detach(); |
|
552 | + $model->$key()->attach($ids); |
|
553 | + break; |
|
554 | + } |
|
555 | + } else { |
|
556 | + switch (class_basename($model->$key())) { |
|
557 | + /** |
|
558 | + * If the relation is one to one. |
|
559 | + */ |
|
560 | + case 'HasOne': |
|
561 | + /** |
|
562 | + * Save the model. |
|
563 | + */ |
|
564 | + $model->save(); |
|
565 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
566 | + $value->$foreignKeyName = $model->id; |
|
567 | + $value->save(); |
|
568 | + break; |
|
569 | + case 'BelongsTo': |
|
570 | + /** |
|
571 | + * Save the model. |
|
572 | + */ |
|
573 | + $value->save(); |
|
574 | + $model->$key()->associate($value); |
|
575 | + break; |
|
576 | + } |
|
577 | + } |
|
578 | + } |
|
579 | + |
|
580 | + /** |
|
581 | + * Save the model. |
|
582 | + */ |
|
583 | + $model->save(); |
|
584 | + |
|
585 | + return $model; |
|
586 | + } |
|
587 | + |
|
588 | + /** |
|
589 | + * Build the conditions recursively for the retrieving methods. |
|
590 | + * @param array $conditions |
|
591 | + * @return array |
|
592 | + */ |
|
593 | + protected function constructConditions($conditions, $model) |
|
594 | + { |
|
595 | + $conditionString = ''; |
|
596 | + $conditionValues = []; |
|
597 | + foreach ($conditions as $key => $value) { |
|
598 | + if (Str::contains($key, '->')) { |
|
599 | + $key = $this->wrapJsonSelector($key); |
|
600 | + } |
|
601 | + |
|
602 | + if ($key == 'and') { |
|
603 | + $conditions = $this->constructConditions($value, $model); |
|
604 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
605 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
606 | + } elseif ($key == 'or') { |
|
607 | + $conditions = $this->constructConditions($value, $model); |
|
608 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
609 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
610 | + } else { |
|
611 | + if (is_array($value)) { |
|
612 | + $operator = $value['op']; |
|
613 | + if (strtolower($operator) == 'between') { |
|
614 | + $value1 = $value['val1']; |
|
615 | + $value2 = $value['val2']; |
|
616 | + } else { |
|
617 | + $value = Arr::get($value, 'val', ''); |
|
618 | + } |
|
619 | + } else { |
|
620 | + $operator = '='; |
|
621 | + } |
|
622 | 622 | |
623 | - if (strtolower($operator) == 'between') { |
|
624 | - $conditionString .= $key.' >= ? and '; |
|
625 | - $conditionValues[] = $value1; |
|
626 | - |
|
627 | - $conditionString .= $key.' <= ? {op} '; |
|
628 | - $conditionValues[] = $value2; |
|
629 | - } elseif (strtolower($operator) == 'in') { |
|
630 | - $conditionValues = array_merge($conditionValues, $value); |
|
631 | - $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
632 | - $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
633 | - } elseif (strtolower($operator) == 'not in') { |
|
634 | - $conditionValues = array_merge($conditionValues, $value); |
|
635 | - $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
636 | - $conditionString .= $key.' not in ('.rtrim($inBindingsString, ',').') {op} '; |
|
637 | - } elseif (strtolower($operator) == 'null') { |
|
638 | - $conditionString .= $key.' is null {op} '; |
|
639 | - } elseif (strtolower($operator) == 'not null') { |
|
640 | - $conditionString .= $key.' is not null {op} '; |
|
641 | - } elseif (strtolower($operator) == 'has') { |
|
642 | - $sql = $model->withTrashed()->withoutGlobalScopes()->has($key)->toSql(); |
|
643 | - $bindings = $model->withTrashed()->withoutGlobalScopes()->has($key)->getBindings(); |
|
644 | - if($value) { |
|
645 | - $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
646 | - $conditionString .= substr(substr($sql, strpos($sql, 'exists')), 0, -1).' and '.$conditions['conditionString'].') {op} '; |
|
647 | - $conditionValues = array_merge($conditionValues, $bindings); |
|
648 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
649 | - } else { |
|
650 | - $conditionString .= substr(substr($sql, strpos($sql, 'exists')), 0, -1).') {op} '; |
|
651 | - $conditionValues = array_merge($conditionValues, $bindings); |
|
652 | - } |
|
653 | - } elseif ($operator == 'doesntHave') { |
|
654 | - $sql = $model->withTrashed()->withoutGlobalScopes()->whereDoesntHave($key)->toSql(); |
|
655 | - $bindings = $model->withTrashed()->withoutGlobalScopes()->whereDoesntHave($key)->getBindings(); |
|
656 | - $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
657 | - $conditionString .= substr(substr($sql, strpos($sql, 'not exists')), 0, -1).' and '.$conditions['conditionString'].') {op} '; |
|
658 | - $conditionValues = array_merge($conditionValues, $bindings); |
|
659 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
660 | - } else { |
|
661 | - $conditionString .= $key.' '.$operator.' ? {op} '; |
|
662 | - $conditionValues[] = $value; |
|
663 | - } |
|
664 | - } |
|
665 | - } |
|
666 | - $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
667 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
668 | - } |
|
669 | - |
|
670 | - /** |
|
671 | - * Wrap the given JSON selector. |
|
672 | - * |
|
673 | - * @param string $value |
|
674 | - * @return string |
|
675 | - */ |
|
676 | - protected function wrapJsonSelector($value) |
|
677 | - { |
|
678 | - $removeLast = strpos($value, ')'); |
|
679 | - $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
680 | - $path = explode('->', $value); |
|
681 | - $field = array_shift($path); |
|
682 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
683 | - return '"'.$part.'"'; |
|
684 | - })->implode('.')); |
|
623 | + if (strtolower($operator) == 'between') { |
|
624 | + $conditionString .= $key.' >= ? and '; |
|
625 | + $conditionValues[] = $value1; |
|
626 | + |
|
627 | + $conditionString .= $key.' <= ? {op} '; |
|
628 | + $conditionValues[] = $value2; |
|
629 | + } elseif (strtolower($operator) == 'in') { |
|
630 | + $conditionValues = array_merge($conditionValues, $value); |
|
631 | + $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
632 | + $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
633 | + } elseif (strtolower($operator) == 'not in') { |
|
634 | + $conditionValues = array_merge($conditionValues, $value); |
|
635 | + $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
636 | + $conditionString .= $key.' not in ('.rtrim($inBindingsString, ',').') {op} '; |
|
637 | + } elseif (strtolower($operator) == 'null') { |
|
638 | + $conditionString .= $key.' is null {op} '; |
|
639 | + } elseif (strtolower($operator) == 'not null') { |
|
640 | + $conditionString .= $key.' is not null {op} '; |
|
641 | + } elseif (strtolower($operator) == 'has') { |
|
642 | + $sql = $model->withTrashed()->withoutGlobalScopes()->has($key)->toSql(); |
|
643 | + $bindings = $model->withTrashed()->withoutGlobalScopes()->has($key)->getBindings(); |
|
644 | + if($value) { |
|
645 | + $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
646 | + $conditionString .= substr(substr($sql, strpos($sql, 'exists')), 0, -1).' and '.$conditions['conditionString'].') {op} '; |
|
647 | + $conditionValues = array_merge($conditionValues, $bindings); |
|
648 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
649 | + } else { |
|
650 | + $conditionString .= substr(substr($sql, strpos($sql, 'exists')), 0, -1).') {op} '; |
|
651 | + $conditionValues = array_merge($conditionValues, $bindings); |
|
652 | + } |
|
653 | + } elseif ($operator == 'doesntHave') { |
|
654 | + $sql = $model->withTrashed()->withoutGlobalScopes()->whereDoesntHave($key)->toSql(); |
|
655 | + $bindings = $model->withTrashed()->withoutGlobalScopes()->whereDoesntHave($key)->getBindings(); |
|
656 | + $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
657 | + $conditionString .= substr(substr($sql, strpos($sql, 'not exists')), 0, -1).' and '.$conditions['conditionString'].') {op} '; |
|
658 | + $conditionValues = array_merge($conditionValues, $bindings); |
|
659 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
660 | + } else { |
|
661 | + $conditionString .= $key.' '.$operator.' ? {op} '; |
|
662 | + $conditionValues[] = $value; |
|
663 | + } |
|
664 | + } |
|
665 | + } |
|
666 | + $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
667 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
668 | + } |
|
669 | + |
|
670 | + /** |
|
671 | + * Wrap the given JSON selector. |
|
672 | + * |
|
673 | + * @param string $value |
|
674 | + * @return string |
|
675 | + */ |
|
676 | + protected function wrapJsonSelector($value) |
|
677 | + { |
|
678 | + $removeLast = strpos($value, ')'); |
|
679 | + $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
680 | + $path = explode('->', $value); |
|
681 | + $field = array_shift($path); |
|
682 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
683 | + return '"'.$part.'"'; |
|
684 | + })->implode('.')); |
|
685 | 685 | |
686 | - return $removeLast === false ? $result : $result.')'; |
|
687 | - } |
|
686 | + return $removeLast === false ? $result : $result.')'; |
|
687 | + } |
|
688 | 688 | } |
@@ -8,78 +8,78 @@ |
||
8 | 8 | |
9 | 9 | class NotificationService extends BaseService |
10 | 10 | { |
11 | - /** |
|
12 | - * @var UserRepository |
|
13 | - */ |
|
14 | - protected $userRepository; |
|
11 | + /** |
|
12 | + * @var UserRepository |
|
13 | + */ |
|
14 | + protected $userRepository; |
|
15 | 15 | |
16 | - /** |
|
17 | - * Init new object. |
|
18 | - * |
|
19 | - * @param NotificationRepository $repo |
|
20 | - * @return void |
|
21 | - */ |
|
22 | - public function __construct(NotificationRepository $repo, UserRepository $userRepository) |
|
23 | - { |
|
24 | - $this->userRepository = $userRepository; |
|
25 | - parent::__construct($repo); |
|
26 | - } |
|
16 | + /** |
|
17 | + * Init new object. |
|
18 | + * |
|
19 | + * @param NotificationRepository $repo |
|
20 | + * @return void |
|
21 | + */ |
|
22 | + public function __construct(NotificationRepository $repo, UserRepository $userRepository) |
|
23 | + { |
|
24 | + $this->userRepository = $userRepository; |
|
25 | + parent::__construct($repo); |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * Retrieve all notifications of the logged in user. |
|
30 | - * |
|
31 | - * @param integer $perPage |
|
32 | - * @return Collection |
|
33 | - */ |
|
34 | - public function my($perPage) |
|
35 | - { |
|
36 | - return $this->repo->my($perPage); |
|
37 | - } |
|
28 | + /** |
|
29 | + * Retrieve all notifications of the logged in user. |
|
30 | + * |
|
31 | + * @param integer $perPage |
|
32 | + * @return Collection |
|
33 | + */ |
|
34 | + public function my($perPage) |
|
35 | + { |
|
36 | + return $this->repo->my($perPage); |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * Retrieve unread notifications of the logged in user. |
|
41 | - * |
|
42 | - * @param integer $perPage |
|
43 | - * @return Collection |
|
44 | - */ |
|
45 | - public function unread($perPage) |
|
46 | - { |
|
47 | - return $this->repo->unread($perPage); |
|
48 | - } |
|
39 | + /** |
|
40 | + * Retrieve unread notifications of the logged in user. |
|
41 | + * |
|
42 | + * @param integer $perPage |
|
43 | + * @return Collection |
|
44 | + */ |
|
45 | + public function unread($perPage) |
|
46 | + { |
|
47 | + return $this->repo->unread($perPage); |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Mark the notification as read. |
|
52 | - * |
|
53 | - * @param integer $id |
|
54 | - * @return object |
|
55 | - */ |
|
56 | - public function markAsRead($id) |
|
57 | - { |
|
58 | - return $this->repo->markAsRead($id); |
|
59 | - } |
|
50 | + /** |
|
51 | + * Mark the notification as read. |
|
52 | + * |
|
53 | + * @param integer $id |
|
54 | + * @return object |
|
55 | + */ |
|
56 | + public function markAsRead($id) |
|
57 | + { |
|
58 | + return $this->repo->markAsRead($id); |
|
59 | + } |
|
60 | 60 | |
61 | - /** |
|
62 | - * Mark all notifications as read. |
|
63 | - * |
|
64 | - * @return void |
|
65 | - */ |
|
66 | - public function markAllAsRead() |
|
67 | - { |
|
68 | - return $this->repo->markAllAsRead(); |
|
69 | - } |
|
61 | + /** |
|
62 | + * Mark all notifications as read. |
|
63 | + * |
|
64 | + * @return void |
|
65 | + */ |
|
66 | + public function markAllAsRead() |
|
67 | + { |
|
68 | + return $this->repo->markAllAsRead(); |
|
69 | + } |
|
70 | 70 | |
71 | - /** |
|
72 | - * Notify th given user with the given notification. |
|
73 | - * |
|
74 | - * @param collection $users |
|
75 | - * @param string $notification |
|
76 | - * @param Variadic $notificationData |
|
77 | - * @return void |
|
78 | - */ |
|
79 | - public function notify($users, $notification, ...$notificationData) |
|
80 | - { |
|
81 | - $users = is_array($users) ? $this->userRepository->findBy(['id' => ['op' => 'in', 'val' => $users]]) : $users; |
|
82 | - $notification = 'App\Modules\Notifications\Notifications\\'.$notification; |
|
83 | - \Notification::send($users, new $notification(...$notificationData)); |
|
84 | - } |
|
71 | + /** |
|
72 | + * Notify th given user with the given notification. |
|
73 | + * |
|
74 | + * @param collection $users |
|
75 | + * @param string $notification |
|
76 | + * @param Variadic $notificationData |
|
77 | + * @return void |
|
78 | + */ |
|
79 | + public function notify($users, $notification, ...$notificationData) |
|
80 | + { |
|
81 | + $users = is_array($users) ? $this->userRepository->findBy(['id' => ['op' => 'in', 'val' => $users]]) : $users; |
|
82 | + $notification = 'App\Modules\Notifications\Notifications\\'.$notification; |
|
83 | + \Notification::send($users, new $notification(...$notificationData)); |
|
84 | + } |
|
85 | 85 | } |