From 464458e17e9bfbba966945eae4e4b5b3a875e979 Mon Sep 17 00:00:00 2001 From: kevin olson Date: Sun, 10 Feb 2019 20:07:59 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9E=95=20overhaul=20of=20spacing,=20editorco?= =?UTF-8?q?nfig,=20helpers=20included?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .editorconfig | 15 ++ src/MetApiController.php | 302 ++++++++++++++++++++------------------- 2 files changed, 169 insertions(+), 148 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..d82865e --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +indent_size = 2 +indent_style = space +trim_trailing_whitespace = true + +[*.php] +indent_size = 4 + +[*.md] +trim_trailing_whitespace = false diff --git a/src/MetApiController.php b/src/MetApiController.php index 65a92a2..e86b115 100644 --- a/src/MetApiController.php +++ b/src/MetApiController.php @@ -3,207 +3,213 @@ namespace acidjazz\metapi; use Illuminate\Routing\Controller as BaseController; + +use Illuminate\Foundation\Bus\DispatchesJobs; +use Illuminate\Foundation\Validation\ValidatesRequests; +use Illuminate\Foundation\Auth\Access\AuthorizesRequests; + use Illuminate\Http\Request; use Validator; use JasonGrimes\Paginator; abstract class MetApiController extends BaseController { + use AuthorizesRequests, DispatchesJobs, ValidatesRequests; - protected $request; - protected $benchmark; + protected $request; + protected $benchmark; - protected $query = [ - 'options' => [], - 'params' => [], - ]; + protected $query = [ + 'options' => [], + 'params' => [], + ]; - protected $errors = []; + protected $errors = []; - protected $meta = []; - protected $compiled = false; + protected $meta = []; + protected $compiled = false; - public function __construct(Request $request) { - $this->benchmark = microtime(true); - $this->request = $request; - } + public function __construct(Request $request) { + $this->benchmark = microtime(true); + $this->request = $request; + } - protected function option($name, $type, $default=false) { - $this->query['options'][$name] = $type; - return $this; - } + protected function option($name, $type, $default=false) { + $this->query['options'][$name] = $type; + return $this; + } + + protected function options($options) { + foreach ($options as $key=>$value) { + $this->option($key, $value); + } + return $this; + } - protected function options($options) { - foreach ($options as $key=>$value) { - $this->option($key, $value); + protected function addMeta($name, $value) { + $this->meta[$name] = $value; } - return $this; - } - protected function addMeta($name, $value) { - $this->meta[$name] = $value; - } + protected function paginate($collection,$perpage=15) { + + $collection = $collection->paginate($perpage); - protected function paginate($collection,$perpage=15) { + $paginator = new Paginator( + $collection->total(), + $collection->perPage(), + $collection->currentPage() + ); - $collection = $collection->paginate($perpage); + $paginator->setMaxPagesToShow(7); - $paginator = new Paginator( - $collection->total(), - $collection->perPage(), - $collection->currentPage() - ); + $pages = []; + + foreach ($paginator->getPages() as $page) { + $pages[] = $page['num']; + } - $paginator->setMaxPagesToShow(7); + $this->addMeta('paginate', [ + 'total' => $collection->total(), + 'per_page' => $collection->perPage(), + 'current_page' => $collection->currentPage(), + 'last_page' => $collection->lastPage(), + 'next_page_url' => $collection->nextPageUrl(), + 'prev_page_url' => $collection->previousPageUrl(), + 'pages' => $pages, + ]); - $pages = []; + return $collection->items(); - foreach ($paginator->getPages() as $page) { - $pages[] = $page['num']; } - $this->addMeta('paginate', [ - 'total' => $collection->total(), - 'per_page' => $collection->perPage(), - 'current_page' => $collection->currentPage(), - 'last_page' => $collection->lastPage(), - 'next_page_url' => $collection->nextPageUrl(), - 'prev_page_url' => $collection->previousPageUrl(), - 'pages' => $pages, - ]); + protected function verify($abort=true) { - return $collection->items(); + $validate = Validator::make($this->request->all(), $this->query['options']); - } + if ($validate->fails()) { - protected function verify($abort=true) { + foreach ($validate->errors()->toArray() as $key=>$value) { + foreach($value as $error) { + $this->addError($key, $error); + } + } - $validate = Validator::make($this->request->all(), $this->query['options']); + if ($abort) { + return $this->abort(); + } else { + return false; + } - if ($validate->fails()) { + } - foreach ($validate->errors()->toArray() as $key=>$value) { - foreach($value as $error) { - $this->addError($key, $error); + foreach ($this->request->all() as $key=>$value) { + if (isset($this->query['options'][$key])) { + $this->query['params'][$key] = $value; + } } - } - if ($abort) { - return $this->abort(); - } else { - return false; - } + return $this->query; } - foreach ($this->request->all() as $key=>$value) { - if (isset($this->query['options'][$key])) { - $this->query['params'][$key] = $value; - } + protected function getMeta() { + $this->meta['benchmark'] = microtime(true)-$this->benchmark; + return $this->meta; } - return $this->query; - - } + protected function addError($type,$message,$file=null,$line=null) + { + $error = [ + 'type' => $type, + 'message' => $message, + ]; - protected function getMeta() { - $this->meta['benchmark'] = microtime(true)-$this->benchmark; - return $this->meta; - } + if ($file !== null) { + $error['file'] = $file; + } - protected function addError($type,$message,$file=null,$line=null) - { - $error = [ - 'type' => $type, - 'message' => $message, - ]; + if ($line !== null) { + $error['line'] = $line; + } - if ($file !== null) { - $error['file'] = $file; - } + $this->errors[$type][] = $message; - if ($line !== null) { - $error['line'] = $line; + return $this; } - $this->errors[$type][] = $message; + /** + * render errors + * returns $this->errors w/ no view, transformer and an error code of 500 + */ - return $this; - } + protected function error($key='unknown',$replace=[]) { - /** - * render errors - * returns $this->errors w/ no view, transformer and an error code of 500 - */ - - protected function error($key='unknown',$replace=[]) { + if ($key !== 'unknown' || count($this->errors) < 1) { + $this->addError($key, __($key,$replace)); + } - if ($key !== 'unknown' || count($this->errors) < 1) { - $this->addError($key, __($key,$replace)); + return $this->render(['errors' => $this->errors], 500); } - return $this->render(['errors' => $this->errors], 500); - } - - /** - * render errors and abort - */ - protected function abort() { - $this->render(['errors' => $this->errors], 500, true); - } - - /** - * Render success - * @param String - * @param Array - * @return \Illuminate\Http\Response - */ - protected function success($message='Successful',$replace=[],$data=[]) - { - return $this->render([ - 'success' => true, - 'type' => 'success', - 'message' => __($message,$replace), - 'data' => $data, - ], 200, true); - } - - /** - * Final output - * @param mixed $data data to be sent - * @param integer $code response code, defaulting to 200 - * @return \Illuminate\Http\Response - */ - protected function render($data=false,$code=200,$abort=false) { - - if ($code === 403 || count($this->errors) > 0) { - $response = $data; - $code = 403; - } else { - $response = $this->getMeta(); - $response['query'] = $this->query; - $response['data'] = $data; + /** + * render errors and abort + */ + protected function abort() { + $this->render(['errors' => $this->errors], 500, true); } - if ($this->request->query('callback') !== null) { - $json = json_encode($response, JSON_PRETTY_PRINT); - $response = ['callback' => $this->request->query('callback'),'json' => $json]; - $responsable = response(view('metapi::jsonp', $response), 200)->header('Content-type', 'text/javascript'); - } else if ( - strpos($this->request->header('accept'),'text/html') !== false && - config('app.debug') === true && $this->request->query('json') !== 'true') + /** + * Render success + * @param String + * @param Array + * @return \Illuminate\Http\Response + */ + protected function success($message='Successful',$replace=[],$data=[]) { - $responsable = response(view('metapi::json', ['json' => json_encode($response, JSON_PRETTY_PRINT)]), $code); - } else { - $responsable = response()->json($response, $code, [], JSON_PRETTY_PRINT); + return $this->render([ + 'success' => true, + 'type' => 'success', + 'message' => __($message,$replace), + 'data' => $data, + ], 200, true); } - if ($abort) { - return abort($responsable); - } + /** + * Final output + * @param mixed $data data to be sent + * @param integer $code response code, defaulting to 200 + * @return \Illuminate\Http\Response + */ + protected function render($data=false,$code=200,$abort=false) { + + if ($code === 403 || count($this->errors) > 0) { + $response = $data; + $code = 403; + } else { + $response = $this->getMeta(); + $response['query'] = $this->query; + $response['data'] = $data; + } + + if ($this->request->query('callback') !== null) { + $json = json_encode($response, JSON_PRETTY_PRINT); + $response = ['callback' => $this->request->query('callback'),'json' => $json]; + $responsable = response(view('metapi::jsonp', $response), 200)->header('Content-type', 'text/javascript'); + } else if ( + strpos($this->request->header('accept'),'text/html') !== false && + config('app.debug') === true && $this->request->query('json') !== 'true') + { + $responsable = response(view('metapi::json', ['json' => json_encode($response, JSON_PRETTY_PRINT)]), $code); + } else { + $responsable = response()->json($response, $code, [], JSON_PRETTY_PRINT); + } - return $responsable; + if ($abort) { + return abort($responsable); + } + + return $responsable; - } + } }