From ae314b333d0acbb347f10549b59585590a2ce106 Mon Sep 17 00:00:00 2001 From: kevin olson Date: Sat, 16 Nov 2019 21:03:00 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20more=20laravel=20support?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 2 +- src/MetApiController.php | 68 +++++++++++++++++++++++----------------- 2 files changed, 41 insertions(+), 29 deletions(-) diff --git a/composer.json b/composer.json index cfc680e..85f961d 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ } ], "require": { - "illuminate/support": "5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.* || 5.6.* || 5.7.* || 5.8.* || 6.0.*", + "illuminate/support": "5.* || 6.*", "jasongrimes/paginator": "^1.0", "php": "~7.0" }, diff --git a/src/MetApiController.php b/src/MetApiController.php index 87a923b..b0c958b 100644 --- a/src/MetApiController.php +++ b/src/MetApiController.php @@ -18,6 +18,7 @@ abstract class MetApiController extends BaseController protected $request; protected $benchmark; + protected $status; protected $query = [ 'options' => [], @@ -29,6 +30,14 @@ abstract class MetApiController extends BaseController protected $meta = []; protected $compiled = false; + // Wether or not we want to return the paginate items or the entire structure + protected $paginateItems = false; + + public function setPaginateItems($boolean) + { + $this->paginateItems = $boolean; + } + public function __construct(Request $request) { $this->benchmark = microtime(true); $this->request = $request; @@ -50,7 +59,7 @@ protected function addMeta($name, $value) { $this->meta[$name] = $value; } - protected function paginate($collection,$perpage=15,$maxPages=7) { + protected function paginate($collection,$perpage=50,$maxPages=10) { if (is_string($collection)) { $collection = $collection::paginate($perpage); @@ -77,14 +86,16 @@ protected function paginate($collection,$perpage=15,$maxPages=7) { 'per_page' => $collection->perPage(), 'current_page' => $collection->currentPage(), 'last_page' => $collection->lastPage(), - 'next_page_url' => $collection->nextPageUrl(), - 'prev_page_url' => $collection->previousPageUrl(), 'first_item' => $paginator->getCurrentPageFirstItem(), 'last_item' => $paginator->getCurrentPageLastItem(), 'pages' => $pages, ]); - return $collection->items(); + if ($this->paginateItems) { + return $collection->items(); + } + + return $collection; } @@ -123,45 +134,45 @@ protected function getMeta() { return $this->meta; } - protected function addError($type,$message,$file=null,$line=null) + protected function addError($title, $detail, $status=400, $source=false ) { - $error = [ - 'type' => $type, - 'message' => $message, - ]; + $error = [ 'status' => $status, 'title' => $title, ]; - if ($file !== null) { - $error['file'] = $file; + if ($source) { + $error['source'] = $source; } - if ($line !== null) { - $error['line'] = $line; - } + $error['detail'] = $detail; - $this->errors[$type][] = $message; + $this->errors[] = $error; return $this; } /** - * render errors - * returns $this->errors w/ no view, transformer and an error code of 500 + * Render errors + * + * @param string $key - shortkey or title of error + * @param string|array $replace - shortkey params + * @param integer $status - HTTP status code to pass + * @param source array - error source + * @return \Illuminate\Http\Response */ + protected function error($key,$replace=null, $status=400, $source=false) { - protected function error($key='unknown',$replace=[]) { - - if ($key !== 'unknown' || count($this->errors) < 1) { - $this->addError($key, __($key,$replace)); + if (__($key, is_array($replace) ? $replace : []) !== $key) { + $this->addError($key, __($key, is_array($replace) ? $replace : []), $status, $source); + } else { + $this->addError($key, $replace, $status, $source); } - - return $this->render(['errors' => $this->errors], 500); + return $this->render(['errors' => $this->errors], $status); } /** * render errors and abort */ protected function abort() { - $this->render(['errors' => $this->errors], 500, true); + $this->render(['errors' => $this->errors], 400, true); } /** @@ -188,11 +199,12 @@ protected function success($message='Successful',$replace=[],$data=[]) */ protected function render($data=false,$code=200,$abort=false) { - if ($code === 403 || count($this->errors) > 0) { - $response = $data; - $code = 403; + if (in_array($code, [400, 403, 500]) || count($this->errors) > 0) { + $response['status'] = 'error'; + $response = array_merge($response, $data); } else { - $response = $this->getMeta(); + $response['status'] = 'success'; + $response = array_merge($response, $this->getMeta()); $response['query'] = $this->query; $response['data'] = $data; }