Skip to content

Commit

Permalink
♻️ refactor and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
acidjazz committed May 16, 2020
1 parent 815f299 commit 5bbf190
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 30 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"require": {
"illuminate/support": "5.* || 6.* || 7.*",
"jasongrimes/paginator": "^1.0",
"php": "~7.0"
"php": "^7.4",
"ext-json": "*"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 2 additions & 2 deletions resources/views/json.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
<head>
<title>{{ config('app.name') }}</title>
<link rel="icon" type="image/x-icon" href="https://png.pngtree.com/svg/20170401/b49f30849c.png" />
<link rel="stylesheet", href="https://cdnjs.cloudflare.com/ajax/libs/jsoneditor/6.3.0/jsoneditor.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jsoneditor@8.6.4/dist/jsoneditor.css"/>
<link href="https://fonts.googleapis.com/css?family=Roboto+Mono&display=swap" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jsoneditor/6.3.0/jsoneditor.min.js" integrity="sha256-o2/L37SBn4ufKxtZAItcvBdidVsP170IKNcqfVd3ZF4=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/jsoneditor@8.6.4/dist/jsoneditor.js"></script>
<style lang="text/css">
body, html {
margin: 0;
Expand Down
11 changes: 7 additions & 4 deletions src/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

class Facade extends \Illuminate\Support\Facades\Facade
{
protected static function getFacadeAccessor()
{
return MetApiController::class;
}
/**
* @return string
*/
protected static function getFacadeAccessor()
{
return MetApiController::class;
}
}
34 changes: 17 additions & 17 deletions src/MetApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace acidjazz\metapi;

use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\Request;
use Illuminate\{Routing\Controller as BaseController,
Foundation\Bus\DispatchesJobs,
Foundation\Validation\ValidatesRequests,
Foundation\Auth\Access\AuthorizesRequests,
Http\Request
};

use Validator;
Expand All @@ -28,12 +28,14 @@ abstract class MetApiController extends BaseController
protected $errors = [];

protected $meta = [];
protected $compiled = false;

// Whether or not we want to return the paginate items or the entire structure
protected $paginateItems = true;

public function setPaginateItems($boolean)
/**
* @param $boolean
*/
public function setPaginateItems($boolean): void
{
$this->paginateItems = $boolean;
}
Expand All @@ -49,10 +51,9 @@ public function __construct(Request $request)
*
* @param string $name
* @param string $type
* @param boolean $default
* @return MetApiController
*/
protected function option($name, $type, $default = false)
protected function option($name, $type)
{
$this->query['options'][$name] = $type;
return $this;
Expand All @@ -76,7 +77,7 @@ protected function options($options)
* add metadata
*
* @param string $name
* @param string $value
* @param mixed $value
*
* @return void
*/
Expand Down Expand Up @@ -133,7 +134,7 @@ protected function paginate($collection, $perpage = 50, $maxPages = 10)
}

/**
* verify through laravels Validator
* Verify through the Validator
*
* @param boolean $abort
* @return array|bool|void
Expand All @@ -144,7 +145,6 @@ protected function verify($abort = true)
$validate = Validator::make($this->request->all(), $this->query['options']);

if ($validate->fails()) {

foreach ($validate->errors()->toArray() as $key => $value) {
foreach ($value as $error) {
$this->addError($key, $error);
Expand All @@ -161,7 +161,6 @@ protected function verify($abort = true)

foreach ($this->request->all() as $key => $value) {
if (isset($this->query['options'][$key])) {

if ($this->isFile($value)) {
$value = (array)$value;
}
Expand Down Expand Up @@ -192,8 +191,10 @@ protected function verify($abort = true)
*/
private function isFile($value)
{
return is_object($value) && in_array(get_class($value),
['Illuminate\Http\UploadedFile', 'Illuminate\Http\Testing\File']);
return is_object($value) && in_array(
get_class($value),
['Illuminate\Http\UploadedFile', 'Illuminate\Http\Testing\File']
);
}

/**
Expand Down Expand Up @@ -238,7 +239,7 @@ protected function addError($title, $detail, $status = 400, $source = false)
* @param string|array $replace - shortkey params
* @param integer $status - HTTP status code to pass
* @param bool $source
* @return \Illuminate\Http\Response
* @return mixed
*/
protected function error($key, $replace = null, $status = 400, $source = false)
{
Expand All @@ -262,7 +263,7 @@ protected function abort()
* Render success
* @param string
* @param array
* @return \Illuminate\Http\Response
* @return mixed
*/
protected function success($message = 'Successful', $replace = [], $data = [])
{
Expand All @@ -277,7 +278,7 @@ protected function success($message = 'Successful', $replace = [], $data = [])
/**
* Final output
* @param mixed $data data to be sent
* @param integer $code response code, defaulting to 200
* @param int $code response code, defaulting to 200
* @param bool $abort
* @return mixed
*/
Expand All @@ -299,8 +300,7 @@ protected function render($data = false, $code = 200, $abort = false)
$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 &&
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, true)]), $code);
} else {
Expand Down
10 changes: 4 additions & 6 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

class ServiceProvider extends \Illuminate\Support\ServiceProvider
{

public function boot()
{
$this->loadViewsFrom(__DIR__.'/../resources/views/', 'metapi');
}

public function boot()
{
$this->loadViewsFrom(__DIR__.'/../resources/views/', 'metapi');
}
}

0 comments on commit 5bbf190

Please sign in to comment.