diff --git a/src/Contracts/Format.php b/src/Contracts/Format.php deleted file mode 100644 index 2fcf4a0..0000000 --- a/src/Contracts/Format.php +++ /dev/null @@ -1,55 +0,0 @@ - - * - * This source file is subject to the MIT license that is bundled - * with this source code in the file LICENSE. - */ - -namespace Jiannei\Response\Laravel\Contracts; - -use Illuminate\Http\Resources\Json\JsonResource; -use Illuminate\Http\Resources\Json\ResourceCollection; -use Illuminate\Pagination\AbstractCursorPaginator; -use Illuminate\Pagination\AbstractPaginator; - -interface Format -{ - /** - * Format return data structure. - * - * @param array|null $data - * @param string|null $message - * @param int $code - * @param null $errors - * @return array - */ - public function format(?array $data, ?string $message, int $code, $errors = null): array; - - /** - * Format paginator data. - * - * @param AbstractPaginator|AbstractCursorPaginator $resource - * @return array - */ - public function paginator(AbstractPaginator|AbstractCursorPaginator $resource): array; - - /** - * Format collection resource data. - * - * @param ResourceCollection $collection - * @return array - */ - public function resourceCollection(ResourceCollection $collection): array; - - /** - * Format JsonResource Data. - * - * @param JsonResource $resource - * @return array - */ - public function jsonResource(JsonResource $resource): array; -} diff --git a/src/Providers/LaravelServiceProvider.php b/src/Providers/LaravelServiceProvider.php index dc142b2..6414aec 100644 --- a/src/Providers/LaravelServiceProvider.php +++ b/src/Providers/LaravelServiceProvider.php @@ -20,17 +20,6 @@ public function register() $this->setupConfig(); } - public function boot() - { - $formatter = $this->app['config']->get('response.format.class', \Jiannei\Response\Laravel\Support\Format::class); - - if (is_string($formatter) && class_exists($formatter)) { - $this->app->bind(\Jiannei\Response\Laravel\Contracts\Format::class, function () use ($formatter) { - return new $formatter; - }); - } - } - protected function setupConfig() { $path = dirname(__DIR__, 2).'/config/response.php'; diff --git a/src/Response.php b/src/Response.php index 460720d..91721a9 100644 --- a/src/Response.php +++ b/src/Response.php @@ -11,17 +11,9 @@ namespace Jiannei\Response\Laravel; -use Jiannei\Response\Laravel\Contracts\Format; use Jiannei\Response\Laravel\Support\Traits\JsonResponseTrait; class Response { use JsonResponseTrait; - - protected $formatter; - - public function __construct(Format $format) - { - $this->formatter = $format; - } } diff --git a/src/Support/Facades/Format.php b/src/Support/Facades/Format.php new file mode 100644 index 0000000..26031a9 --- /dev/null +++ b/src/Support/Facades/Format.php @@ -0,0 +1,28 @@ +formatDataFields([ 'status' => $this->formatStatus($code), diff --git a/src/Support/Traits/JsonResponseTrait.php b/src/Support/Traits/JsonResponseTrait.php index f9a4d7c..631ada3 100644 --- a/src/Support/Traits/JsonResponseTrait.php +++ b/src/Support/Traits/JsonResponseTrait.php @@ -18,9 +18,9 @@ use Illuminate\Http\Resources\Json\ResourceCollection; use Illuminate\Pagination\AbstractCursorPaginator; use Illuminate\Pagination\AbstractPaginator; -use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Config; +use Jiannei\Response\Laravel\Support\Facades\Format; trait JsonResponseTrait { @@ -30,7 +30,7 @@ trait JsonResponseTrait * @param array $data * @param string $message * @param string $location - * @return JsonResponse|JsonResource + * @return JsonResponse */ public function accepted($data = [], string $message = '', string $location = '') { @@ -48,7 +48,7 @@ public function accepted($data = [], string $message = '', string $location = '' * @param null $data * @param string $message * @param string $location - * @return JsonResponse|JsonResource + * @return JsonResponse */ public function created($data = [], string $message = '', string $location = '') { @@ -64,7 +64,7 @@ public function created($data = [], string $message = '', string $location = '') * Respond with a no content response. * * @param string $message - * @return JsonResponse|JsonResource + * @return JsonResponse */ public function noContent(string $message = '') { @@ -78,7 +78,7 @@ public function noContent(string $message = '') * @param int $code * @param array $headers * @param int $option - * @return JsonResponse|JsonResource + * @return JsonResponse */ public function ok(string $message = '', int $code = 200, array $headers = [], int $option = 0) { @@ -92,7 +92,7 @@ public function ok(string $message = '', int $code = 200, array $headers = [], i * @param int $code * @param array $headers * @param int $option - * @return JsonResponse|JsonResource + * @return JsonResponse */ public function localize(int $code = 200, array $headers = [], int $option = 0) { @@ -173,8 +173,8 @@ public function errorInternal(string $message = '') */ public function fail(string $message = '', int $code = 500, $errors = null, array $header = [], int $options = 0) { - $response = $this->formatter->response( - $this->formatter->format(null, $message, $code, $errors), + $response = Format::response( + Format::data(null, $message, $code, $errors), Config::get('response.error_code') ?: $code, $header, $options @@ -195,18 +195,18 @@ public function fail(string $message = '', int $code = 500, $errors = null, arra * @param int $code * @param array $headers * @param int $option - * @return JsonResponse|JsonResource + * @return JsonResponse */ public function success($data = [], string $message = '', int $code = 200, array $headers = [], int $option = 0) { $data = match (true) { - $data instanceof ResourceCollection => $this->formatter->resourceCollection($data), - $data instanceof JsonResource => $this->formatter->jsonResource($data), - $data instanceof AbstractPaginator || $data instanceof AbstractCursorPaginator => $this->formatter->paginator($data), + $data instanceof ResourceCollection => Format::resourceCollection($data), + $data instanceof JsonResource => Format::jsonResource($data), + $data instanceof AbstractPaginator || $data instanceof AbstractCursorPaginator => Format::paginator($data), $data instanceof Arrayable || (is_object($data) && method_exists($data, 'toArray')) => $data->toArray(), default => Arr::wrap($data) }; - return $this->formatter->response($this->formatter->format($data,$message,$code), $code, $headers, $option); + return Format::response(Format::data($data,$message,$code), $code, $headers, $option); } } diff --git a/tests/Support/Format.php b/tests/Support/Format.php index 39bbc3c..4422ee3 100644 --- a/tests/Support/Format.php +++ b/tests/Support/Format.php @@ -13,7 +13,7 @@ class Format extends \Jiannei\Response\Laravel\Support\Format { - public function format(?array $data, ?string $message, int $code, $errors = null): array + public function data($data, ?string $message, int $code, $errors = null): array { return [ 'status' => $this->formatStatus($code), diff --git a/tests/TestCase.php b/tests/TestCase.php index 1c43831..bf20a51 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -11,6 +11,8 @@ namespace Jiannei\Response\Laravel\Tests; +use Jiannei\Response\Laravel\Tests\Support\Format; + abstract class TestCase extends \Orchestra\Testbench\TestCase { protected function getPackageProviders($app)