Skip to content

Commit

Permalink
pref: response format
Browse files Browse the repository at this point in the history
  • Loading branch information
jiannei committed Feb 6, 2023
1 parent de97216 commit 24f6b7b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 39 deletions.
8 changes: 0 additions & 8 deletions src/Contracts/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ interface Format
*/
public function data(?array $data, ?string $message, int $code, $errors = null): array;

/**
* Http status code.
*
* @param $code
* @return int
*/
public function statusCode($code): int;

/**
* Format paginator data.
*
Expand Down
39 changes: 27 additions & 12 deletions src/Support/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Jiannei\Response\Laravel\Support;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Pagination\AbstractPaginator;
Expand All @@ -29,6 +30,20 @@ public function __construct($config = [])
$this->config = $config;
}

/**
* Return a new JSON response from the application.
*
* @param mixed $data
* @param int $status
* @param array $headers
* @param int $options
* @return JsonResponse
*/
public function response($data = [], int $status = 200, array $headers = [], int $options = 0): JsonResponse
{
return new JsonResponse($data, $this->formatStatusCode($status), $headers, $options);
}

/**
* Format return data structure.
*
Expand All @@ -49,17 +64,6 @@ public function data(?array $data, ?string $message, int $code, $errors = null):
], $this->config);
}

/**
* Http status code.
*
* @param $code
* @return int
*/
public function statusCode($code): int
{
return (int) substr($code, 0, 3);
}

/**
* Format paginator data.
*
Expand Down Expand Up @@ -145,7 +149,7 @@ protected function formatMessage(int $code, ?string $message): ?string
*/
protected function formatStatus(int $code): string
{
$statusCode = $this->statusCode($code);
$statusCode = $this->formatStatusCode($code);
if ($statusCode >= 400 && $statusCode <= 499) {// client error
$status = 'error';
} elseif ($statusCode >= 500 && $statusCode <= 599) {// service error
Expand All @@ -157,6 +161,17 @@ protected function formatStatus(int $code): string
return $status;
}

/**
* Http status code.
*
* @param $code
* @return int
*/
protected function formatStatusCode($code): int
{
return (int) substr($code, 0, 3);
}

/**
* Format paginated data.
*
Expand Down
24 changes: 5 additions & 19 deletions src/Support/Traits/JsonResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public function errorInternal(string $message = '')
*/
public function fail(string $message = '', int $code = 500, $errors = null, array $header = [], int $options = 0)
{
$response = $this->response(
$response = $this->formatter->response(
$this->formatter->data(null, $message, $code, $errors),
Config::get('response.error_code') ?: $code,
$header,
Expand Down Expand Up @@ -199,7 +199,7 @@ public function success($data = [], string $message = '', int $code = 200, array
{
if ($data instanceof ResourceCollection) {
return tap(
$this->response($this->formatter->resourceCollection(...func_get_args()), $code, $headers, $option),
$this->formatter->response($this->formatter->resourceCollection(...func_get_args()), $code, $headers, $option),
function ($response) use ($data) {
$response->original = $data->resource->map(
function ($item) {
Expand All @@ -214,7 +214,7 @@ function ($item) {

if ($data instanceof JsonResource) {
return tap(
$this->response($this->formatter->jsonResource(...func_get_args()), $code, $headers, $option),
$this->formatter->response($this->formatter->jsonResource(...func_get_args()), $code, $headers, $option),
function ($response) use ($data) {
$response->original = $data->resource;

Expand All @@ -224,27 +224,13 @@ function ($response) use ($data) {
}

if ($data instanceof AbstractPaginator) {
return $this->response($this->formatter->paginator(...func_get_args()), $code, $headers, $option);
return $this->formatter->response($this->formatter->paginator(...func_get_args()), $code, $headers, $option);
}

if ($data instanceof Arrayable) {
$data = $data->toArray();
}

return $this->response($this->formatter->data(Arr::wrap($data), $message, $code), $code, $headers, $option);
}

/**
* Return a new JSON response from the application.
*
* @param mixed $data
* @param int $status
* @param array $headers
* @param int $options
* @return JsonResponse
*/
protected function response($data = [], int $status = 200, array $headers = [], int $options = 0): JsonResponse
{
return new JsonResponse($data, $this->formatter->statusCode($status), $headers, $options);
return $this->formatter->response($this->formatter->data(Arr::wrap($data), $message, $code), $code, $headers, $option);
}
}

0 comments on commit 24f6b7b

Please sign in to comment.