Skip to content

Commit

Permalink
✨ more laravel support
Browse files Browse the repository at this point in the history
  • Loading branch information
acidjazz committed Nov 17, 2019
1 parent 7fc6713 commit ae314b3
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
68 changes: 40 additions & 28 deletions src/MetApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ abstract class MetApiController extends BaseController

protected $request;
protected $benchmark;
protected $status;

protected $query = [
'options' => [],
Expand All @@ -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;
Expand All @@ -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);
Expand All @@ -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;

}

Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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;
}
Expand Down

0 comments on commit ae314b3

Please sign in to comment.