Skip to content

Commit

Permalink
➕ overhaul of spacing, editorconfig, helpers included
Browse files Browse the repository at this point in the history
  • Loading branch information
acidjazz committed Feb 11, 2019
1 parent d48be31 commit 464458e
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 148 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
302 changes: 154 additions & 148 deletions src/MetApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

}
}

}

0 comments on commit 464458e

Please sign in to comment.