Skip to content

Commit

Permalink
wip: refactor formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
jiannei committed Oct 11, 2023
1 parent 4f9dc62 commit 623de65
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 97 deletions.
55 changes: 0 additions & 55 deletions src/Contracts/Format.php

This file was deleted.

11 changes: 0 additions & 11 deletions src/Providers/LaravelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
8 changes: 0 additions & 8 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
28 changes: 28 additions & 0 deletions src/Support/Facades/Format.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Jiannei\Response\Laravel\Support\Facades;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Pagination\AbstractCursorPaginator;
use Illuminate\Pagination\AbstractPaginator;
use Illuminate\Support\Facades\Facade as IlluminateFacade;

/**
*
* @method static JsonResponse response($data = [], int $status = 200, array $headers = [], int $options = 0)
* @method static array data($data, ?string $message, int $code, $errors = null)
* @method static array paginator(AbstractPaginator|AbstractCursorPaginator $resource)
* @method static array resourceCollection(ResourceCollection $collection)
* @method static array jsonResource(JsonResource $resource)
*
* @see \Jiannei\Response\Laravel\Support\Format
*/
class Format extends IlluminateFacade
{
protected static function getFacadeAccessor()
{
return config('response.format.class', \Jiannei\Response\Laravel\Support\Format::class);
}
}
12 changes: 6 additions & 6 deletions src/Support/Facades/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@
use Illuminate\Support\Facades\Facade as IlluminateFacade;

/**
* @method static JsonResponse|JsonResource accepted($data = null, string $message = '', string $location = '')
* @method static JsonResponse|JsonResource created($data = null, string $message = '', string $location = '')
* @method static noContent(string $message = '')
* @method static JsonResponse|JsonResource localize(int $code = 200, array $headers = [], int $option = 0)
* @method static JsonResponse|JsonResource ok(string $message = '', int $code = 200, array $headers = [], int $option = 0)
* @method static JsonResponse|JsonResource success($data = null, string $message = '', int $code = 200, array $headers = [], int $option = 0)
* @method static JsonResponse accepted($data = null, string $message = '', string $location = '')
* @method static JsonResponse created($data = null, string $message = '', string $location = '')
* @method static JsonResponse noContent(string $message = '')
* @method static JsonResponse localize(int $code = 200, array $headers = [], int $option = 0)
* @method static JsonResponse ok(string $message = '', int $code = 200, array $headers = [], int $option = 0)
* @method static JsonResponse success($data = null, string $message = '', int $code = 200, array $headers = [], int $option = 0)
* @method static void errorBadRequest(?string $message = '')
* @method static void errorUnauthorized(string $message = '')
* @method static void errorForbidden(string $message = '')
Expand Down
6 changes: 3 additions & 3 deletions src/Support/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
use League\Fractal\Serializer\DataArraySerializer;
use Spatie\Fractal\Fractal;

class Format implements \Jiannei\Response\Laravel\Contracts\Format
class Format
{
use Macroable;

Expand All @@ -49,13 +49,13 @@ public function response($data = [], int $status = 200, array $headers = [], int
/**
* Format return data structure.
*
* @param array|null $data
* @param JsonResource|array|mixed $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
public function data($data, ?string $message, int $code, $errors = null): array
{
return $this->formatDataFields([
'status' => $this->formatStatus($code),
Expand Down
26 changes: 13 additions & 13 deletions src/Support/Traits/JsonResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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 = '')
{
Expand All @@ -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 = '')
{
Expand All @@ -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 = '')
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}
2 changes: 1 addition & 1 deletion tests/Support/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 623de65

Please sign in to comment.