@@ -199,7 +199,7 @@ |
||
| 199 | 199 | /** |
| 200 | 200 | * Get the given method body code. |
| 201 | 201 | * |
| 202 | - * @param object $reflectionMethod |
|
| 202 | + * @param \ReflectionMethod $reflectionMethod |
|
| 203 | 203 | * @return string |
| 204 | 204 | */ |
| 205 | 205 | protected function getMethodBody($reflectionMethod) |
@@ -7,255 +7,255 @@ |
||
| 7 | 7 | |
| 8 | 8 | class GenerateDoc extends Command |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * The name and signature of the console command. |
|
| 12 | - * |
|
| 13 | - * @var string |
|
| 14 | - */ |
|
| 15 | - protected $signature = 'doc:generate'; |
|
| 10 | + /** |
|
| 11 | + * The name and signature of the console command. |
|
| 12 | + * |
|
| 13 | + * @var string |
|
| 14 | + */ |
|
| 15 | + protected $signature = 'doc:generate'; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * The console command description. |
|
| 19 | - * |
|
| 20 | - * @var string |
|
| 21 | - */ |
|
| 22 | - protected $description = 'Generate api documentation'; |
|
| 17 | + /** |
|
| 18 | + * The console command description. |
|
| 19 | + * |
|
| 20 | + * @var string |
|
| 21 | + */ |
|
| 22 | + protected $description = 'Generate api documentation'; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Init new object. |
|
| 26 | - * |
|
| 27 | - * @return void |
|
| 28 | - */ |
|
| 29 | - public function __construct() |
|
| 30 | - { |
|
| 31 | - parent::__construct(); |
|
| 32 | - } |
|
| 24 | + /** |
|
| 25 | + * Init new object. |
|
| 26 | + * |
|
| 27 | + * @return void |
|
| 28 | + */ |
|
| 29 | + public function __construct() |
|
| 30 | + { |
|
| 31 | + parent::__construct(); |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Execute the console command. |
|
| 36 | - * |
|
| 37 | - * @return mixed |
|
| 38 | - */ |
|
| 39 | - public function handle() |
|
| 40 | - { |
|
| 41 | - $docData = []; |
|
| 42 | - $docData['models'] = []; |
|
| 43 | - $routes = $this->getRoutes(); |
|
| 44 | - foreach ($routes as $route) { |
|
| 45 | - if ($route) { |
|
| 46 | - $actoinArray = explode('@', $route['action']); |
|
| 47 | - if (Arr::get($actoinArray, 1, false)) { |
|
| 48 | - $controller = $actoinArray[0]; |
|
| 49 | - $method = $actoinArray[1]; |
|
| 50 | - $route['name'] = $method !== 'index' ? $method : 'list'; |
|
| 34 | + /** |
|
| 35 | + * Execute the console command. |
|
| 36 | + * |
|
| 37 | + * @return mixed |
|
| 38 | + */ |
|
| 39 | + public function handle() |
|
| 40 | + { |
|
| 41 | + $docData = []; |
|
| 42 | + $docData['models'] = []; |
|
| 43 | + $routes = $this->getRoutes(); |
|
| 44 | + foreach ($routes as $route) { |
|
| 45 | + if ($route) { |
|
| 46 | + $actoinArray = explode('@', $route['action']); |
|
| 47 | + if (Arr::get($actoinArray, 1, false)) { |
|
| 48 | + $controller = $actoinArray[0]; |
|
| 49 | + $method = $actoinArray[1]; |
|
| 50 | + $route['name'] = $method !== 'index' ? $method : 'list'; |
|
| 51 | 51 | |
| 52 | - $reflectionClass = new \ReflectionClass($controller); |
|
| 53 | - $reflectionMethod = $reflectionClass->getMethod($method); |
|
| 54 | - $classProperties = $reflectionClass->getDefaultProperties(); |
|
| 55 | - $skipLoginCheck = Arr::get($classProperties, 'skipLoginCheck', false); |
|
| 56 | - $validationRules = Arr::get($classProperties, 'validationRules', false); |
|
| 52 | + $reflectionClass = new \ReflectionClass($controller); |
|
| 53 | + $reflectionMethod = $reflectionClass->getMethod($method); |
|
| 54 | + $classProperties = $reflectionClass->getDefaultProperties(); |
|
| 55 | + $skipLoginCheck = Arr::get($classProperties, 'skipLoginCheck', false); |
|
| 56 | + $validationRules = Arr::get($classProperties, 'validationRules', false); |
|
| 57 | 57 | |
| 58 | - dd($classProperties); |
|
| 59 | - $this->processDocBlock($route, $reflectionMethod); |
|
| 60 | - $this->getHeaders($route, $method, $skipLoginCheck); |
|
| 61 | - $this->getPostData($route, $reflectionMethod, $validationRules); |
|
| 58 | + dd($classProperties); |
|
| 59 | + $this->processDocBlock($route, $reflectionMethod); |
|
| 60 | + $this->getHeaders($route, $method, $skipLoginCheck); |
|
| 61 | + $this->getPostData($route, $reflectionMethod, $validationRules); |
|
| 62 | 62 | |
| 63 | - $route['response'] = $this->getResponseObject($classProperties['model'], $route['name'], $route['returnDocBlock']); |
|
| 63 | + $route['response'] = $this->getResponseObject($classProperties['model'], $route['name'], $route['returnDocBlock']); |
|
| 64 | 64 | |
| 65 | - preg_match('/api\/([^#]+)\//iU', $route['uri'], $module); |
|
| 66 | - $docData['modules'][$module[1]][substr($route['prefix'], strlen('/api/'.$module[1].'/') - 1)][] = $route; |
|
| 65 | + preg_match('/api\/([^#]+)\//iU', $route['uri'], $module); |
|
| 66 | + $docData['modules'][$module[1]][substr($route['prefix'], strlen('/api/'.$module[1].'/') - 1)][] = $route; |
|
| 67 | 67 | |
| 68 | - $this->getModels($classProperties['model'], $docData); |
|
| 69 | - } |
|
| 70 | - } |
|
| 71 | - } |
|
| 68 | + $this->getModels($classProperties['model'], $docData); |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - $docData['errors'] = $this->getErrors(); |
|
| 74 | - $docData['reports'] = \Core::reports()->all(); |
|
| 75 | - \File::put(app_path('Modules/Core/Resources/api.json'), json_encode($docData)); |
|
| 76 | - } |
|
| 73 | + $docData['errors'] = $this->getErrors(); |
|
| 74 | + $docData['reports'] = \Core::reports()->all(); |
|
| 75 | + \File::put(app_path('Modules/Core/Resources/api.json'), json_encode($docData)); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * Get list of all registered routes. |
|
| 80 | - * |
|
| 81 | - * @return collection |
|
| 82 | - */ |
|
| 83 | - protected function getRoutes() |
|
| 84 | - { |
|
| 85 | - return collect(\Route::getRoutes())->map(function ($route) { |
|
| 86 | - if (strpos($route->uri(), 'api/') !== false) { |
|
| 87 | - return [ |
|
| 88 | - 'method' => $route->methods()[0], |
|
| 89 | - 'uri' => $route->uri(), |
|
| 90 | - 'action' => $route->getActionName(), |
|
| 91 | - 'prefix' => $route->getPrefix() |
|
| 92 | - ]; |
|
| 93 | - } |
|
| 94 | - return false; |
|
| 95 | - })->all(); |
|
| 96 | - } |
|
| 78 | + /** |
|
| 79 | + * Get list of all registered routes. |
|
| 80 | + * |
|
| 81 | + * @return collection |
|
| 82 | + */ |
|
| 83 | + protected function getRoutes() |
|
| 84 | + { |
|
| 85 | + return collect(\Route::getRoutes())->map(function ($route) { |
|
| 86 | + if (strpos($route->uri(), 'api/') !== false) { |
|
| 87 | + return [ |
|
| 88 | + 'method' => $route->methods()[0], |
|
| 89 | + 'uri' => $route->uri(), |
|
| 90 | + 'action' => $route->getActionName(), |
|
| 91 | + 'prefix' => $route->getPrefix() |
|
| 92 | + ]; |
|
| 93 | + } |
|
| 94 | + return false; |
|
| 95 | + })->all(); |
|
| 96 | + } |
|
| 97 | 97 | |
| 98 | - /** |
|
| 99 | - * Generate headers for the given route. |
|
| 100 | - * |
|
| 101 | - * @param array &$route |
|
| 102 | - * @param string $method |
|
| 103 | - * @param array $skipLoginCheck |
|
| 104 | - * @return void |
|
| 105 | - */ |
|
| 106 | - protected function getHeaders(&$route, $method, $skipLoginCheck) |
|
| 107 | - { |
|
| 108 | - $route['headers'] = [ |
|
| 109 | - 'Accept' => 'application/json', |
|
| 110 | - 'Content-Type' => 'application/json', |
|
| 111 | - 'locale' => 'The language of the returned data: ar, en or all.', |
|
| 112 | - 'time-zone' => 'Your locale time zone', |
|
| 113 | - ]; |
|
| 98 | + /** |
|
| 99 | + * Generate headers for the given route. |
|
| 100 | + * |
|
| 101 | + * @param array &$route |
|
| 102 | + * @param string $method |
|
| 103 | + * @param array $skipLoginCheck |
|
| 104 | + * @return void |
|
| 105 | + */ |
|
| 106 | + protected function getHeaders(&$route, $method, $skipLoginCheck) |
|
| 107 | + { |
|
| 108 | + $route['headers'] = [ |
|
| 109 | + 'Accept' => 'application/json', |
|
| 110 | + 'Content-Type' => 'application/json', |
|
| 111 | + 'locale' => 'The language of the returned data: ar, en or all.', |
|
| 112 | + 'time-zone' => 'Your locale time zone', |
|
| 113 | + ]; |
|
| 114 | 114 | |
| 115 | 115 | |
| 116 | - if (! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) { |
|
| 117 | - $route['headers']['Authorization'] = 'Bearer {token}'; |
|
| 118 | - } |
|
| 119 | - } |
|
| 116 | + if (! $skipLoginCheck || ! in_array($method, $skipLoginCheck)) { |
|
| 117 | + $route['headers']['Authorization'] = 'Bearer {token}'; |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | - /** |
|
| 122 | - * Generate description and params for the given route |
|
| 123 | - * based on the docblock. |
|
| 124 | - * |
|
| 125 | - * @param array &$route |
|
| 126 | - * @param \ReflectionMethod $reflectionMethod |
|
| 127 | - * @return void |
|
| 128 | - */ |
|
| 129 | - protected function processDocBlock(&$route, $reflectionMethod) |
|
| 130 | - { |
|
| 131 | - $factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance(); |
|
| 132 | - $docblock = $factory->create($reflectionMethod->getDocComment()); |
|
| 133 | - $route['description'] = trim(preg_replace('/\s+/', ' ', $docblock->getSummary())); |
|
| 134 | - $params = $docblock->getTagsByName('param'); |
|
| 135 | - $route['returnDocBlock'] = $docblock->getTagsByName('return')[0]->getType()->getFqsen()->getName(); |
|
| 136 | - foreach ($params as $param) { |
|
| 137 | - $name = $param->getVariableName(); |
|
| 138 | - if ($name !== 'request') { |
|
| 139 | - $route['parametars'][$param->getVariableName()] = $param->getDescription()->render(); |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - } |
|
| 121 | + /** |
|
| 122 | + * Generate description and params for the given route |
|
| 123 | + * based on the docblock. |
|
| 124 | + * |
|
| 125 | + * @param array &$route |
|
| 126 | + * @param \ReflectionMethod $reflectionMethod |
|
| 127 | + * @return void |
|
| 128 | + */ |
|
| 129 | + protected function processDocBlock(&$route, $reflectionMethod) |
|
| 130 | + { |
|
| 131 | + $factory = \phpDocumentor\Reflection\DocBlockFactory::createInstance(); |
|
| 132 | + $docblock = $factory->create($reflectionMethod->getDocComment()); |
|
| 133 | + $route['description'] = trim(preg_replace('/\s+/', ' ', $docblock->getSummary())); |
|
| 134 | + $params = $docblock->getTagsByName('param'); |
|
| 135 | + $route['returnDocBlock'] = $docblock->getTagsByName('return')[0]->getType()->getFqsen()->getName(); |
|
| 136 | + foreach ($params as $param) { |
|
| 137 | + $name = $param->getVariableName(); |
|
| 138 | + if ($name !== 'request') { |
|
| 139 | + $route['parametars'][$param->getVariableName()] = $param->getDescription()->render(); |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | 143 | |
| 144 | - /** |
|
| 145 | - * Generate post body for the given route. |
|
| 146 | - * |
|
| 147 | - * @param array &$route |
|
| 148 | - * @param \ReflectionMethod $reflectionMethod |
|
| 149 | - * @param array $validationRules |
|
| 150 | - * @return void |
|
| 151 | - */ |
|
| 152 | - protected function getPostData(&$route, $reflectionMethod, $validationRules) |
|
| 153 | - { |
|
| 154 | - if ($route['method'] == 'POST') { |
|
| 155 | - $body = $this->getMethodBody($reflectionMethod); |
|
| 144 | + /** |
|
| 145 | + * Generate post body for the given route. |
|
| 146 | + * |
|
| 147 | + * @param array &$route |
|
| 148 | + * @param \ReflectionMethod $reflectionMethod |
|
| 149 | + * @param array $validationRules |
|
| 150 | + * @return void |
|
| 151 | + */ |
|
| 152 | + protected function getPostData(&$route, $reflectionMethod, $validationRules) |
|
| 153 | + { |
|
| 154 | + if ($route['method'] == 'POST') { |
|
| 155 | + $body = $this->getMethodBody($reflectionMethod); |
|
| 156 | 156 | |
| 157 | - preg_match('/\$this->validate\(\$request,([^#]+)\);/iU', $body, $match); |
|
| 158 | - if (count($match)) { |
|
| 159 | - if ($match[1] == '$this->validationRules') { |
|
| 160 | - $route['body'] = $validationRules; |
|
| 161 | - } else { |
|
| 162 | - $route['body'] = eval('return '.str_replace(',\'.$request->get(\'id\')', ',{id}\'', $match[1]).';'); |
|
| 163 | - } |
|
| 157 | + preg_match('/\$this->validate\(\$request,([^#]+)\);/iU', $body, $match); |
|
| 158 | + if (count($match)) { |
|
| 159 | + if ($match[1] == '$this->validationRules') { |
|
| 160 | + $route['body'] = $validationRules; |
|
| 161 | + } else { |
|
| 162 | + $route['body'] = eval('return '.str_replace(',\'.$request->get(\'id\')', ',{id}\'', $match[1]).';'); |
|
| 163 | + } |
|
| 164 | 164 | |
| 165 | - foreach ($route['body'] as &$rule) { |
|
| 166 | - if (strpos($rule, 'unique')) { |
|
| 167 | - $rule = substr($rule, 0, strpos($rule, 'unique') + 6); |
|
| 168 | - } elseif (strpos($rule, 'exists')) { |
|
| 169 | - $rule = substr($rule, 0, strpos($rule, 'exists') - 1); |
|
| 170 | - } |
|
| 171 | - } |
|
| 172 | - } else { |
|
| 173 | - $route['body'] = 'conditions'; |
|
| 174 | - } |
|
| 175 | - } |
|
| 176 | - } |
|
| 165 | + foreach ($route['body'] as &$rule) { |
|
| 166 | + if (strpos($rule, 'unique')) { |
|
| 167 | + $rule = substr($rule, 0, strpos($rule, 'unique') + 6); |
|
| 168 | + } elseif (strpos($rule, 'exists')) { |
|
| 169 | + $rule = substr($rule, 0, strpos($rule, 'exists') - 1); |
|
| 170 | + } |
|
| 171 | + } |
|
| 172 | + } else { |
|
| 173 | + $route['body'] = 'conditions'; |
|
| 174 | + } |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | - /** |
|
| 179 | - * Generate application errors. |
|
| 180 | - * |
|
| 181 | - * @return array |
|
| 182 | - */ |
|
| 183 | - protected function getErrors() |
|
| 184 | - { |
|
| 185 | - $errors = []; |
|
| 186 | - $reflectionClass = new \ReflectionClass('App\Modules\Core\Utl\ErrorHandler'); |
|
| 187 | - foreach ($reflectionClass->getMethods() as $method) { |
|
| 188 | - $methodName = $method->name; |
|
| 189 | - $reflectionMethod = $reflectionClass->getMethod($methodName); |
|
| 190 | - $body = $this->getMethodBody($reflectionMethod); |
|
| 178 | + /** |
|
| 179 | + * Generate application errors. |
|
| 180 | + * |
|
| 181 | + * @return array |
|
| 182 | + */ |
|
| 183 | + protected function getErrors() |
|
| 184 | + { |
|
| 185 | + $errors = []; |
|
| 186 | + $reflectionClass = new \ReflectionClass('App\Modules\Core\Utl\ErrorHandler'); |
|
| 187 | + foreach ($reflectionClass->getMethods() as $method) { |
|
| 188 | + $methodName = $method->name; |
|
| 189 | + $reflectionMethod = $reflectionClass->getMethod($methodName); |
|
| 190 | + $body = $this->getMethodBody($reflectionMethod); |
|
| 191 | 191 | |
| 192 | - preg_match('/\$error=\[\'status\'=>([^#]+)\,/iU', $body, $match); |
|
| 192 | + preg_match('/\$error=\[\'status\'=>([^#]+)\,/iU', $body, $match); |
|
| 193 | 193 | |
| 194 | - if (count($match)) { |
|
| 195 | - $errors[$match[1]][] = $methodName; |
|
| 196 | - } |
|
| 197 | - } |
|
| 194 | + if (count($match)) { |
|
| 195 | + $errors[$match[1]][] = $methodName; |
|
| 196 | + } |
|
| 197 | + } |
|
| 198 | 198 | |
| 199 | - return $errors; |
|
| 200 | - } |
|
| 199 | + return $errors; |
|
| 200 | + } |
|
| 201 | 201 | |
| 202 | - /** |
|
| 203 | - * Get the given method body code. |
|
| 204 | - * |
|
| 205 | - * @param object $reflectionMethod |
|
| 206 | - * @return string |
|
| 207 | - */ |
|
| 208 | - protected function getMethodBody($reflectionMethod) |
|
| 209 | - { |
|
| 210 | - $filename = $reflectionMethod->getFileName(); |
|
| 211 | - $start_line = $reflectionMethod->getStartLine() - 1; |
|
| 212 | - $end_line = $reflectionMethod->getEndLine(); |
|
| 213 | - $length = $end_line - $start_line; |
|
| 214 | - $source = file($filename); |
|
| 215 | - $body = implode("", array_slice($source, $start_line, $length)); |
|
| 216 | - $body = trim(preg_replace('/\s+/', '', $body)); |
|
| 202 | + /** |
|
| 203 | + * Get the given method body code. |
|
| 204 | + * |
|
| 205 | + * @param object $reflectionMethod |
|
| 206 | + * @return string |
|
| 207 | + */ |
|
| 208 | + protected function getMethodBody($reflectionMethod) |
|
| 209 | + { |
|
| 210 | + $filename = $reflectionMethod->getFileName(); |
|
| 211 | + $start_line = $reflectionMethod->getStartLine() - 1; |
|
| 212 | + $end_line = $reflectionMethod->getEndLine(); |
|
| 213 | + $length = $end_line - $start_line; |
|
| 214 | + $source = file($filename); |
|
| 215 | + $body = implode("", array_slice($source, $start_line, $length)); |
|
| 216 | + $body = trim(preg_replace('/\s+/', '', $body)); |
|
| 217 | 217 | |
| 218 | - return $body; |
|
| 219 | - } |
|
| 218 | + return $body; |
|
| 219 | + } |
|
| 220 | 220 | |
| 221 | - /** |
|
| 222 | - * Get example object of all availble models. |
|
| 223 | - * |
|
| 224 | - * @param string $modelName |
|
| 225 | - * @param array $docData |
|
| 226 | - * @return string |
|
| 227 | - */ |
|
| 228 | - protected function getModels($modelName, &$docData) |
|
| 229 | - { |
|
| 230 | - if ($modelName && ! Arr::has($docData['models'], $modelName)) { |
|
| 231 | - $modelClass = call_user_func_array("\Core::{$modelName}", [])->modelClass; |
|
| 232 | - $model = factory($modelClass)->make(); |
|
| 233 | - $modelArr = $model->toArray(); |
|
| 221 | + /** |
|
| 222 | + * Get example object of all availble models. |
|
| 223 | + * |
|
| 224 | + * @param string $modelName |
|
| 225 | + * @param array $docData |
|
| 226 | + * @return string |
|
| 227 | + */ |
|
| 228 | + protected function getModels($modelName, &$docData) |
|
| 229 | + { |
|
| 230 | + if ($modelName && ! Arr::has($docData['models'], $modelName)) { |
|
| 231 | + $modelClass = call_user_func_array("\Core::{$modelName}", [])->modelClass; |
|
| 232 | + $model = factory($modelClass)->make(); |
|
| 233 | + $modelArr = $model->toArray(); |
|
| 234 | 234 | |
| 235 | - if ($model->trans && ! $model->trans->count()) { |
|
| 236 | - $modelArr['trans'] = [ |
|
| 237 | - 'en' => factory($modelClass.'Translation')->make()->toArray() |
|
| 238 | - ]; |
|
| 239 | - } |
|
| 235 | + if ($model->trans && ! $model->trans->count()) { |
|
| 236 | + $modelArr['trans'] = [ |
|
| 237 | + 'en' => factory($modelClass.'Translation')->make()->toArray() |
|
| 238 | + ]; |
|
| 239 | + } |
|
| 240 | 240 | |
| 241 | - $docData['models'][$modelName] = json_encode($modelArr, JSON_PRETTY_PRINT); |
|
| 242 | - } |
|
| 243 | - } |
|
| 241 | + $docData['models'][$modelName] = json_encode($modelArr, JSON_PRETTY_PRINT); |
|
| 242 | + } |
|
| 243 | + } |
|
| 244 | 244 | |
| 245 | - /** |
|
| 246 | - * Get the route response object type. |
|
| 247 | - * |
|
| 248 | - * @param string $modelName |
|
| 249 | - * @param string $method |
|
| 250 | - * @param string $returnDocBlock |
|
| 251 | - * @return array |
|
| 252 | - */ |
|
| 253 | - protected function getResponseObject($modelName, $method, $returnDocBlock) |
|
| 254 | - { |
|
| 255 | - $config = \CoreConfig::getConfig(); |
|
| 256 | - $relations = Arr::has($config['relations'], $modelName) ? Arr::has($config['relations'][$modelName], $method) ? $config['relations'][$modelName] : false : false; |
|
| 257 | - $modelName = call_user_func_array("\Core::{$returnDocBlock}", []) ? $returnDocBlock : $modelName; |
|
| 245 | + /** |
|
| 246 | + * Get the route response object type. |
|
| 247 | + * |
|
| 248 | + * @param string $modelName |
|
| 249 | + * @param string $method |
|
| 250 | + * @param string $returnDocBlock |
|
| 251 | + * @return array |
|
| 252 | + */ |
|
| 253 | + protected function getResponseObject($modelName, $method, $returnDocBlock) |
|
| 254 | + { |
|
| 255 | + $config = \CoreConfig::getConfig(); |
|
| 256 | + $relations = Arr::has($config['relations'], $modelName) ? Arr::has($config['relations'][$modelName], $method) ? $config['relations'][$modelName] : false : false; |
|
| 257 | + $modelName = call_user_func_array("\Core::{$returnDocBlock}", []) ? $returnDocBlock : $modelName; |
|
| 258 | 258 | |
| 259 | - return $relations ? [$modelName => $relations && $relations[$method] ? $relations[$method] : []] : false; |
|
| 260 | - } |
|
| 259 | + return $relations ? [$modelName => $relations && $relations[$method] ? $relations[$method] : []] : false; |
|
| 260 | + } |
|
| 261 | 261 | } |
@@ -28,10 +28,6 @@ discard block |
||
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | - * @param string $uri |
|
| 32 | - * @param array $data |
|
| 33 | - * @param array $headers |
|
| 34 | - * @param string $content |
|
| 35 | 31 | * @return \Illuminate\Http\Response |
| 36 | 32 | */ |
| 37 | 33 | public function get() |
@@ -40,10 +36,6 @@ discard block |
||
| 40 | 36 | } |
| 41 | 37 | |
| 42 | 38 | /** |
| 43 | - * @param string $uri |
|
| 44 | - * @param array $data |
|
| 45 | - * @param array $headers |
|
| 46 | - * @param string $content |
|
| 47 | 39 | * @return \Illuminate\Http\Response |
| 48 | 40 | */ |
| 49 | 41 | public function post() |
@@ -52,10 +44,6 @@ discard block |
||
| 52 | 44 | } |
| 53 | 45 | |
| 54 | 46 | /** |
| 55 | - * @param string $uri |
|
| 56 | - * @param array $data |
|
| 57 | - * @param array $headers |
|
| 58 | - * @param string $content |
|
| 59 | 47 | * @return \Illuminate\Http\Response |
| 60 | 48 | */ |
| 61 | 49 | public function put() |
@@ -64,10 +52,6 @@ discard block |
||
| 64 | 52 | } |
| 65 | 53 | |
| 66 | 54 | /** |
| 67 | - * @param string $uri |
|
| 68 | - * @param array $data |
|
| 69 | - * @param array $headers |
|
| 70 | - * @param string $content |
|
| 71 | 55 | * @return \Illuminate\Http\Response |
| 72 | 56 | */ |
| 73 | 57 | public function delete() |
@@ -7,218 +7,218 @@ |
||
| 7 | 7 | class ApiConsumer |
| 8 | 8 | { |
| 9 | 9 | |
| 10 | - private $app; |
|
| 11 | - |
|
| 12 | - private $router; |
|
| 13 | - |
|
| 14 | - private $request; |
|
| 15 | - |
|
| 16 | - private $disableMiddleware = false; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * @param \Illuminate\Foundation\Application $app |
|
| 20 | - * @param \Illuminate\Http\Request $request, |
|
| 21 | - * @param \Illuminate\Routing\Router $router |
|
| 22 | - */ |
|
| 23 | - public function __construct(Application $app, Request $request, LaravelRouter $router) |
|
| 24 | - { |
|
| 25 | - $this->app = $app; |
|
| 26 | - $this->request = $request; |
|
| 27 | - $this->router = $router; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @param string $uri |
|
| 32 | - * @param array $data |
|
| 33 | - * @param array $headers |
|
| 34 | - * @param string $content |
|
| 35 | - * @return \Illuminate\Http\Response |
|
| 36 | - */ |
|
| 37 | - public function get() |
|
| 38 | - { |
|
| 39 | - return $this->quickCall('GET', func_get_args()); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @param string $uri |
|
| 44 | - * @param array $data |
|
| 45 | - * @param array $headers |
|
| 46 | - * @param string $content |
|
| 47 | - * @return \Illuminate\Http\Response |
|
| 48 | - */ |
|
| 49 | - public function post() |
|
| 50 | - { |
|
| 51 | - return $this->quickCall('POST', func_get_args()); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @param string $uri |
|
| 56 | - * @param array $data |
|
| 57 | - * @param array $headers |
|
| 58 | - * @param string $content |
|
| 59 | - * @return \Illuminate\Http\Response |
|
| 60 | - */ |
|
| 61 | - public function put() |
|
| 62 | - { |
|
| 63 | - return $this->quickCall('PUT', func_get_args()); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @param string $uri |
|
| 68 | - * @param array $data |
|
| 69 | - * @param array $headers |
|
| 70 | - * @param string $content |
|
| 71 | - * @return \Illuminate\Http\Response |
|
| 72 | - */ |
|
| 73 | - public function delete() |
|
| 74 | - { |
|
| 75 | - return $this->quickCall('DELETE', func_get_args()); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * @param array $requests An array of requests |
|
| 80 | - * @return array |
|
| 81 | - */ |
|
| 82 | - public function batchRequest(array $requests) |
|
| 83 | - { |
|
| 84 | - foreach ($requests as $i => $request) { |
|
| 85 | - $requests[$i] = call_user_func_array([$this, 'singleRequest'], $request); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - return $requests; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * @param string $method |
|
| 93 | - * @param array $args |
|
| 94 | - * @return \Illuminate\Http\Response |
|
| 95 | - */ |
|
| 96 | - public function quickCall($method, array $args) |
|
| 97 | - { |
|
| 98 | - array_unshift($args, $method); |
|
| 99 | - return call_user_func_array([$this, "singleRequest"], $args); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @param string $method |
|
| 104 | - * @param string $uri |
|
| 105 | - * @param array $data |
|
| 106 | - * @param array $headers |
|
| 107 | - * @param string $content |
|
| 108 | - * @return \Illuminate\Http\Response |
|
| 109 | - */ |
|
| 110 | - public function singleRequest($method, $uri, array $data = [], array $headers = [], $content = null) |
|
| 111 | - { |
|
| 112 | - // Save the current request so we can reset the router back to it |
|
| 113 | - // after we've completed our internal request. |
|
| 114 | - $currentRequest = $this->request->instance()->duplicate(); |
|
| 115 | - |
|
| 116 | - $headers = $this->overrideHeaders($currentRequest->server->getHeaders(), $headers); |
|
| 117 | - |
|
| 118 | - if ($this->disableMiddleware) { |
|
| 119 | - $this->app->instance('middleware.disable', true); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - $response = $this->request($method, $uri, $data, $headers, $content); |
|
| 123 | - |
|
| 124 | - if ($this->disableMiddleware) { |
|
| 125 | - $this->app->instance('middleware.disable', false); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - // Once the request has completed we reset the currentRequest of the router |
|
| 129 | - // to match the original request. |
|
| 130 | - $this->request->instance()->initialize( |
|
| 131 | - $currentRequest->query->all(), |
|
| 132 | - $currentRequest->request->all(), |
|
| 133 | - $currentRequest->attributes->all(), |
|
| 134 | - $currentRequest->cookies->all(), |
|
| 135 | - $currentRequest->files->all(), |
|
| 136 | - $currentRequest->server->all(), |
|
| 137 | - $currentRequest->content |
|
| 138 | - ); |
|
| 139 | - |
|
| 140 | - return $response; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - private function overrideHeaders(array $default, array $headers) |
|
| 144 | - { |
|
| 145 | - $headers = $this->transformHeadersToUppercaseUnderscoreType($headers); |
|
| 146 | - return array_merge($default, $headers); |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - public function enableMiddleware() |
|
| 150 | - { |
|
| 151 | - $this->disableMiddleware = false; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - public function disableMiddleware() |
|
| 155 | - { |
|
| 156 | - $this->disableMiddleware = true; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * @param string $method |
|
| 161 | - * @param string $uri |
|
| 162 | - * @param array $data |
|
| 163 | - * @param array $headers |
|
| 164 | - * @param string $content |
|
| 165 | - * @return \Illuminate\Http\Response |
|
| 166 | - */ |
|
| 167 | - private function request($method, $uri, array $data = [], array $headers = [], $content = null) |
|
| 168 | - { |
|
| 169 | - // Create a new request object for the internal request |
|
| 170 | - $request = $this->createRequest($method, $uri, $data, $headers, $content); |
|
| 171 | - |
|
| 172 | - // Handle the request in the kernel and prepare a response |
|
| 173 | - $response = $this->router->prepareResponse($request, $this->app->handle($request)); |
|
| 174 | - |
|
| 175 | - return $response; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * @param string $method |
|
| 180 | - * @param string $uri |
|
| 181 | - * @param array $data |
|
| 182 | - * @param array $headers |
|
| 183 | - * @param string $content |
|
| 184 | - * @return \Illuminate\Http\Request |
|
| 185 | - */ |
|
| 186 | - private function createRequest($method, $uri, array $data = [], array $headers = [], $content = null) |
|
| 187 | - { |
|
| 188 | - $server = $this->transformHeadersToServerVariables($headers); |
|
| 189 | - |
|
| 190 | - return $this->request->create($uri, $method, $data, [], [], $server, $content); |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - private function transformHeadersToUppercaseUnderscoreType($headers) |
|
| 194 | - { |
|
| 195 | - $transformed = []; |
|
| 196 | - |
|
| 197 | - foreach ($headers as $headerType => $headerValue) { |
|
| 198 | - $headerType = strtoupper(str_replace('-', '_', $headerType)); |
|
| 199 | - |
|
| 200 | - $transformed[$headerType] = $headerValue; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - return $transformed; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * https://github.com/symfony/symfony/issues/5074 |
|
| 208 | - * |
|
| 209 | - * @param array $headers |
|
| 210 | - * @return array |
|
| 211 | - */ |
|
| 212 | - private function transformHeadersToServerVariables($headers) |
|
| 213 | - { |
|
| 214 | - $server = []; |
|
| 215 | - |
|
| 216 | - foreach ($headers as $headerType => $headerValue) { |
|
| 217 | - $headerType = 'HTTP_' . $headerType; |
|
| 218 | - |
|
| 219 | - $server[$headerType] = $headerValue; |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - return $server; |
|
| 223 | - } |
|
| 10 | + private $app; |
|
| 11 | + |
|
| 12 | + private $router; |
|
| 13 | + |
|
| 14 | + private $request; |
|
| 15 | + |
|
| 16 | + private $disableMiddleware = false; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * @param \Illuminate\Foundation\Application $app |
|
| 20 | + * @param \Illuminate\Http\Request $request, |
|
| 21 | + * @param \Illuminate\Routing\Router $router |
|
| 22 | + */ |
|
| 23 | + public function __construct(Application $app, Request $request, LaravelRouter $router) |
|
| 24 | + { |
|
| 25 | + $this->app = $app; |
|
| 26 | + $this->request = $request; |
|
| 27 | + $this->router = $router; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @param string $uri |
|
| 32 | + * @param array $data |
|
| 33 | + * @param array $headers |
|
| 34 | + * @param string $content |
|
| 35 | + * @return \Illuminate\Http\Response |
|
| 36 | + */ |
|
| 37 | + public function get() |
|
| 38 | + { |
|
| 39 | + return $this->quickCall('GET', func_get_args()); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @param string $uri |
|
| 44 | + * @param array $data |
|
| 45 | + * @param array $headers |
|
| 46 | + * @param string $content |
|
| 47 | + * @return \Illuminate\Http\Response |
|
| 48 | + */ |
|
| 49 | + public function post() |
|
| 50 | + { |
|
| 51 | + return $this->quickCall('POST', func_get_args()); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @param string $uri |
|
| 56 | + * @param array $data |
|
| 57 | + * @param array $headers |
|
| 58 | + * @param string $content |
|
| 59 | + * @return \Illuminate\Http\Response |
|
| 60 | + */ |
|
| 61 | + public function put() |
|
| 62 | + { |
|
| 63 | + return $this->quickCall('PUT', func_get_args()); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @param string $uri |
|
| 68 | + * @param array $data |
|
| 69 | + * @param array $headers |
|
| 70 | + * @param string $content |
|
| 71 | + * @return \Illuminate\Http\Response |
|
| 72 | + */ |
|
| 73 | + public function delete() |
|
| 74 | + { |
|
| 75 | + return $this->quickCall('DELETE', func_get_args()); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * @param array $requests An array of requests |
|
| 80 | + * @return array |
|
| 81 | + */ |
|
| 82 | + public function batchRequest(array $requests) |
|
| 83 | + { |
|
| 84 | + foreach ($requests as $i => $request) { |
|
| 85 | + $requests[$i] = call_user_func_array([$this, 'singleRequest'], $request); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + return $requests; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * @param string $method |
|
| 93 | + * @param array $args |
|
| 94 | + * @return \Illuminate\Http\Response |
|
| 95 | + */ |
|
| 96 | + public function quickCall($method, array $args) |
|
| 97 | + { |
|
| 98 | + array_unshift($args, $method); |
|
| 99 | + return call_user_func_array([$this, "singleRequest"], $args); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @param string $method |
|
| 104 | + * @param string $uri |
|
| 105 | + * @param array $data |
|
| 106 | + * @param array $headers |
|
| 107 | + * @param string $content |
|
| 108 | + * @return \Illuminate\Http\Response |
|
| 109 | + */ |
|
| 110 | + public function singleRequest($method, $uri, array $data = [], array $headers = [], $content = null) |
|
| 111 | + { |
|
| 112 | + // Save the current request so we can reset the router back to it |
|
| 113 | + // after we've completed our internal request. |
|
| 114 | + $currentRequest = $this->request->instance()->duplicate(); |
|
| 115 | + |
|
| 116 | + $headers = $this->overrideHeaders($currentRequest->server->getHeaders(), $headers); |
|
| 117 | + |
|
| 118 | + if ($this->disableMiddleware) { |
|
| 119 | + $this->app->instance('middleware.disable', true); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + $response = $this->request($method, $uri, $data, $headers, $content); |
|
| 123 | + |
|
| 124 | + if ($this->disableMiddleware) { |
|
| 125 | + $this->app->instance('middleware.disable', false); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + // Once the request has completed we reset the currentRequest of the router |
|
| 129 | + // to match the original request. |
|
| 130 | + $this->request->instance()->initialize( |
|
| 131 | + $currentRequest->query->all(), |
|
| 132 | + $currentRequest->request->all(), |
|
| 133 | + $currentRequest->attributes->all(), |
|
| 134 | + $currentRequest->cookies->all(), |
|
| 135 | + $currentRequest->files->all(), |
|
| 136 | + $currentRequest->server->all(), |
|
| 137 | + $currentRequest->content |
|
| 138 | + ); |
|
| 139 | + |
|
| 140 | + return $response; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + private function overrideHeaders(array $default, array $headers) |
|
| 144 | + { |
|
| 145 | + $headers = $this->transformHeadersToUppercaseUnderscoreType($headers); |
|
| 146 | + return array_merge($default, $headers); |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + public function enableMiddleware() |
|
| 150 | + { |
|
| 151 | + $this->disableMiddleware = false; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + public function disableMiddleware() |
|
| 155 | + { |
|
| 156 | + $this->disableMiddleware = true; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * @param string $method |
|
| 161 | + * @param string $uri |
|
| 162 | + * @param array $data |
|
| 163 | + * @param array $headers |
|
| 164 | + * @param string $content |
|
| 165 | + * @return \Illuminate\Http\Response |
|
| 166 | + */ |
|
| 167 | + private function request($method, $uri, array $data = [], array $headers = [], $content = null) |
|
| 168 | + { |
|
| 169 | + // Create a new request object for the internal request |
|
| 170 | + $request = $this->createRequest($method, $uri, $data, $headers, $content); |
|
| 171 | + |
|
| 172 | + // Handle the request in the kernel and prepare a response |
|
| 173 | + $response = $this->router->prepareResponse($request, $this->app->handle($request)); |
|
| 174 | + |
|
| 175 | + return $response; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * @param string $method |
|
| 180 | + * @param string $uri |
|
| 181 | + * @param array $data |
|
| 182 | + * @param array $headers |
|
| 183 | + * @param string $content |
|
| 184 | + * @return \Illuminate\Http\Request |
|
| 185 | + */ |
|
| 186 | + private function createRequest($method, $uri, array $data = [], array $headers = [], $content = null) |
|
| 187 | + { |
|
| 188 | + $server = $this->transformHeadersToServerVariables($headers); |
|
| 189 | + |
|
| 190 | + return $this->request->create($uri, $method, $data, [], [], $server, $content); |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + private function transformHeadersToUppercaseUnderscoreType($headers) |
|
| 194 | + { |
|
| 195 | + $transformed = []; |
|
| 196 | + |
|
| 197 | + foreach ($headers as $headerType => $headerValue) { |
|
| 198 | + $headerType = strtoupper(str_replace('-', '_', $headerType)); |
|
| 199 | + |
|
| 200 | + $transformed[$headerType] = $headerValue; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + return $transformed; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * https://github.com/symfony/symfony/issues/5074 |
|
| 208 | + * |
|
| 209 | + * @param array $headers |
|
| 210 | + * @return array |
|
| 211 | + */ |
|
| 212 | + private function transformHeadersToServerVariables($headers) |
|
| 213 | + { |
|
| 214 | + $server = []; |
|
| 215 | + |
|
| 216 | + foreach ($headers as $headerType => $headerValue) { |
|
| 217 | + $headerType = 'HTTP_' . $headerType; |
|
| 218 | + |
|
| 219 | + $server[$headerType] = $headerValue; |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + return $server; |
|
| 223 | + } |
|
| 224 | 224 | } |
@@ -34,7 +34,7 @@ discard block |
||
| 34 | 34 | * |
| 35 | 35 | * @param array $relations |
| 36 | 36 | * @param string $sortBy |
| 37 | - * @param boolean $desc |
|
| 37 | + * @param integer $desc |
|
| 38 | 38 | * @param array $columns |
| 39 | 39 | * @return collection |
| 40 | 40 | */ |
@@ -52,7 +52,7 @@ discard block |
||
| 52 | 52 | * @param integer $perPage |
| 53 | 53 | * @param array $relations |
| 54 | 54 | * @param string $sortBy |
| 55 | - * @param boolean $desc |
|
| 55 | + * @param integer $desc |
|
| 56 | 56 | * @param array $columns |
| 57 | 57 | * @return collection |
| 58 | 58 | */ |
@@ -154,7 +154,7 @@ discard block |
||
| 154 | 154 | * @param integer $perPage |
| 155 | 155 | * @param array $relations |
| 156 | 156 | * @param string $sortBy |
| 157 | - * @param boolean $desc |
|
| 157 | + * @param integer $desc |
|
| 158 | 158 | * @param array $columns |
| 159 | 159 | * @return collection |
| 160 | 160 | */ |
@@ -172,7 +172,7 @@ discard block |
||
| 172 | 172 | * @param integer $perPage |
| 173 | 173 | * @param array $relations |
| 174 | 174 | * @param string $sortBy |
| 175 | - * @param boolean $desc |
|
| 175 | + * @param integer $desc |
|
| 176 | 176 | * @param array $columns |
| 177 | 177 | * @return collection |
| 178 | 178 | */ |
@@ -188,7 +188,7 @@ discard block |
||
| 188 | 188 | * Save the given model to the storage. |
| 189 | 189 | * |
| 190 | 190 | * @param array $data |
| 191 | - * @return mixed |
|
| 191 | + * @return boolean |
|
| 192 | 192 | */ |
| 193 | 193 | public function save(array $data) |
| 194 | 194 | { |
@@ -477,7 +477,7 @@ discard block |
||
| 477 | 477 | * @param array $conditions array of conditions |
| 478 | 478 | * @param array $relations |
| 479 | 479 | * @param string $sortBy |
| 480 | - * @param boolean $desc |
|
| 480 | + * @param integer $desc |
|
| 481 | 481 | * @param array $columns |
| 482 | 482 | * @return collection |
| 483 | 483 | */ |
@@ -509,7 +509,7 @@ discard block |
||
| 509 | 509 | * @param array $conditions array of conditions |
| 510 | 510 | * @param integer $perPage |
| 511 | 511 | * @param string $sortBy |
| 512 | - * @param boolean $desc |
|
| 512 | + * @param integer $desc |
|
| 513 | 513 | * @param array $columns |
| 514 | 514 | * @return collection |
| 515 | 515 | */ |
@@ -6,679 +6,679 @@ |
||
| 6 | 6 | |
| 7 | 7 | abstract class BaseRepository implements BaseRepositoryInterface |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * The model implementation. |
|
| 11 | - * |
|
| 12 | - * @var object |
|
| 13 | - */ |
|
| 14 | - public $model; |
|
| 9 | + /** |
|
| 10 | + * The model implementation. |
|
| 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 records with relations based on the given params. |
|
| 29 | - * |
|
| 30 | - * @param string $relations |
|
| 31 | - * @param array $conditions |
|
| 32 | - * @param integer $perPage |
|
| 33 | - * @param string $sortBy |
|
| 34 | - * @param boolean $desc |
|
| 35 | - * @return collection |
|
| 36 | - */ |
|
| 37 | - public function list($relations = [], $conditions = false, $perPage = 15, $sortBy = 'created_at', $desc = true) |
|
| 38 | - { |
|
| 39 | - unset($conditions['page']); |
|
| 40 | - unset($conditions['perPage']); |
|
| 41 | - unset($conditions['sortBy']); |
|
| 42 | - unset($conditions['sort']); |
|
| 43 | - unset($conditions['page']); |
|
| 44 | - |
|
| 45 | - if (count($conditions)) { |
|
| 46 | - return $this->paginateBy(['and' => $conditions], $perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - return $this->paginate($perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Fetch all records with relations from the storage. |
|
| 54 | - * |
|
| 55 | - * @param array $relations |
|
| 56 | - * @param string $sortBy |
|
| 57 | - * @param boolean $desc |
|
| 58 | - * @param array $columns |
|
| 59 | - * @return collection |
|
| 60 | - */ |
|
| 61 | - public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 62 | - { |
|
| 63 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 64 | - return $this->model->with($relations)->orderBy($sortBy, $sort)->get($columns); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Fetch all records with relations from storage in pages |
|
| 69 | - * that matche the given query. |
|
| 70 | - * |
|
| 71 | - * @param string $query |
|
| 72 | - * @param integer $perPage |
|
| 73 | - * @param array $relations |
|
| 74 | - * @param string $sortBy |
|
| 75 | - * @param boolean $desc |
|
| 76 | - * @param array $columns |
|
| 77 | - * @return collection |
|
| 78 | - */ |
|
| 79 | - public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 80 | - { |
|
| 81 | - $model = $this->model->with($relations); |
|
| 82 | - $conditionColumns = $this->model->searchable; |
|
| 83 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Construct the select conditions for the model. |
|
| 87 | - */ |
|
| 88 | - $model->where(function ($q) use ($query, $conditionColumns, $relations) { |
|
| 89 | - |
|
| 90 | - if (count($conditionColumns)) { |
|
| 91 | - $column = 'LOWER('.array_shift($conditionColumns).')'; |
|
| 92 | - if (Str::contains($column, '->')) { |
|
| 93 | - $column = $this->wrapJsonSelector($column); |
|
| 94 | - } |
|
| 95 | - |
|
| 96 | - /** |
|
| 97 | - * Use the first element in the model columns to construct the first condition. |
|
| 98 | - */ |
|
| 99 | - $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Loop through the rest of the columns to construct or where conditions. |
|
| 104 | - */ |
|
| 105 | - foreach ($conditionColumns as $column) { |
|
| 106 | - $column = 'LOWER('.$column.')'; |
|
| 107 | - if (Str::contains($column, '->')) { |
|
| 108 | - $column = $this->wrapJsonSelector($column); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Loop through the model relations. |
|
| 116 | - */ |
|
| 117 | - foreach ($relations as $relation) { |
|
| 118 | - /** |
|
| 119 | - * Remove the sub relation if exists. |
|
| 120 | - */ |
|
| 121 | - $relation = explode('.', $relation)[0]; |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Try to fetch the relation repository from the core. |
|
| 125 | - */ |
|
| 126 | - if (\Core::$relation()) { |
|
| 127 | - /** |
|
| 128 | - * Construct the relation condition. |
|
| 129 | - */ |
|
| 130 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation) { |
|
| 131 | - |
|
| 132 | - $subModel->where(function ($q) use ($query, $relation) { |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Get columns of the relation. |
|
| 136 | - */ |
|
| 137 | - $subConditionColumns = \Core::$relation()->model->searchable; |
|
| 138 | - |
|
| 139 | - if (count($subConditionColumns)) { |
|
| 140 | - $column = 'LOWER('.array_shift($subConditionColumns).')'; |
|
| 141 | - if (Str::contains($column, '->')) { |
|
| 142 | - $column = $this->wrapJsonSelector($column); |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Use the first element in the relation model columns to construct the first condition. |
|
| 147 | - */ |
|
| 148 | - $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Loop through the rest of the columns to construct or where conditions. |
|
| 153 | - */ |
|
| 154 | - foreach ($subConditionColumns as $subConditionColumn) { |
|
| 155 | - $column = 'LOWER('.$subConditionColumn.')'; |
|
| 156 | - if (Str::contains($column, '->')) { |
|
| 157 | - $column = $this->wrapJsonSelector($column); |
|
| 158 | - } |
|
| 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 records with relations based on the given params. |
|
| 29 | + * |
|
| 30 | + * @param string $relations |
|
| 31 | + * @param array $conditions |
|
| 32 | + * @param integer $perPage |
|
| 33 | + * @param string $sortBy |
|
| 34 | + * @param boolean $desc |
|
| 35 | + * @return collection |
|
| 36 | + */ |
|
| 37 | + public function list($relations = [], $conditions = false, $perPage = 15, $sortBy = 'created_at', $desc = true) |
|
| 38 | + { |
|
| 39 | + unset($conditions['page']); |
|
| 40 | + unset($conditions['perPage']); |
|
| 41 | + unset($conditions['sortBy']); |
|
| 42 | + unset($conditions['sort']); |
|
| 43 | + unset($conditions['page']); |
|
| 44 | + |
|
| 45 | + if (count($conditions)) { |
|
| 46 | + return $this->paginateBy(['and' => $conditions], $perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + return $this->paginate($perPage ?? 15, $relations, $sortBy ?? 'created_at', $desc ?? true); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Fetch all records with relations from the storage. |
|
| 54 | + * |
|
| 55 | + * @param array $relations |
|
| 56 | + * @param string $sortBy |
|
| 57 | + * @param boolean $desc |
|
| 58 | + * @param array $columns |
|
| 59 | + * @return collection |
|
| 60 | + */ |
|
| 61 | + public function all($relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 62 | + { |
|
| 63 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 64 | + return $this->model->with($relations)->orderBy($sortBy, $sort)->get($columns); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Fetch all records with relations from storage in pages |
|
| 69 | + * that matche the given query. |
|
| 70 | + * |
|
| 71 | + * @param string $query |
|
| 72 | + * @param integer $perPage |
|
| 73 | + * @param array $relations |
|
| 74 | + * @param string $sortBy |
|
| 75 | + * @param boolean $desc |
|
| 76 | + * @param array $columns |
|
| 77 | + * @return collection |
|
| 78 | + */ |
|
| 79 | + public function search($query, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 80 | + { |
|
| 81 | + $model = $this->model->with($relations); |
|
| 82 | + $conditionColumns = $this->model->searchable; |
|
| 83 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Construct the select conditions for the model. |
|
| 87 | + */ |
|
| 88 | + $model->where(function ($q) use ($query, $conditionColumns, $relations) { |
|
| 89 | + |
|
| 90 | + if (count($conditionColumns)) { |
|
| 91 | + $column = 'LOWER('.array_shift($conditionColumns).')'; |
|
| 92 | + if (Str::contains($column, '->')) { |
|
| 93 | + $column = $this->wrapJsonSelector($column); |
|
| 94 | + } |
|
| 95 | + |
|
| 96 | + /** |
|
| 97 | + * Use the first element in the model columns to construct the first condition. |
|
| 98 | + */ |
|
| 99 | + $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Loop through the rest of the columns to construct or where conditions. |
|
| 104 | + */ |
|
| 105 | + foreach ($conditionColumns as $column) { |
|
| 106 | + $column = 'LOWER('.$column.')'; |
|
| 107 | + if (Str::contains($column, '->')) { |
|
| 108 | + $column = $this->wrapJsonSelector($column); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Loop through the model relations. |
|
| 116 | + */ |
|
| 117 | + foreach ($relations as $relation) { |
|
| 118 | + /** |
|
| 119 | + * Remove the sub relation if exists. |
|
| 120 | + */ |
|
| 121 | + $relation = explode('.', $relation)[0]; |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Try to fetch the relation repository from the core. |
|
| 125 | + */ |
|
| 126 | + if (\Core::$relation()) { |
|
| 127 | + /** |
|
| 128 | + * Construct the relation condition. |
|
| 129 | + */ |
|
| 130 | + $q->orWhereHas($relation, function ($subModel) use ($query, $relation) { |
|
| 131 | + |
|
| 132 | + $subModel->where(function ($q) use ($query, $relation) { |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Get columns of the relation. |
|
| 136 | + */ |
|
| 137 | + $subConditionColumns = \Core::$relation()->model->searchable; |
|
| 138 | + |
|
| 139 | + if (count($subConditionColumns)) { |
|
| 140 | + $column = 'LOWER('.array_shift($subConditionColumns).')'; |
|
| 141 | + if (Str::contains($column, '->')) { |
|
| 142 | + $column = $this->wrapJsonSelector($column); |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Use the first element in the relation model columns to construct the first condition. |
|
| 147 | + */ |
|
| 148 | + $q->where(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Loop through the rest of the columns to construct or where conditions. |
|
| 153 | + */ |
|
| 154 | + foreach ($subConditionColumns as $subConditionColumn) { |
|
| 155 | + $column = 'LOWER('.$subConditionColumn.')'; |
|
| 156 | + if (Str::contains($column, '->')) { |
|
| 157 | + $column = $this->wrapJsonSelector($column); |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | - $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
| 161 | - } |
|
| 162 | - }); |
|
| 163 | - }); |
|
| 164 | - } |
|
| 165 | - } |
|
| 166 | - }); |
|
| 160 | + $q->orWhere(\DB::raw($column), 'LIKE', '%'.strtolower($query).'%'); |
|
| 161 | + } |
|
| 162 | + }); |
|
| 163 | + }); |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + }); |
|
| 167 | 167 | |
| 168 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 169 | - } |
|
| 168 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - /** |
|
| 172 | - * Fetch all records with relations from storage in pages. |
|
| 173 | - * |
|
| 174 | - * @param integer $perPage |
|
| 175 | - * @param array $relations |
|
| 176 | - * @param string $sortBy |
|
| 177 | - * @param boolean $desc |
|
| 178 | - * @param array $columns |
|
| 179 | - * @return collection |
|
| 180 | - */ |
|
| 181 | - public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 182 | - { |
|
| 183 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 184 | - return $this->model->with($relations)->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - /** |
|
| 188 | - * Fetch all records with relations based on |
|
| 189 | - * the given condition from storage in pages. |
|
| 190 | - * |
|
| 191 | - * @param array $conditions array of conditions |
|
| 192 | - * @param integer $perPage |
|
| 193 | - * @param array $relations |
|
| 194 | - * @param string $sortBy |
|
| 195 | - * @param boolean $desc |
|
| 196 | - * @param array $columns |
|
| 197 | - * @return collection |
|
| 198 | - */ |
|
| 199 | - public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 200 | - { |
|
| 201 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 202 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 203 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 204 | - } |
|
| 171 | + /** |
|
| 172 | + * Fetch all records with relations from storage in pages. |
|
| 173 | + * |
|
| 174 | + * @param integer $perPage |
|
| 175 | + * @param array $relations |
|
| 176 | + * @param string $sortBy |
|
| 177 | + * @param boolean $desc |
|
| 178 | + * @param array $columns |
|
| 179 | + * @return collection |
|
| 180 | + */ |
|
| 181 | + public function paginate($perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 182 | + { |
|
| 183 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 184 | + return $this->model->with($relations)->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + /** |
|
| 188 | + * Fetch all records with relations based on |
|
| 189 | + * the given condition from storage in pages. |
|
| 190 | + * |
|
| 191 | + * @param array $conditions array of conditions |
|
| 192 | + * @param integer $perPage |
|
| 193 | + * @param array $relations |
|
| 194 | + * @param string $sortBy |
|
| 195 | + * @param boolean $desc |
|
| 196 | + * @param array $columns |
|
| 197 | + * @return collection |
|
| 198 | + */ |
|
| 199 | + public function paginateBy($conditions, $perPage = 15, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 200 | + { |
|
| 201 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 202 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 203 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | - /** |
|
| 207 | - * Save the given model to the storage. |
|
| 208 | - * |
|
| 209 | - * @param array $data |
|
| 210 | - * @return mixed |
|
| 211 | - */ |
|
| 212 | - public function save(array $data) |
|
| 213 | - { |
|
| 214 | - \Session::put('locale', 'all'); |
|
| 215 | - $model = false; |
|
| 216 | - $relations = []; |
|
| 217 | - |
|
| 218 | - \DB::transaction(function () use (&$model, $relations, $data) { |
|
| 206 | + /** |
|
| 207 | + * Save the given model to the storage. |
|
| 208 | + * |
|
| 209 | + * @param array $data |
|
| 210 | + * @return mixed |
|
| 211 | + */ |
|
| 212 | + public function save(array $data) |
|
| 213 | + { |
|
| 214 | + \Session::put('locale', 'all'); |
|
| 215 | + $model = false; |
|
| 216 | + $relations = []; |
|
| 217 | + |
|
| 218 | + \DB::transaction(function () use (&$model, $relations, $data) { |
|
| 219 | 219 | |
| 220 | - $model = $this->prepareModel($data); |
|
| 221 | - $relations = $this->prepareRelations($data, $model); |
|
| 222 | - $model = $this->saveModel($model, $relations); |
|
| 223 | - }); |
|
| 220 | + $model = $this->prepareModel($data); |
|
| 221 | + $relations = $this->prepareRelations($data, $model); |
|
| 222 | + $model = $this->saveModel($model, $relations); |
|
| 223 | + }); |
|
| 224 | 224 | |
| 225 | - return $model; |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - /** |
|
| 229 | - * Delete record from the storage based on the given |
|
| 230 | - * condition. |
|
| 231 | - * |
|
| 232 | - * @param var $value condition value |
|
| 233 | - * @param string $attribute condition column name |
|
| 234 | - * @return void |
|
| 235 | - */ |
|
| 236 | - public function delete($value, $attribute = 'id') |
|
| 237 | - { |
|
| 238 | - if ($attribute == 'id') { |
|
| 239 | - \DB::transaction(function () use ($value) { |
|
| 240 | - $model = $this->model->lockForUpdate()->find($value); |
|
| 241 | - if (! $model) { |
|
| 242 | - \ErrorHandler::notFound(class_basename($this->model).' with id : '.$value); |
|
| 243 | - } |
|
| 225 | + return $model; |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + /** |
|
| 229 | + * Delete record from the storage based on the given |
|
| 230 | + * condition. |
|
| 231 | + * |
|
| 232 | + * @param var $value condition value |
|
| 233 | + * @param string $attribute condition column name |
|
| 234 | + * @return void |
|
| 235 | + */ |
|
| 236 | + public function delete($value, $attribute = 'id') |
|
| 237 | + { |
|
| 238 | + if ($attribute == 'id') { |
|
| 239 | + \DB::transaction(function () use ($value) { |
|
| 240 | + $model = $this->model->lockForUpdate()->find($value); |
|
| 241 | + if (! $model) { |
|
| 242 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$value); |
|
| 243 | + } |
|
| 244 | 244 | |
| 245 | - $model->delete(); |
|
| 246 | - }); |
|
| 247 | - } else { |
|
| 248 | - \DB::transaction(function () use ($value, $attribute) { |
|
| 249 | - $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
| 250 | - $model->delete(); |
|
| 251 | - }); |
|
| 252 | - }); |
|
| 253 | - } |
|
| 254 | - } |
|
| 245 | + $model->delete(); |
|
| 246 | + }); |
|
| 247 | + } else { |
|
| 248 | + \DB::transaction(function () use ($value, $attribute) { |
|
| 249 | + $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
| 250 | + $model->delete(); |
|
| 251 | + }); |
|
| 252 | + }); |
|
| 253 | + } |
|
| 254 | + } |
|
| 255 | 255 | |
| 256 | - /** |
|
| 257 | - * Fetch records from the storage based on the given |
|
| 258 | - * id. |
|
| 259 | - * |
|
| 260 | - * @param integer $id |
|
| 261 | - * @param string[] $relations |
|
| 262 | - * @param array $columns |
|
| 263 | - * @return object |
|
| 264 | - */ |
|
| 265 | - public function find($id, $relations = [], $columns = ['*']) |
|
| 266 | - { |
|
| 267 | - return $this->model->with($relations)->find($id, $columns); |
|
| 268 | - } |
|
| 256 | + /** |
|
| 257 | + * Fetch records from the storage based on the given |
|
| 258 | + * id. |
|
| 259 | + * |
|
| 260 | + * @param integer $id |
|
| 261 | + * @param string[] $relations |
|
| 262 | + * @param array $columns |
|
| 263 | + * @return object |
|
| 264 | + */ |
|
| 265 | + public function find($id, $relations = [], $columns = ['*']) |
|
| 266 | + { |
|
| 267 | + return $this->model->with($relations)->find($id, $columns); |
|
| 268 | + } |
|
| 269 | 269 | |
| 270 | - /** |
|
| 271 | - * Fetch records from the storage based on the given |
|
| 272 | - * condition. |
|
| 273 | - * |
|
| 274 | - * @param array $conditions array of conditions |
|
| 275 | - * @param array $relations |
|
| 276 | - * @param string $sortBy |
|
| 277 | - * @param boolean $desc |
|
| 278 | - * @param array $columns |
|
| 279 | - * @return collection |
|
| 280 | - */ |
|
| 281 | - public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 282 | - { |
|
| 283 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 284 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 285 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - /** |
|
| 289 | - * Fetch the first record from the storage based on the given |
|
| 290 | - * condition. |
|
| 291 | - * |
|
| 292 | - * @param array $conditions array of conditions |
|
| 293 | - * @param array $relations |
|
| 294 | - * @param array $columns |
|
| 295 | - * @return object |
|
| 296 | - */ |
|
| 297 | - public function first($conditions, $relations = [], $columns = ['*']) |
|
| 298 | - { |
|
| 299 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 300 | - return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - /** |
|
| 304 | - * Return the deleted models in pages based on the given conditions. |
|
| 305 | - * |
|
| 306 | - * @param array $conditions array of conditions |
|
| 307 | - * @param integer $perPage |
|
| 308 | - * @param string $sortBy |
|
| 309 | - * @param boolean $desc |
|
| 310 | - * @param array $columns |
|
| 311 | - * @return collection |
|
| 312 | - */ |
|
| 313 | - public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 314 | - { |
|
| 315 | - unset($conditions['page']); |
|
| 316 | - unset($conditions['perPage']); |
|
| 317 | - unset($conditions['sortBy']); |
|
| 318 | - unset($conditions['sort']); |
|
| 319 | - $conditions = $this->constructConditions($conditions, $this->model); |
|
| 320 | - $sort = $desc ? 'desc' : 'asc'; |
|
| 321 | - $model = $this->model->onlyTrashed(); |
|
| 322 | - |
|
| 323 | - if (count($conditions['conditionValues'])) { |
|
| 324 | - $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - /** |
|
| 331 | - * Restore the deleted model. |
|
| 332 | - * |
|
| 333 | - * @param integer $id |
|
| 334 | - * @return void |
|
| 335 | - */ |
|
| 336 | - public function restore($id) |
|
| 337 | - { |
|
| 338 | - $model = $this->model->onlyTrashed()->find($id); |
|
| 339 | - |
|
| 340 | - if (! $model) { |
|
| 341 | - \ErrorHandler::notFound(class_basename($this->model).' with id : '.$id); |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - $model->restore(); |
|
| 345 | - } |
|
| 346 | - |
|
| 347 | - /** |
|
| 348 | - * Fill the model with the given data. |
|
| 349 | - * |
|
| 350 | - * @param array $data |
|
| 351 | - * |
|
| 352 | - * @return object |
|
| 353 | - */ |
|
| 354 | - public function prepareModel($data) |
|
| 355 | - { |
|
| 356 | - $modelClass = $this->model; |
|
| 357 | - |
|
| 358 | - /** |
|
| 359 | - * If the id is present in the data then select the model for updating, |
|
| 360 | - * else create new model. |
|
| 361 | - * @var array |
|
| 362 | - */ |
|
| 363 | - $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
| 364 | - if (! $model) { |
|
| 365 | - \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - /** |
|
| 369 | - * Construct the model object with the given data, |
|
| 370 | - * and if there is a relation add it to relations array, |
|
| 371 | - * then save the model. |
|
| 372 | - */ |
|
| 373 | - foreach ($data as $key => $value) { |
|
| 374 | - if (array_search($key, $model->getFillable(), true) !== false) { |
|
| 375 | - /** |
|
| 376 | - * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
| 377 | - */ |
|
| 378 | - $model->$key = $value; |
|
| 379 | - } |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - return $model; |
|
| 383 | - } |
|
| 270 | + /** |
|
| 271 | + * Fetch records from the storage based on the given |
|
| 272 | + * condition. |
|
| 273 | + * |
|
| 274 | + * @param array $conditions array of conditions |
|
| 275 | + * @param array $relations |
|
| 276 | + * @param string $sortBy |
|
| 277 | + * @param boolean $desc |
|
| 278 | + * @param array $columns |
|
| 279 | + * @return collection |
|
| 280 | + */ |
|
| 281 | + public function findBy($conditions, $relations = [], $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 282 | + { |
|
| 283 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 284 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 285 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->orderBy($sortBy, $sort)->get($columns); |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + /** |
|
| 289 | + * Fetch the first record from the storage based on the given |
|
| 290 | + * condition. |
|
| 291 | + * |
|
| 292 | + * @param array $conditions array of conditions |
|
| 293 | + * @param array $relations |
|
| 294 | + * @param array $columns |
|
| 295 | + * @return object |
|
| 296 | + */ |
|
| 297 | + public function first($conditions, $relations = [], $columns = ['*']) |
|
| 298 | + { |
|
| 299 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 300 | + return $this->model->with($relations)->whereRaw($conditions['conditionString'], $conditions['conditionValues'])->first($columns); |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + /** |
|
| 304 | + * Return the deleted models in pages based on the given conditions. |
|
| 305 | + * |
|
| 306 | + * @param array $conditions array of conditions |
|
| 307 | + * @param integer $perPage |
|
| 308 | + * @param string $sortBy |
|
| 309 | + * @param boolean $desc |
|
| 310 | + * @param array $columns |
|
| 311 | + * @return collection |
|
| 312 | + */ |
|
| 313 | + public function deleted($conditions, $perPage = 15, $sortBy = 'created_at', $desc = 1, $columns = ['*']) |
|
| 314 | + { |
|
| 315 | + unset($conditions['page']); |
|
| 316 | + unset($conditions['perPage']); |
|
| 317 | + unset($conditions['sortBy']); |
|
| 318 | + unset($conditions['sort']); |
|
| 319 | + $conditions = $this->constructConditions($conditions, $this->model); |
|
| 320 | + $sort = $desc ? 'desc' : 'asc'; |
|
| 321 | + $model = $this->model->onlyTrashed(); |
|
| 322 | + |
|
| 323 | + if (count($conditions['conditionValues'])) { |
|
| 324 | + $model->whereRaw($conditions['conditionString'], $conditions['conditionValues']); |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + return $model->orderBy($sortBy, $sort)->paginate($perPage, $columns); |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + /** |
|
| 331 | + * Restore the deleted model. |
|
| 332 | + * |
|
| 333 | + * @param integer $id |
|
| 334 | + * @return void |
|
| 335 | + */ |
|
| 336 | + public function restore($id) |
|
| 337 | + { |
|
| 338 | + $model = $this->model->onlyTrashed()->find($id); |
|
| 339 | + |
|
| 340 | + if (! $model) { |
|
| 341 | + \ErrorHandler::notFound(class_basename($this->model).' with id : '.$id); |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + $model->restore(); |
|
| 345 | + } |
|
| 346 | + |
|
| 347 | + /** |
|
| 348 | + * Fill the model with the given data. |
|
| 349 | + * |
|
| 350 | + * @param array $data |
|
| 351 | + * |
|
| 352 | + * @return object |
|
| 353 | + */ |
|
| 354 | + public function prepareModel($data) |
|
| 355 | + { |
|
| 356 | + $modelClass = $this->model; |
|
| 357 | + |
|
| 358 | + /** |
|
| 359 | + * If the id is present in the data then select the model for updating, |
|
| 360 | + * else create new model. |
|
| 361 | + * @var array |
|
| 362 | + */ |
|
| 363 | + $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
|
| 364 | + if (! $model) { |
|
| 365 | + \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + /** |
|
| 369 | + * Construct the model object with the given data, |
|
| 370 | + * and if there is a relation add it to relations array, |
|
| 371 | + * then save the model. |
|
| 372 | + */ |
|
| 373 | + foreach ($data as $key => $value) { |
|
| 374 | + if (array_search($key, $model->getFillable(), true) !== false) { |
|
| 375 | + /** |
|
| 376 | + * If the attribute isn't a relation and prevent attributes not in the fillable. |
|
| 377 | + */ |
|
| 378 | + $model->$key = $value; |
|
| 379 | + } |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + return $model; |
|
| 383 | + } |
|
| 384 | 384 | |
| 385 | - /** |
|
| 386 | - * Prepare related models based on the given data for the given model. |
|
| 387 | - * |
|
| 388 | - * @param array $data |
|
| 389 | - * @param object $model |
|
| 390 | - * |
|
| 391 | - * @return array |
|
| 392 | - */ |
|
| 393 | - public function prepareRelations($data, $model) |
|
| 394 | - { |
|
| 395 | - /** |
|
| 396 | - * Construct the model object with the given data, |
|
| 397 | - * and if there is a relation add it to relations array, |
|
| 398 | - * then save the model. |
|
| 399 | - */ |
|
| 400 | - foreach ($data as $key => $value) { |
|
| 401 | - /** |
|
| 402 | - * If the attribute is a relation. |
|
| 403 | - */ |
|
| 404 | - $relation = \Str::camel($key); |
|
| 405 | - if (method_exists($model, $relation) && \Core::$relation()) { |
|
| 406 | - /** |
|
| 407 | - * Check if the relation is a collection. |
|
| 408 | - */ |
|
| 409 | - if (class_basename($model->$relation) == 'Collection') { |
|
| 410 | - /** |
|
| 411 | - * If the relation has no value then marke the relation data |
|
| 412 | - * related to the model to be deleted. |
|
| 413 | - */ |
|
| 414 | - if (! $value || ! count($value)) { |
|
| 415 | - $relations[$relation] = 'delete'; |
|
| 416 | - } |
|
| 417 | - } |
|
| 418 | - if (is_array($value)) { |
|
| 419 | - /** |
|
| 420 | - * Loop through the relation data. |
|
| 421 | - */ |
|
| 422 | - foreach ($value as $attr => $val) { |
|
| 423 | - /** |
|
| 424 | - * Get the relation model. |
|
| 425 | - */ |
|
| 426 | - $relationBaseModel = \Core::$relation()->model; |
|
| 427 | - |
|
| 428 | - /** |
|
| 429 | - * Check if the relation is a collection. |
|
| 430 | - */ |
|
| 431 | - if (class_basename($model->$relation) == 'Collection') { |
|
| 432 | - /** |
|
| 433 | - * If the id is present in the data then select the relation model for updating, |
|
| 434 | - * else create new model. |
|
| 435 | - */ |
|
| 436 | - $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
| 437 | - |
|
| 438 | - /** |
|
| 439 | - * If model doesn't exists. |
|
| 440 | - */ |
|
| 441 | - if (! $relationModel) { |
|
| 442 | - \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
| 443 | - } |
|
| 444 | - |
|
| 445 | - /** |
|
| 446 | - * Loop through the relation attributes. |
|
| 447 | - */ |
|
| 448 | - foreach ($val as $attr => $val) { |
|
| 449 | - /** |
|
| 450 | - * Prevent the sub relations or attributes not in the fillable. |
|
| 451 | - */ |
|
| 452 | - if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
|
| 453 | - $relationModel->$attr = $val; |
|
| 454 | - } |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - $relations[$relation][] = $relationModel; |
|
| 458 | - } else { |
|
| 459 | - /** |
|
| 460 | - * Prevent the sub relations. |
|
| 461 | - */ |
|
| 462 | - if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
| 463 | - /** |
|
| 464 | - * If the id is present in the data then select the relation model for updating, |
|
| 465 | - * else create new model. |
|
| 466 | - */ |
|
| 467 | - $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
| 468 | - |
|
| 469 | - /** |
|
| 470 | - * If model doesn't exists. |
|
| 471 | - */ |
|
| 472 | - if (! $relationModel) { |
|
| 473 | - \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - foreach ($value as $relationAttribute => $relationValue) { |
|
| 477 | - /** |
|
| 478 | - * Prevent attributes not in the fillable. |
|
| 479 | - */ |
|
| 480 | - if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
| 481 | - $relationModel->$relationAttribute = $relationValue; |
|
| 482 | - } |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - $relations[$relation] = $relationModel; |
|
| 486 | - } |
|
| 487 | - } |
|
| 488 | - } |
|
| 489 | - } |
|
| 490 | - } |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - return $relations; |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - /** |
|
| 497 | - * Save the model with related models. |
|
| 498 | - * |
|
| 499 | - * @param object $model |
|
| 500 | - * @param array $relations |
|
| 501 | - * |
|
| 502 | - * @return object |
|
| 503 | - */ |
|
| 504 | - public function saveModel($model, $relations) |
|
| 505 | - { |
|
| 506 | - |
|
| 507 | - /** |
|
| 508 | - * Loop through the relations array. |
|
| 509 | - */ |
|
| 510 | - foreach ($relations as $key => $value) { |
|
| 511 | - /** |
|
| 512 | - * If the relation is marked for delete then delete it. |
|
| 513 | - */ |
|
| 514 | - if ($value == 'delete' && $model->$key()->count()) { |
|
| 515 | - $model->$key()->delete(); |
|
| 516 | - } elseif (gettype($value) == 'array') { |
|
| 517 | - /** |
|
| 518 | - * Save the model. |
|
| 519 | - */ |
|
| 520 | - $model->save(); |
|
| 521 | - $ids = []; |
|
| 522 | - |
|
| 523 | - /** |
|
| 524 | - * Loop through the relations. |
|
| 525 | - */ |
|
| 526 | - foreach ($value as $val) { |
|
| 527 | - switch (class_basename($model->$key())) { |
|
| 528 | - /** |
|
| 529 | - * If the relation is one to many then update it's foreign key with |
|
| 530 | - * the model id and save it then add its id to ids array to delete all |
|
| 531 | - * relations who's id isn't in the ids array. |
|
| 532 | - */ |
|
| 533 | - case 'HasMany': |
|
| 534 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 535 | - $val->$foreignKeyName = $model->id; |
|
| 536 | - $val->save(); |
|
| 537 | - $ids[] = $val->id; |
|
| 538 | - break; |
|
| 539 | - |
|
| 540 | - /** |
|
| 541 | - * If the relation is many to many then add it's id to the ids array to |
|
| 542 | - * attache these ids to the model. |
|
| 543 | - */ |
|
| 544 | - case 'BelongsToMany': |
|
| 545 | - $val->save(); |
|
| 546 | - $ids[] = $val->id; |
|
| 547 | - break; |
|
| 548 | - } |
|
| 549 | - } |
|
| 550 | - switch (class_basename($model->$key())) { |
|
| 551 | - /** |
|
| 552 | - * If the relation is one to many then delete all |
|
| 553 | - * relations who's id isn't in the ids array. |
|
| 554 | - */ |
|
| 555 | - case 'HasMany': |
|
| 556 | - $model->$key()->whereNotIn('id', $ids)->delete(); |
|
| 557 | - break; |
|
| 558 | - |
|
| 559 | - /** |
|
| 560 | - * If the relation is many to many then |
|
| 561 | - * detach the previous data and attach |
|
| 562 | - * the ids array to the model. |
|
| 563 | - */ |
|
| 564 | - case 'BelongsToMany': |
|
| 565 | - $model->$key()->detach(); |
|
| 566 | - $model->$key()->attach($ids); |
|
| 567 | - break; |
|
| 568 | - } |
|
| 569 | - } else { |
|
| 570 | - switch (class_basename($model->$key())) { |
|
| 571 | - /** |
|
| 572 | - * If the relation is one to one. |
|
| 573 | - */ |
|
| 574 | - case 'HasOne': |
|
| 575 | - /** |
|
| 576 | - * Save the model. |
|
| 577 | - */ |
|
| 578 | - $model->save(); |
|
| 579 | - $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 580 | - $value->$foreignKeyName = $model->id; |
|
| 581 | - $value->save(); |
|
| 582 | - break; |
|
| 583 | - case 'BelongsTo': |
|
| 584 | - /** |
|
| 585 | - * Save the model. |
|
| 586 | - */ |
|
| 587 | - $value->save(); |
|
| 588 | - $model->$key()->associate($value); |
|
| 589 | - break; |
|
| 590 | - } |
|
| 591 | - } |
|
| 592 | - } |
|
| 593 | - |
|
| 594 | - /** |
|
| 595 | - * Save the model. |
|
| 596 | - */ |
|
| 597 | - $model->save(); |
|
| 598 | - |
|
| 599 | - return $model; |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - /** |
|
| 603 | - * Build the conditions recursively for the retrieving methods. |
|
| 604 | - * @param array $conditions |
|
| 605 | - * @return array |
|
| 606 | - */ |
|
| 607 | - protected function constructConditions($conditions, $model) |
|
| 608 | - { |
|
| 609 | - $conditionString = ''; |
|
| 610 | - $conditionValues = []; |
|
| 611 | - foreach ($conditions as $key => $value) { |
|
| 612 | - if (Str::contains($key, '->')) { |
|
| 613 | - $key = $this->wrapJsonSelector($key); |
|
| 614 | - } |
|
| 615 | - |
|
| 616 | - if ($key == 'and') { |
|
| 617 | - $conditions = $this->constructConditions($value, $model); |
|
| 618 | - $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
| 619 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 620 | - } elseif ($key == 'or') { |
|
| 621 | - $conditions = $this->constructConditions($value, $model); |
|
| 622 | - $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
| 623 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 624 | - } else { |
|
| 625 | - if (is_array($value)) { |
|
| 626 | - $operator = $value['op']; |
|
| 627 | - if (strtolower($operator) == 'between') { |
|
| 628 | - $value1 = $value['val1']; |
|
| 629 | - $value2 = $value['val2']; |
|
| 630 | - } else { |
|
| 631 | - $value = Arr::get($value, 'val', ''); |
|
| 632 | - } |
|
| 633 | - } else { |
|
| 634 | - $operator = '='; |
|
| 635 | - } |
|
| 385 | + /** |
|
| 386 | + * Prepare related models based on the given data for the given model. |
|
| 387 | + * |
|
| 388 | + * @param array $data |
|
| 389 | + * @param object $model |
|
| 390 | + * |
|
| 391 | + * @return array |
|
| 392 | + */ |
|
| 393 | + public function prepareRelations($data, $model) |
|
| 394 | + { |
|
| 395 | + /** |
|
| 396 | + * Construct the model object with the given data, |
|
| 397 | + * and if there is a relation add it to relations array, |
|
| 398 | + * then save the model. |
|
| 399 | + */ |
|
| 400 | + foreach ($data as $key => $value) { |
|
| 401 | + /** |
|
| 402 | + * If the attribute is a relation. |
|
| 403 | + */ |
|
| 404 | + $relation = \Str::camel($key); |
|
| 405 | + if (method_exists($model, $relation) && \Core::$relation()) { |
|
| 406 | + /** |
|
| 407 | + * Check if the relation is a collection. |
|
| 408 | + */ |
|
| 409 | + if (class_basename($model->$relation) == 'Collection') { |
|
| 410 | + /** |
|
| 411 | + * If the relation has no value then marke the relation data |
|
| 412 | + * related to the model to be deleted. |
|
| 413 | + */ |
|
| 414 | + if (! $value || ! count($value)) { |
|
| 415 | + $relations[$relation] = 'delete'; |
|
| 416 | + } |
|
| 417 | + } |
|
| 418 | + if (is_array($value)) { |
|
| 419 | + /** |
|
| 420 | + * Loop through the relation data. |
|
| 421 | + */ |
|
| 422 | + foreach ($value as $attr => $val) { |
|
| 423 | + /** |
|
| 424 | + * Get the relation model. |
|
| 425 | + */ |
|
| 426 | + $relationBaseModel = \Core::$relation()->model; |
|
| 427 | + |
|
| 428 | + /** |
|
| 429 | + * Check if the relation is a collection. |
|
| 430 | + */ |
|
| 431 | + if (class_basename($model->$relation) == 'Collection') { |
|
| 432 | + /** |
|
| 433 | + * If the id is present in the data then select the relation model for updating, |
|
| 434 | + * else create new model. |
|
| 435 | + */ |
|
| 436 | + $relationModel = Arr::has($val, 'id') ? $relationBaseModel->lockForUpdate()->find($val['id']) : new $relationBaseModel; |
|
| 437 | + |
|
| 438 | + /** |
|
| 439 | + * If model doesn't exists. |
|
| 440 | + */ |
|
| 441 | + if (! $relationModel) { |
|
| 442 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
|
| 443 | + } |
|
| 444 | + |
|
| 445 | + /** |
|
| 446 | + * Loop through the relation attributes. |
|
| 447 | + */ |
|
| 448 | + foreach ($val as $attr => $val) { |
|
| 449 | + /** |
|
| 450 | + * Prevent the sub relations or attributes not in the fillable. |
|
| 451 | + */ |
|
| 452 | + if (gettype($val) !== 'object' && gettype($val) !== 'array' && array_search($attr, $relationModel->getFillable(), true) !== false) { |
|
| 453 | + $relationModel->$attr = $val; |
|
| 454 | + } |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + $relations[$relation][] = $relationModel; |
|
| 458 | + } else { |
|
| 459 | + /** |
|
| 460 | + * Prevent the sub relations. |
|
| 461 | + */ |
|
| 462 | + if (gettype($val) !== 'object' && gettype($val) !== 'array') { |
|
| 463 | + /** |
|
| 464 | + * If the id is present in the data then select the relation model for updating, |
|
| 465 | + * else create new model. |
|
| 466 | + */ |
|
| 467 | + $relationModel = Arr::has($value, 'id') ? $relationBaseModel->lockForUpdate()->find($value['id']) : new $relationBaseModel; |
|
| 468 | + |
|
| 469 | + /** |
|
| 470 | + * If model doesn't exists. |
|
| 471 | + */ |
|
| 472 | + if (! $relationModel) { |
|
| 473 | + \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + foreach ($value as $relationAttribute => $relationValue) { |
|
| 477 | + /** |
|
| 478 | + * Prevent attributes not in the fillable. |
|
| 479 | + */ |
|
| 480 | + if (array_search($relationAttribute, $relationModel->getFillable(), true) !== false) { |
|
| 481 | + $relationModel->$relationAttribute = $relationValue; |
|
| 482 | + } |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + $relations[$relation] = $relationModel; |
|
| 486 | + } |
|
| 487 | + } |
|
| 488 | + } |
|
| 489 | + } |
|
| 490 | + } |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + return $relations; |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + /** |
|
| 497 | + * Save the model with related models. |
|
| 498 | + * |
|
| 499 | + * @param object $model |
|
| 500 | + * @param array $relations |
|
| 501 | + * |
|
| 502 | + * @return object |
|
| 503 | + */ |
|
| 504 | + public function saveModel($model, $relations) |
|
| 505 | + { |
|
| 506 | + |
|
| 507 | + /** |
|
| 508 | + * Loop through the relations array. |
|
| 509 | + */ |
|
| 510 | + foreach ($relations as $key => $value) { |
|
| 511 | + /** |
|
| 512 | + * If the relation is marked for delete then delete it. |
|
| 513 | + */ |
|
| 514 | + if ($value == 'delete' && $model->$key()->count()) { |
|
| 515 | + $model->$key()->delete(); |
|
| 516 | + } elseif (gettype($value) == 'array') { |
|
| 517 | + /** |
|
| 518 | + * Save the model. |
|
| 519 | + */ |
|
| 520 | + $model->save(); |
|
| 521 | + $ids = []; |
|
| 522 | + |
|
| 523 | + /** |
|
| 524 | + * Loop through the relations. |
|
| 525 | + */ |
|
| 526 | + foreach ($value as $val) { |
|
| 527 | + switch (class_basename($model->$key())) { |
|
| 528 | + /** |
|
| 529 | + * If the relation is one to many then update it's foreign key with |
|
| 530 | + * the model id and save it then add its id to ids array to delete all |
|
| 531 | + * relations who's id isn't in the ids array. |
|
| 532 | + */ |
|
| 533 | + case 'HasMany': |
|
| 534 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 535 | + $val->$foreignKeyName = $model->id; |
|
| 536 | + $val->save(); |
|
| 537 | + $ids[] = $val->id; |
|
| 538 | + break; |
|
| 539 | + |
|
| 540 | + /** |
|
| 541 | + * If the relation is many to many then add it's id to the ids array to |
|
| 542 | + * attache these ids to the model. |
|
| 543 | + */ |
|
| 544 | + case 'BelongsToMany': |
|
| 545 | + $val->save(); |
|
| 546 | + $ids[] = $val->id; |
|
| 547 | + break; |
|
| 548 | + } |
|
| 549 | + } |
|
| 550 | + switch (class_basename($model->$key())) { |
|
| 551 | + /** |
|
| 552 | + * If the relation is one to many then delete all |
|
| 553 | + * relations who's id isn't in the ids array. |
|
| 554 | + */ |
|
| 555 | + case 'HasMany': |
|
| 556 | + $model->$key()->whereNotIn('id', $ids)->delete(); |
|
| 557 | + break; |
|
| 558 | + |
|
| 559 | + /** |
|
| 560 | + * If the relation is many to many then |
|
| 561 | + * detach the previous data and attach |
|
| 562 | + * the ids array to the model. |
|
| 563 | + */ |
|
| 564 | + case 'BelongsToMany': |
|
| 565 | + $model->$key()->detach(); |
|
| 566 | + $model->$key()->attach($ids); |
|
| 567 | + break; |
|
| 568 | + } |
|
| 569 | + } else { |
|
| 570 | + switch (class_basename($model->$key())) { |
|
| 571 | + /** |
|
| 572 | + * If the relation is one to one. |
|
| 573 | + */ |
|
| 574 | + case 'HasOne': |
|
| 575 | + /** |
|
| 576 | + * Save the model. |
|
| 577 | + */ |
|
| 578 | + $model->save(); |
|
| 579 | + $foreignKeyName = $model->$key()->getForeignKeyName(); |
|
| 580 | + $value->$foreignKeyName = $model->id; |
|
| 581 | + $value->save(); |
|
| 582 | + break; |
|
| 583 | + case 'BelongsTo': |
|
| 584 | + /** |
|
| 585 | + * Save the model. |
|
| 586 | + */ |
|
| 587 | + $value->save(); |
|
| 588 | + $model->$key()->associate($value); |
|
| 589 | + break; |
|
| 590 | + } |
|
| 591 | + } |
|
| 592 | + } |
|
| 593 | + |
|
| 594 | + /** |
|
| 595 | + * Save the model. |
|
| 596 | + */ |
|
| 597 | + $model->save(); |
|
| 598 | + |
|
| 599 | + return $model; |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + /** |
|
| 603 | + * Build the conditions recursively for the retrieving methods. |
|
| 604 | + * @param array $conditions |
|
| 605 | + * @return array |
|
| 606 | + */ |
|
| 607 | + protected function constructConditions($conditions, $model) |
|
| 608 | + { |
|
| 609 | + $conditionString = ''; |
|
| 610 | + $conditionValues = []; |
|
| 611 | + foreach ($conditions as $key => $value) { |
|
| 612 | + if (Str::contains($key, '->')) { |
|
| 613 | + $key = $this->wrapJsonSelector($key); |
|
| 614 | + } |
|
| 615 | + |
|
| 616 | + if ($key == 'and') { |
|
| 617 | + $conditions = $this->constructConditions($value, $model); |
|
| 618 | + $conditionString .= str_replace('{op}', 'and', $conditions['conditionString']).' {op} '; |
|
| 619 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 620 | + } elseif ($key == 'or') { |
|
| 621 | + $conditions = $this->constructConditions($value, $model); |
|
| 622 | + $conditionString .= str_replace('{op}', 'or', $conditions['conditionString']).' {op} '; |
|
| 623 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 624 | + } else { |
|
| 625 | + if (is_array($value)) { |
|
| 626 | + $operator = $value['op']; |
|
| 627 | + if (strtolower($operator) == 'between') { |
|
| 628 | + $value1 = $value['val1']; |
|
| 629 | + $value2 = $value['val2']; |
|
| 630 | + } else { |
|
| 631 | + $value = Arr::get($value, 'val', ''); |
|
| 632 | + } |
|
| 633 | + } else { |
|
| 634 | + $operator = '='; |
|
| 635 | + } |
|
| 636 | 636 | |
| 637 | - if (strtolower($operator) == 'between') { |
|
| 638 | - $conditionString .= $key.' >= ? and '; |
|
| 639 | - $conditionValues[] = $value1; |
|
| 640 | - |
|
| 641 | - $conditionString .= $key.' <= ? {op} '; |
|
| 642 | - $conditionValues[] = $value2; |
|
| 643 | - } elseif (strtolower($operator) == 'in') { |
|
| 644 | - $conditionValues = array_merge($conditionValues, $value); |
|
| 645 | - $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
| 646 | - $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
| 647 | - } elseif (strtolower($operator) == 'null') { |
|
| 648 | - $conditionString .= $key.' is null {op} '; |
|
| 649 | - } elseif (strtolower($operator) == 'not null') { |
|
| 650 | - $conditionString .= $key.' is not null {op} '; |
|
| 651 | - } elseif (strtolower($operator) == 'has') { |
|
| 652 | - $sql = $model->withTrashed()->has($key)->toSql(); |
|
| 653 | - $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
| 654 | - $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')').' and '.$conditions['conditionString'].') {op} '; |
|
| 655 | - $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 656 | - } else { |
|
| 657 | - $conditionString .= $key.' '.$operator.' ? {op} '; |
|
| 658 | - $conditionValues[] = $value; |
|
| 659 | - } |
|
| 660 | - } |
|
| 661 | - } |
|
| 662 | - $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
| 663 | - return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
| 664 | - } |
|
| 665 | - |
|
| 666 | - /** |
|
| 667 | - * Wrap the given JSON selector. |
|
| 668 | - * |
|
| 669 | - * @param string $value |
|
| 670 | - * @return string |
|
| 671 | - */ |
|
| 672 | - protected function wrapJsonSelector($value) |
|
| 673 | - { |
|
| 674 | - $removeLast = strpos($value, ')'); |
|
| 675 | - $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
| 676 | - $path = explode('->', $value); |
|
| 677 | - $field = array_shift($path); |
|
| 678 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
| 679 | - return '"'.$part.'"'; |
|
| 680 | - })->implode('.')); |
|
| 637 | + if (strtolower($operator) == 'between') { |
|
| 638 | + $conditionString .= $key.' >= ? and '; |
|
| 639 | + $conditionValues[] = $value1; |
|
| 640 | + |
|
| 641 | + $conditionString .= $key.' <= ? {op} '; |
|
| 642 | + $conditionValues[] = $value2; |
|
| 643 | + } elseif (strtolower($operator) == 'in') { |
|
| 644 | + $conditionValues = array_merge($conditionValues, $value); |
|
| 645 | + $inBindingsString = rtrim(str_repeat('?,', count($value)), ','); |
|
| 646 | + $conditionString .= $key.' in ('.rtrim($inBindingsString, ',').') {op} '; |
|
| 647 | + } elseif (strtolower($operator) == 'null') { |
|
| 648 | + $conditionString .= $key.' is null {op} '; |
|
| 649 | + } elseif (strtolower($operator) == 'not null') { |
|
| 650 | + $conditionString .= $key.' is not null {op} '; |
|
| 651 | + } elseif (strtolower($operator) == 'has') { |
|
| 652 | + $sql = $model->withTrashed()->has($key)->toSql(); |
|
| 653 | + $conditions = $this->constructConditions($value, $model->$key()->getRelated()); |
|
| 654 | + $conditionString .= rtrim(substr($sql, strpos($sql, 'exists')), ')').' and '.$conditions['conditionString'].') {op} '; |
|
| 655 | + $conditionValues = array_merge($conditionValues, $conditions['conditionValues']); |
|
| 656 | + } else { |
|
| 657 | + $conditionString .= $key.' '.$operator.' ? {op} '; |
|
| 658 | + $conditionValues[] = $value; |
|
| 659 | + } |
|
| 660 | + } |
|
| 661 | + } |
|
| 662 | + $conditionString = '('.rtrim($conditionString, '{op} ').')'; |
|
| 663 | + return ['conditionString' => $conditionString, 'conditionValues' => $conditionValues]; |
|
| 664 | + } |
|
| 665 | + |
|
| 666 | + /** |
|
| 667 | + * Wrap the given JSON selector. |
|
| 668 | + * |
|
| 669 | + * @param string $value |
|
| 670 | + * @return string |
|
| 671 | + */ |
|
| 672 | + protected function wrapJsonSelector($value) |
|
| 673 | + { |
|
| 674 | + $removeLast = strpos($value, ')'); |
|
| 675 | + $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
|
| 676 | + $path = explode('->', $value); |
|
| 677 | + $field = array_shift($path); |
|
| 678 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
| 679 | + return '"'.$part.'"'; |
|
| 680 | + })->implode('.')); |
|
| 681 | 681 | |
| 682 | - return $removeLast === false ? $result : $result.')'; |
|
| 683 | - } |
|
| 682 | + return $removeLast === false ? $result : $result.')'; |
|
| 683 | + } |
|
| 684 | 684 | } |
@@ -21,7 +21,7 @@ discard block |
||
| 21 | 21 | */ |
| 22 | 22 | public function __construct($model) |
| 23 | 23 | { |
| 24 | - $this->model = $model; |
|
| 24 | + $this->model = $model; |
|
| 25 | 25 | } |
| 26 | 26 | |
| 27 | 27 | /** |
@@ -85,7 +85,7 @@ discard block |
||
| 85 | 85 | /** |
| 86 | 86 | * Construct the select conditions for the model. |
| 87 | 87 | */ |
| 88 | - $model->where(function ($q) use ($query, $conditionColumns, $relations) { |
|
| 88 | + $model->where(function($q) use ($query, $conditionColumns, $relations) { |
|
| 89 | 89 | |
| 90 | 90 | if (count($conditionColumns)) { |
| 91 | 91 | $column = 'LOWER('.array_shift($conditionColumns).')'; |
@@ -127,9 +127,9 @@ discard block |
||
| 127 | 127 | /** |
| 128 | 128 | * Construct the relation condition. |
| 129 | 129 | */ |
| 130 | - $q->orWhereHas($relation, function ($subModel) use ($query, $relation) { |
|
| 130 | + $q->orWhereHas($relation, function($subModel) use ($query, $relation) { |
|
| 131 | 131 | |
| 132 | - $subModel->where(function ($q) use ($query, $relation) { |
|
| 132 | + $subModel->where(function($q) use ($query, $relation) { |
|
| 133 | 133 | |
| 134 | 134 | /** |
| 135 | 135 | * Get columns of the relation. |
@@ -215,7 +215,7 @@ discard block |
||
| 215 | 215 | $model = false; |
| 216 | 216 | $relations = []; |
| 217 | 217 | |
| 218 | - \DB::transaction(function () use (&$model, $relations, $data) { |
|
| 218 | + \DB::transaction(function() use (&$model, $relations, $data) { |
|
| 219 | 219 | |
| 220 | 220 | $model = $this->prepareModel($data); |
| 221 | 221 | $relations = $this->prepareRelations($data, $model); |
@@ -236,17 +236,17 @@ discard block |
||
| 236 | 236 | public function delete($value, $attribute = 'id') |
| 237 | 237 | { |
| 238 | 238 | if ($attribute == 'id') { |
| 239 | - \DB::transaction(function () use ($value) { |
|
| 239 | + \DB::transaction(function() use ($value) { |
|
| 240 | 240 | $model = $this->model->lockForUpdate()->find($value); |
| 241 | - if (! $model) { |
|
| 241 | + if ( ! $model) { |
|
| 242 | 242 | \ErrorHandler::notFound(class_basename($this->model).' with id : '.$value); |
| 243 | 243 | } |
| 244 | 244 | |
| 245 | 245 | $model->delete(); |
| 246 | 246 | }); |
| 247 | 247 | } else { |
| 248 | - \DB::transaction(function () use ($value, $attribute) { |
|
| 249 | - $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function ($model) { |
|
| 248 | + \DB::transaction(function() use ($value, $attribute) { |
|
| 249 | + $this->model->where($attribute, '=', $value)->lockForUpdate()->get()->each(function($model) { |
|
| 250 | 250 | $model->delete(); |
| 251 | 251 | }); |
| 252 | 252 | }); |
@@ -337,7 +337,7 @@ discard block |
||
| 337 | 337 | { |
| 338 | 338 | $model = $this->model->onlyTrashed()->find($id); |
| 339 | 339 | |
| 340 | - if (! $model) { |
|
| 340 | + if ( ! $model) { |
|
| 341 | 341 | \ErrorHandler::notFound(class_basename($this->model).' with id : '.$id); |
| 342 | 342 | } |
| 343 | 343 | |
@@ -361,7 +361,7 @@ discard block |
||
| 361 | 361 | * @var array |
| 362 | 362 | */ |
| 363 | 363 | $model = Arr::has($data, 'id') ? $modelClass->lockForUpdate()->find($data['id']) : new $modelClass; |
| 364 | - if (! $model) { |
|
| 364 | + if ( ! $model) { |
|
| 365 | 365 | \ErrorHandler::notFound(class_basename($modelClass).' with id : '.$data['id']); |
| 366 | 366 | } |
| 367 | 367 | |
@@ -411,7 +411,7 @@ discard block |
||
| 411 | 411 | * If the relation has no value then marke the relation data |
| 412 | 412 | * related to the model to be deleted. |
| 413 | 413 | */ |
| 414 | - if (! $value || ! count($value)) { |
|
| 414 | + if ( ! $value || ! count($value)) { |
|
| 415 | 415 | $relations[$relation] = 'delete'; |
| 416 | 416 | } |
| 417 | 417 | } |
@@ -438,7 +438,7 @@ discard block |
||
| 438 | 438 | /** |
| 439 | 439 | * If model doesn't exists. |
| 440 | 440 | */ |
| 441 | - if (! $relationModel) { |
|
| 441 | + if ( ! $relationModel) { |
|
| 442 | 442 | \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$val['id']); |
| 443 | 443 | } |
| 444 | 444 | |
@@ -469,7 +469,7 @@ discard block |
||
| 469 | 469 | /** |
| 470 | 470 | * If model doesn't exists. |
| 471 | 471 | */ |
| 472 | - if (! $relationModel) { |
|
| 472 | + if ( ! $relationModel) { |
|
| 473 | 473 | \ErrorHandler::notFound(class_basename($relationBaseModel).' with id : '.$value['id']); |
| 474 | 474 | } |
| 475 | 475 | |
@@ -675,7 +675,7 @@ discard block |
||
| 675 | 675 | $value = $removeLast === false ? $value : substr($value, 0, $removeLast); |
| 676 | 676 | $path = explode('->', $value); |
| 677 | 677 | $field = array_shift($path); |
| 678 | - $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function ($part) { |
|
| 678 | + $result = sprintf('%s->\'$.%s\'', $field, collect($path)->map(function($part) { |
|
| 679 | 679 | return '"'.$part.'"'; |
| 680 | 680 | })->implode('.')); |
| 681 | 681 | |
@@ -34,7 +34,7 @@ |
||
| 34 | 34 | * Init new object. |
| 35 | 35 | * |
| 36 | 36 | * @param mixed $repo |
| 37 | - * @param CoreConfig $config |
|
| 37 | + * @param \App\Modules\Core\Utl\CoreConfig $config |
|
| 38 | 38 | * @param string $modelResource |
| 39 | 39 | * @return void |
| 40 | 40 | */ |
@@ -10,204 +10,204 @@ |
||
| 10 | 10 | |
| 11 | 11 | class BaseApiController extends Controller |
| 12 | 12 | { |
| 13 | - /** |
|
| 14 | - * The config implementation. |
|
| 15 | - * |
|
| 16 | - * @var array |
|
| 17 | - */ |
|
| 18 | - protected $config; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * The relations implementation. |
|
| 22 | - * |
|
| 23 | - * @var array |
|
| 24 | - */ |
|
| 25 | - protected $relations; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * The repo implementation. |
|
| 29 | - * |
|
| 30 | - * @var object |
|
| 31 | - */ |
|
| 32 | - protected $repo; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * Init new object. |
|
| 36 | - * |
|
| 37 | - * @param mixed $repo |
|
| 38 | - * @param CoreConfig $config |
|
| 39 | - * @param string $modelResource |
|
| 40 | - * @return void |
|
| 41 | - */ |
|
| 42 | - public function __construct($repo, $config, $modelResource) |
|
| 43 | - { |
|
| 44 | - $this->repo = new CachingDecorator($repo, \App::make('Illuminate\Contracts\Cache\Repository')); |
|
| 45 | - $this->modelResource = $modelResource; |
|
| 46 | - $this->config = $config->getConfig(); |
|
| 47 | - $this->modelName = explode('\\', get_called_class()); |
|
| 48 | - $this->modelName = lcfirst(str_replace('Controller', '', end($this->modelName))); |
|
| 49 | - $this->validationRules = property_exists($this, 'validationRules') ? $this->validationRules : false; |
|
| 50 | - $this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : []; |
|
| 51 | - $this->skipLoginCheck = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : []; |
|
| 52 | - $route = explode('@', \Route::currentRouteAction())[1]; |
|
| 53 | - |
|
| 54 | - $this->setSessions(); |
|
| 55 | - $this->checkPermission($route); |
|
| 56 | - $this->setRelations($route); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Fetch all records with relations from storage. |
|
| 61 | - * |
|
| 62 | - * @param Request $request |
|
| 63 | - * @return \Illuminate\Http\Response |
|
| 64 | - */ |
|
| 65 | - public function index(Request $request) |
|
| 66 | - { |
|
| 67 | - return $this->modelResource::collection($this->repo->list($this->relations, $request->query(), $request->query('perPage'), $request->query('sortBy'), $request->query('desc'))); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Fetch the single object with relations from storage. |
|
| 72 | - * |
|
| 73 | - * @param integer $id Id of the requested model. |
|
| 74 | - * @return \Illuminate\Http\Response |
|
| 75 | - */ |
|
| 76 | - public function find($id) |
|
| 77 | - { |
|
| 78 | - return new $this->modelResource($this->repo->find($id, $this->relations)); |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Delete by the given id from storage. |
|
| 83 | - * |
|
| 84 | - * @param integer $id Id of the deleted model. |
|
| 85 | - * @return \Illuminate\Http\Response |
|
| 86 | - */ |
|
| 87 | - public function delete($id) |
|
| 88 | - { |
|
| 89 | - return new GeneralResource($this->repo->delete($id)); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Return the deleted models in pages based on the given conditions. |
|
| 94 | - * |
|
| 95 | - * @param Request $request |
|
| 96 | - * @return \Illuminate\Http\Response |
|
| 97 | - */ |
|
| 98 | - public function deleted(Request $request) |
|
| 99 | - { |
|
| 100 | - return $this->modelResource::collection($this->repo->deleted($request->all(), $request->query('perPage'), $request->query('sortBy'), $request->query('desc'))); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Restore the deleted model. |
|
| 105 | - * |
|
| 106 | - * @param integer $id Id of the restored model. |
|
| 107 | - * @return \Illuminate\Http\Response |
|
| 108 | - */ |
|
| 109 | - public function restore($id) |
|
| 110 | - { |
|
| 111 | - return new GeneralResource($this->repo->restore($id)); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Check if the logged in user can do the given permission. |
|
| 116 | - * |
|
| 117 | - * @param string $permission |
|
| 118 | - * @return void |
|
| 119 | - */ |
|
| 120 | - private function checkPermission($permission) |
|
| 121 | - { |
|
| 122 | - \Auth::shouldUse('api'); |
|
| 123 | - $this->middleware('auth:api', ['except' => $this->skipLoginCheck]); |
|
| 13 | + /** |
|
| 14 | + * The config implementation. |
|
| 15 | + * |
|
| 16 | + * @var array |
|
| 17 | + */ |
|
| 18 | + protected $config; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * The relations implementation. |
|
| 22 | + * |
|
| 23 | + * @var array |
|
| 24 | + */ |
|
| 25 | + protected $relations; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * The repo implementation. |
|
| 29 | + * |
|
| 30 | + * @var object |
|
| 31 | + */ |
|
| 32 | + protected $repo; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * Init new object. |
|
| 36 | + * |
|
| 37 | + * @param mixed $repo |
|
| 38 | + * @param CoreConfig $config |
|
| 39 | + * @param string $modelResource |
|
| 40 | + * @return void |
|
| 41 | + */ |
|
| 42 | + public function __construct($repo, $config, $modelResource) |
|
| 43 | + { |
|
| 44 | + $this->repo = new CachingDecorator($repo, \App::make('Illuminate\Contracts\Cache\Repository')); |
|
| 45 | + $this->modelResource = $modelResource; |
|
| 46 | + $this->config = $config->getConfig(); |
|
| 47 | + $this->modelName = explode('\\', get_called_class()); |
|
| 48 | + $this->modelName = lcfirst(str_replace('Controller', '', end($this->modelName))); |
|
| 49 | + $this->validationRules = property_exists($this, 'validationRules') ? $this->validationRules : false; |
|
| 50 | + $this->skipPermissionCheck = property_exists($this, 'skipPermissionCheck') ? $this->skipPermissionCheck : []; |
|
| 51 | + $this->skipLoginCheck = property_exists($this, 'skipLoginCheck') ? $this->skipLoginCheck : []; |
|
| 52 | + $route = explode('@', \Route::currentRouteAction())[1]; |
|
| 53 | + |
|
| 54 | + $this->setSessions(); |
|
| 55 | + $this->checkPermission($route); |
|
| 56 | + $this->setRelations($route); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Fetch all records with relations from storage. |
|
| 61 | + * |
|
| 62 | + * @param Request $request |
|
| 63 | + * @return \Illuminate\Http\Response |
|
| 64 | + */ |
|
| 65 | + public function index(Request $request) |
|
| 66 | + { |
|
| 67 | + return $this->modelResource::collection($this->repo->list($this->relations, $request->query(), $request->query('perPage'), $request->query('sortBy'), $request->query('desc'))); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Fetch the single object with relations from storage. |
|
| 72 | + * |
|
| 73 | + * @param integer $id Id of the requested model. |
|
| 74 | + * @return \Illuminate\Http\Response |
|
| 75 | + */ |
|
| 76 | + public function find($id) |
|
| 77 | + { |
|
| 78 | + return new $this->modelResource($this->repo->find($id, $this->relations)); |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Delete by the given id from storage. |
|
| 83 | + * |
|
| 84 | + * @param integer $id Id of the deleted model. |
|
| 85 | + * @return \Illuminate\Http\Response |
|
| 86 | + */ |
|
| 87 | + public function delete($id) |
|
| 88 | + { |
|
| 89 | + return new GeneralResource($this->repo->delete($id)); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Return the deleted models in pages based on the given conditions. |
|
| 94 | + * |
|
| 95 | + * @param Request $request |
|
| 96 | + * @return \Illuminate\Http\Response |
|
| 97 | + */ |
|
| 98 | + public function deleted(Request $request) |
|
| 99 | + { |
|
| 100 | + return $this->modelResource::collection($this->repo->deleted($request->all(), $request->query('perPage'), $request->query('sortBy'), $request->query('desc'))); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Restore the deleted model. |
|
| 105 | + * |
|
| 106 | + * @param integer $id Id of the restored model. |
|
| 107 | + * @return \Illuminate\Http\Response |
|
| 108 | + */ |
|
| 109 | + public function restore($id) |
|
| 110 | + { |
|
| 111 | + return new GeneralResource($this->repo->restore($id)); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Check if the logged in user can do the given permission. |
|
| 116 | + * |
|
| 117 | + * @param string $permission |
|
| 118 | + * @return void |
|
| 119 | + */ |
|
| 120 | + private function checkPermission($permission) |
|
| 121 | + { |
|
| 122 | + \Auth::shouldUse('api'); |
|
| 123 | + $this->middleware('auth:api', ['except' => $this->skipLoginCheck]); |
|
| 124 | 124 | |
| 125 | - if (! in_array($permission, $this->skipLoginCheck) && $user = \Auth::user()) { |
|
| 126 | - $user = \Auth::user(); |
|
| 127 | - $isPasswordClient = $user->token()->client->password_client; |
|
| 128 | - $this->updateLocaleAndTimezone($user); |
|
| 129 | - |
|
| 130 | - if ($user->blocked) { |
|
| 131 | - \ErrorHandler::userIsBlocked(); |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - if ($isPasswordClient && (in_array($permission, $this->skipPermissionCheck) || \Core::users()->can($permission, $this->modelName))) { |
|
| 135 | - } elseif (! $isPasswordClient && $user->tokenCan($this->modelName.'-'.$permission)) { |
|
| 136 | - } else { |
|
| 137 | - \ErrorHandler::noPermissions(); |
|
| 138 | - } |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Set sessions based on the given headers in the request. |
|
| 144 | - * |
|
| 145 | - * @return void |
|
| 146 | - */ |
|
| 147 | - private function setSessions() |
|
| 148 | - { |
|
| 149 | - \Session::put('time-zone', \Request::header('time-zone') ?: 0); |
|
| 150 | - |
|
| 151 | - $locale = \Request::header('locale'); |
|
| 152 | - switch ($locale) { |
|
| 153 | - case 'en': |
|
| 154 | - \App::setLocale('en'); |
|
| 155 | - \Session::put('locale', 'en'); |
|
| 156 | - break; |
|
| 157 | - |
|
| 158 | - case 'ar': |
|
| 159 | - \App::setLocale('ar'); |
|
| 160 | - \Session::put('locale', 'ar'); |
|
| 161 | - break; |
|
| 162 | - |
|
| 163 | - case 'all': |
|
| 164 | - \App::setLocale('en'); |
|
| 165 | - \Session::put('locale', 'all'); |
|
| 166 | - break; |
|
| 167 | - |
|
| 168 | - default: |
|
| 169 | - \App::setLocale('en'); |
|
| 170 | - \Session::put('locale', 'en'); |
|
| 171 | - break; |
|
| 172 | - } |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * Set relation based on the called api. |
|
| 177 | - * |
|
| 178 | - * @param string $route |
|
| 179 | - * @return void |
|
| 180 | - */ |
|
| 181 | - private function setRelations($route) |
|
| 182 | - { |
|
| 183 | - $route = $route !== 'index' ? $route : 'list'; |
|
| 184 | - $relations = Arr::get($this->config['relations'], $this->modelName, false); |
|
| 185 | - $this->relations = $relations && isset($relations[$route]) ? $relations[$route] : []; |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - /** |
|
| 189 | - * Update the logged in user locale and time zone. |
|
| 190 | - * |
|
| 191 | - * @param object $user |
|
| 192 | - * @return void |
|
| 193 | - */ |
|
| 194 | - private function updateLocaleAndTimezone($user) |
|
| 195 | - { |
|
| 196 | - $update = false; |
|
| 197 | - $locale = \Session::get('locale'); |
|
| 198 | - $timezone = \Session::get('time-zone'); |
|
| 199 | - if ($locale && $locale !== 'all' && $locale !== $user->locale) { |
|
| 200 | - $user->locale = $locale; |
|
| 201 | - $update = true; |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - if ($timezone && $timezone !== $user->timezone) { |
|
| 205 | - $user->timezone = $timezone; |
|
| 206 | - $update = true; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - if ($update) { |
|
| 210 | - $user->save(); |
|
| 211 | - } |
|
| 212 | - } |
|
| 125 | + if (! in_array($permission, $this->skipLoginCheck) && $user = \Auth::user()) { |
|
| 126 | + $user = \Auth::user(); |
|
| 127 | + $isPasswordClient = $user->token()->client->password_client; |
|
| 128 | + $this->updateLocaleAndTimezone($user); |
|
| 129 | + |
|
| 130 | + if ($user->blocked) { |
|
| 131 | + \ErrorHandler::userIsBlocked(); |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + if ($isPasswordClient && (in_array($permission, $this->skipPermissionCheck) || \Core::users()->can($permission, $this->modelName))) { |
|
| 135 | + } elseif (! $isPasswordClient && $user->tokenCan($this->modelName.'-'.$permission)) { |
|
| 136 | + } else { |
|
| 137 | + \ErrorHandler::noPermissions(); |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Set sessions based on the given headers in the request. |
|
| 144 | + * |
|
| 145 | + * @return void |
|
| 146 | + */ |
|
| 147 | + private function setSessions() |
|
| 148 | + { |
|
| 149 | + \Session::put('time-zone', \Request::header('time-zone') ?: 0); |
|
| 150 | + |
|
| 151 | + $locale = \Request::header('locale'); |
|
| 152 | + switch ($locale) { |
|
| 153 | + case 'en': |
|
| 154 | + \App::setLocale('en'); |
|
| 155 | + \Session::put('locale', 'en'); |
|
| 156 | + break; |
|
| 157 | + |
|
| 158 | + case 'ar': |
|
| 159 | + \App::setLocale('ar'); |
|
| 160 | + \Session::put('locale', 'ar'); |
|
| 161 | + break; |
|
| 162 | + |
|
| 163 | + case 'all': |
|
| 164 | + \App::setLocale('en'); |
|
| 165 | + \Session::put('locale', 'all'); |
|
| 166 | + break; |
|
| 167 | + |
|
| 168 | + default: |
|
| 169 | + \App::setLocale('en'); |
|
| 170 | + \Session::put('locale', 'en'); |
|
| 171 | + break; |
|
| 172 | + } |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * Set relation based on the called api. |
|
| 177 | + * |
|
| 178 | + * @param string $route |
|
| 179 | + * @return void |
|
| 180 | + */ |
|
| 181 | + private function setRelations($route) |
|
| 182 | + { |
|
| 183 | + $route = $route !== 'index' ? $route : 'list'; |
|
| 184 | + $relations = Arr::get($this->config['relations'], $this->modelName, false); |
|
| 185 | + $this->relations = $relations && isset($relations[$route]) ? $relations[$route] : []; |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + /** |
|
| 189 | + * Update the logged in user locale and time zone. |
|
| 190 | + * |
|
| 191 | + * @param object $user |
|
| 192 | + * @return void |
|
| 193 | + */ |
|
| 194 | + private function updateLocaleAndTimezone($user) |
|
| 195 | + { |
|
| 196 | + $update = false; |
|
| 197 | + $locale = \Session::get('locale'); |
|
| 198 | + $timezone = \Session::get('time-zone'); |
|
| 199 | + if ($locale && $locale !== 'all' && $locale !== $user->locale) { |
|
| 200 | + $user->locale = $locale; |
|
| 201 | + $update = true; |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + if ($timezone && $timezone !== $user->timezone) { |
|
| 205 | + $user->timezone = $timezone; |
|
| 206 | + $update = true; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + if ($update) { |
|
| 210 | + $user->save(); |
|
| 211 | + } |
|
| 212 | + } |
|
| 213 | 213 | } |
@@ -33,7 +33,7 @@ |
||
| 33 | 33 | * |
| 34 | 34 | * @param Request $request |
| 35 | 35 | * @param string $reportName Name of the requested report |
| 36 | - * @return \Illuminate\Http\Response |
|
| 36 | + * @return \Illuminate\Http\JsonResponse |
|
| 37 | 37 | */ |
| 38 | 38 | public function getReport(Request $request, $reportName) |
| 39 | 39 | { |
@@ -9,34 +9,34 @@ |
||
| 9 | 9 | |
| 10 | 10 | class ReportController extends BaseApiController |
| 11 | 11 | { |
| 12 | - /** |
|
| 13 | - * List of all route actions that the base api controller |
|
| 14 | - * will skip permissions check for them. |
|
| 15 | - * @var array |
|
| 16 | - */ |
|
| 17 | - protected $skipPermissionCheck = ['getReport']; |
|
| 12 | + /** |
|
| 13 | + * List of all route actions that the base api controller |
|
| 14 | + * will skip permissions check for them. |
|
| 15 | + * @var array |
|
| 16 | + */ |
|
| 17 | + protected $skipPermissionCheck = ['getReport']; |
|
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Init new object. |
|
| 21 | - * |
|
| 22 | - * @param ReportRepository $repo |
|
| 23 | - * @param CoreConfig $config |
|
| 24 | - * @return void |
|
| 25 | - */ |
|
| 26 | - public function __construct(ReportRepository $repo, CoreConfig $config) |
|
| 27 | - { |
|
| 28 | - parent::__construct($repo, $config, 'App\Modules\Reporting\Http\Resources\Report'); |
|
| 29 | - } |
|
| 19 | + /** |
|
| 20 | + * Init new object. |
|
| 21 | + * |
|
| 22 | + * @param ReportRepository $repo |
|
| 23 | + * @param CoreConfig $config |
|
| 24 | + * @return void |
|
| 25 | + */ |
|
| 26 | + public function __construct(ReportRepository $repo, CoreConfig $config) |
|
| 27 | + { |
|
| 28 | + parent::__construct($repo, $config, 'App\Modules\Reporting\Http\Resources\Report'); |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Render the given report name with the given conditions. |
|
| 33 | - * |
|
| 34 | - * @param Request $request |
|
| 35 | - * @param string $reportName Name of the requested report |
|
| 36 | - * @return \Illuminate\Http\Response |
|
| 37 | - */ |
|
| 38 | - public function getReport(Request $request, $reportName) |
|
| 39 | - { |
|
| 40 | - return \Response::json($this->repo->getReport($reportName, $request->all(), $request->query('perPage')), 200); |
|
| 41 | - } |
|
| 31 | + /** |
|
| 32 | + * Render the given report name with the given conditions. |
|
| 33 | + * |
|
| 34 | + * @param Request $request |
|
| 35 | + * @param string $reportName Name of the requested report |
|
| 36 | + * @return \Illuminate\Http\Response |
|
| 37 | + */ |
|
| 38 | + public function getReport(Request $request, $reportName) |
|
| 39 | + { |
|
| 40 | + return \Response::json($this->repo->getReport($reportName, $request->all(), $request->query('perPage')), 200); |
|
| 41 | + } |
|
| 42 | 42 | } |
@@ -1,7 +1,5 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -use Illuminate\Http\Request; |
|
| 4 | - |
|
| 5 | 3 | /* |
| 6 | 4 | |-------------------------------------------------------------------------- |
| 7 | 5 | | API Routes |
@@ -1,7 +1,5 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -use Illuminate\Http\Request; |
|
| 4 | - |
|
| 5 | 3 | /* |
| 6 | 4 | |-------------------------------------------------------------------------- |
| 7 | 5 | | API Routes |
@@ -21,7 +21,7 @@ |
||
| 21 | 21 | * Register the given device to the logged in user. |
| 22 | 22 | * |
| 23 | 23 | * @param array $data |
| 24 | - * @return void |
|
| 24 | + * @return boolean |
|
| 25 | 25 | */ |
| 26 | 26 | public function registerDevice($data) |
| 27 | 27 | { |
@@ -8,60 +8,60 @@ |
||
| 8 | 8 | |
| 9 | 9 | class PushNotificationDeviceRepository extends BaseRepository |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * Init new object. |
|
| 13 | - * |
|
| 14 | - * @param PushNotificationDevice $model |
|
| 15 | - * @return void |
|
| 16 | - */ |
|
| 17 | - public function __construct(PushNotificationDevice $model) |
|
| 18 | - { |
|
| 19 | - parent::__construct($model); |
|
| 20 | - } |
|
| 11 | + /** |
|
| 12 | + * Init new object. |
|
| 13 | + * |
|
| 14 | + * @param PushNotificationDevice $model |
|
| 15 | + * @return void |
|
| 16 | + */ |
|
| 17 | + public function __construct(PushNotificationDevice $model) |
|
| 18 | + { |
|
| 19 | + parent::__construct($model); |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Register the given device to the logged in user. |
|
| 24 | - * |
|
| 25 | - * @param array $data |
|
| 26 | - * @return void |
|
| 27 | - */ |
|
| 28 | - public function registerDevice($data) |
|
| 29 | - { |
|
| 30 | - $data['access_token'] = \Auth::user()->token(); |
|
| 31 | - $data['user_id'] = \Auth::id(); |
|
| 32 | - $device = $this->model->where('device_token', $data['device_token'])-> |
|
| 33 | - where('user_id', $data['user_id'])-> |
|
| 34 | - first(); |
|
| 22 | + /** |
|
| 23 | + * Register the given device to the logged in user. |
|
| 24 | + * |
|
| 25 | + * @param array $data |
|
| 26 | + * @return void |
|
| 27 | + */ |
|
| 28 | + public function registerDevice($data) |
|
| 29 | + { |
|
| 30 | + $data['access_token'] = \Auth::user()->token(); |
|
| 31 | + $data['user_id'] = \Auth::id(); |
|
| 32 | + $device = $this->model->where('device_token', $data['device_token'])-> |
|
| 33 | + where('user_id', $data['user_id'])-> |
|
| 34 | + first(); |
|
| 35 | 35 | |
| 36 | - if ($device) { |
|
| 37 | - $data['id'] = $device->id; |
|
| 38 | - } |
|
| 36 | + if ($device) { |
|
| 37 | + $data['id'] = $device->id; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - return $this->save($data); |
|
| 41 | - } |
|
| 40 | + return $this->save($data); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Generate the given message data. |
|
| 45 | - * |
|
| 46 | - * @param string $title |
|
| 47 | - * @param string $message |
|
| 48 | - * @param array $data |
|
| 49 | - * @return void |
|
| 50 | - */ |
|
| 51 | - public function generateMessageData($title, $message, $data = []) |
|
| 52 | - { |
|
| 53 | - $optionBuilder = new OptionsBuilder(); |
|
| 54 | - $notificationBuilder = new PayloadNotificationBuilder($title); |
|
| 55 | - $dataBuilder = new PayloadDataBuilder(); |
|
| 43 | + /** |
|
| 44 | + * Generate the given message data. |
|
| 45 | + * |
|
| 46 | + * @param string $title |
|
| 47 | + * @param string $message |
|
| 48 | + * @param array $data |
|
| 49 | + * @return void |
|
| 50 | + */ |
|
| 51 | + public function generateMessageData($title, $message, $data = []) |
|
| 52 | + { |
|
| 53 | + $optionBuilder = new OptionsBuilder(); |
|
| 54 | + $notificationBuilder = new PayloadNotificationBuilder($title); |
|
| 55 | + $dataBuilder = new PayloadDataBuilder(); |
|
| 56 | 56 | |
| 57 | - $optionBuilder->setTimeToLive(60 * 20); |
|
| 58 | - $notificationBuilder->setBody($message); |
|
| 59 | - $dataBuilder->addData($data); |
|
| 57 | + $optionBuilder->setTimeToLive(60 * 20); |
|
| 58 | + $notificationBuilder->setBody($message); |
|
| 59 | + $dataBuilder->addData($data); |
|
| 60 | 60 | |
| 61 | - $options = $optionBuilder->build(); |
|
| 62 | - $notification = $notificationBuilder->build(); |
|
| 63 | - $data = $dataBuilder->build(); |
|
| 61 | + $options = $optionBuilder->build(); |
|
| 62 | + $notification = $notificationBuilder->build(); |
|
| 63 | + $data = $dataBuilder->build(); |
|
| 64 | 64 | |
| 65 | - return compact('options', 'notification', 'data'); |
|
| 66 | - } |
|
| 65 | + return compact('options', 'notification', 'data'); |
|
| 66 | + } |
|
| 67 | 67 | } |
@@ -1,7 +1,5 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -use Illuminate\Http\Request; |
|
| 4 | - |
|
| 5 | 3 | /* |
| 6 | 4 | |-------------------------------------------------------------------------- |
| 7 | 5 | | API Routes |