JSON response provider

This commit is contained in:
Miguel Nogueira 2021-03-29 23:47:55 +01:00
parent 14a8e9e9d5
commit 6d94263ede
Signed by: miguel456
GPG Key ID: 2CF61B825316C6A0
6 changed files with 168 additions and 0 deletions

View File

@ -146,6 +146,7 @@
<excludeFolder url="file://$MODULE_DIR$/vendor/vlucas/phpdotenv" /> <excludeFolder url="file://$MODULE_DIR$/vendor/vlucas/phpdotenv" />
<excludeFolder url="file://$MODULE_DIR$/vendor/voku/portable-ascii" /> <excludeFolder url="file://$MODULE_DIR$/vendor/voku/portable-ascii" />
<excludeFolder url="file://$MODULE_DIR$/vendor/webmozart/assert" /> <excludeFolder url="file://$MODULE_DIR$/vendor/webmozart/assert" />
<excludeFolder url="file://$MODULE_DIR$/vendor/laravel/sanctum" />
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />

View File

@ -153,6 +153,7 @@
<path value="$PROJECT_DIR$/vendor/awssat/discord-notification-channel" /> <path value="$PROJECT_DIR$/vendor/awssat/discord-notification-channel" />
<path value="$PROJECT_DIR$/vendor/berkayk/onesignal-laravel" /> <path value="$PROJECT_DIR$/vendor/berkayk/onesignal-laravel" />
<path value="$PROJECT_DIR$/vendor/symfony/psr-http-message-bridge" /> <path value="$PROJECT_DIR$/vendor/symfony/psr-http-message-bridge" />
<path value="$PROJECT_DIR$/vendor/laravel/sanctum" />
</include_path> </include_path>
</component> </component>
<component name="PhpProjectSharedConfiguration" php_language_level="7.3" /> <component name="PhpProjectSharedConfiguration" php_language_level="7.3" />

17
app/Facades/JSON.php Normal file
View File

@ -0,0 +1,17 @@
<?php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class JSON extends Facade
{
protected static function getFacadeAccessor()
{
return 'json';
}
}

116
app/Helpers/JSON.php Normal file
View File

@ -0,0 +1,116 @@
<?php
namespace App\Helpers;
/**
* Class JSON - Used for JSON responses.
* @package App\Helpers
*/
class JSON
{
protected $type, $status, $message, $code, $data;
/**
* @param mixed $type
*/
public function setResponseType($type): JSON
{
$this->type = $type;
return $this;
}
/**
* @return mixed
*/
public function getType()
{
return $this->type;
}
/**
* @return mixed
*/
public function getStatus()
{
return $this->status;
}
/**
* @param mixed $status
* @return JSON
*/
public function setStatus($status)
{
$this->status = $status;
return $this;
}
/**
* @return mixed
*/
public function getMessage()
{
return $this->message;
}
/**
* @param mixed $message
* @return JSON
*/
public function setMessage($message)
{
$this->message = $message;
return $this;
}
/**
* @return mixed
*/
public function getCode()
{
return $this->code;
}
/**
* @param mixed $code
* @return JSON
*/
public function setCode($code)
{
$this->code = $code;
return $this;
}
/**
* @return mixed
*/
public function getData()
{
return $this->data;
}
/**
* @param mixed $data
* @return JSON
*/
public function setData($data)
{
$this->data = $data;
return $this;
}
public function build($headers = [])
{
$response = [
'status' => $this->getStatus(),
'message' => $this->getMessage(),
'type' => $this->getType(),
'response' => $this->getData()
];
return response($response, $this->getCode(), $headers);
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace App\Providers;
use App\Helpers\JSON;
use Illuminate\Support\ServiceProvider;
class JSONProvider extends ServiceProvider
{
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
App::bind('json', function () {
return new JSON();
});
}
}

View File

@ -244,6 +244,7 @@ return [
\App\Providers\MojangStatusProvider::class, \App\Providers\MojangStatusProvider::class,
\App\Providers\OptionsProvider::class, \App\Providers\OptionsProvider::class,
App\Providers\DigitalStorageProvider::class, App\Providers\DigitalStorageProvider::class,
App\Providers\JSONProvider::class,
], ],
@ -301,6 +302,7 @@ return [
'Markdown' => GrahamCampbell\Markdown\Facades\Markdown::class, 'Markdown' => GrahamCampbell\Markdown\Facades\Markdown::class,
'ContextAwareValidator' => App\Facades\ContextAwareValidation::class, 'ContextAwareValidator' => App\Facades\ContextAwareValidation::class,
'Settings' => App\Facades\Options::class, 'Settings' => App\Facades\Options::class,
'JSON' => App\Facades\JSON::class
], ],