Skip to content

Commit

Permalink
feat: 使用 Fractal 重构 Format
Browse files Browse the repository at this point in the history
  • Loading branch information
jiannei committed Oct 11, 2023
1 parent d10582f commit 7bdfaf0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 42 deletions.
8 changes: 4 additions & 4 deletions src/Support/Facades/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
use Illuminate\Support\Facades\Facade as IlluminateFacade;

/**
* @method static JsonResponse response($data = [], int $status = 200, array $headers = [], int $options = 0)
* @method static JsonResponse response($data = null, string $message = '', int $code = 200, $errors = null, array $headers = [], int $option = 0, string $from = 'success')
* @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)
* @method static array paginator(AbstractPaginator|AbstractCursorPaginator $resource, $transformer = null, $resourceName = null)
* @method static array resourceCollection(ResourceCollection $collection, $transformer = null, $resourceName = null)
* @method static array jsonResource(JsonResource $resource, $transformer = null, $resourceName = null)
*
* @see \Jiannei\Response\Laravel\Support\Format
*/
Expand Down
40 changes: 25 additions & 15 deletions src/Support/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ class Format
* Return a new JSON response from the application.
*
* @param mixed $data
* @param int $status
* @param string $message
* @param int $code
* @param null $errors
* @param array $headers
* @param int $options
* @param int $option
* @param string $from
* @return JsonResponse
*/
public function response($data = [], int $status = 200, array $headers = [], int $options = 0): JsonResponse
public function response($data = null, string $message = '', int $code = 200, $errors = null, array $headers = [], int $option = 0, string $from = 'success'): JsonResponse
{
return new JsonResponse($data, $this->formatStatusCode($status), $headers, $options);
return new JsonResponse($this->data($data, $message, $code, $errors), $this->formatStatusCode($code, $from), $headers, $option);
}

/**
Expand Down Expand Up @@ -79,13 +82,15 @@ public function data($data, ?string $message, int $code, $errors = null): array
* Format paginator data.
*
* @param AbstractPaginator|AbstractCursorPaginator $resource
* @param null $transformer
* @param null $resourceName
* @return array
*/
public function paginator(AbstractPaginator|AbstractCursorPaginator $resource): array
public function paginator(AbstractPaginator|AbstractCursorPaginator $resource, $transformer = null, $resourceName = null): array
{
$fractal = fractal()->collection($resource, function ($item) {
$fractal = fractal()->collection($resource, $transformer ?: function ($item) {
return $item->toArray();
})->serializeWith(DataArraySerializer::class);
}, $resourceName)->serializeWith(DataArraySerializer::class);

return tap($fractal, $this->formatCollection($resource))->toArray();
}
Expand All @@ -94,13 +99,15 @@ public function paginator(AbstractPaginator|AbstractCursorPaginator $resource):
* Format collection resource data.
*
* @param ResourceCollection $collection
* @param null $transformer
* @param null $resourceName
* @return array
*/
public function resourceCollection(ResourceCollection $collection): array
public function resourceCollection(ResourceCollection $collection, $transformer = null, $resourceName = null): array
{
$fractal = fractal()->collection($collection->resource, function (JsonResource $resource) {
$fractal = fractal()->collection($collection->resource, $transformer ?: function (JsonResource $resource) {
return array_merge_recursive($resource->resolve(request()), $resource->with(request()), $resource->additional);
})->serializeWith(DataArraySerializer::class);
}, $resourceName)->serializeWith(DataArraySerializer::class);

return tap($fractal, $this->formatCollection($collection->resource))->toArray();
}
Expand All @@ -109,21 +116,23 @@ public function resourceCollection(ResourceCollection $collection): array
* Format JsonResource Data.
*
* @param JsonResource $resource
* @param null $transformer
* @param null $resourceName
* @return array
*/
public function jsonResource(JsonResource $resource): array
public function jsonResource(JsonResource $resource, $transformer = null, $resourceName = null): array
{
$data = array_merge_recursive($resource->resolve(request()), $resource->with(request()), $resource->additional);

return fractal()->item($data, fn() => $data)->serializeWith(ArraySerializer::class)->toArray();
return fractal()->item($data, $transformer ?: fn() => $data, $resourceName)->serializeWith(ArraySerializer::class)->toArray();
}

/**
* Format return message.
*
* @param int $code
* @param string|null $message
* @return string
* @return string|null
*/
protected function formatMessage(int $code, ?string $message): ?string
{
Expand Down Expand Up @@ -158,11 +167,12 @@ protected function formatStatus(int $code): string
* Http status code.
*
* @param $code
* @param string $from
* @return int
*/
protected function formatStatusCode($code): int
protected function formatStatusCode($code, string $from = 'success'): int
{
return (int) substr($code, 0, 3);
return (int) substr($from === 'fail' ? (Config::get('response.error_code') ?: $code) : $code, 0, 3);
}

protected function formatCollection($collection): \Closure
Expand Down
38 changes: 16 additions & 22 deletions src/Support/Traits/JsonResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ trait JsonResponseTrait
* @param string $location
* @return JsonResponse
*/
public function accepted($data = [], string $message = '', string $location = '')
public function accepted($data = [], string $message = '', string $location = ''): JsonResponse
{
$response = $this->success($data, $message, 202);
if ($location) {
Expand All @@ -50,7 +50,7 @@ public function accepted($data = [], string $message = '', string $location = ''
* @param string $location
* @return JsonResponse
*/
public function created($data = [], string $message = '', string $location = '')
public function created($data = [], string $message = '', string $location = ''): JsonResponse
{
$response = $this->success($data, $message, 201);
if ($location) {
Expand All @@ -66,7 +66,7 @@ public function created($data = [], string $message = '', string $location = '')
* @param string $message
* @return JsonResponse
*/
public function noContent(string $message = '')
public function noContent(string $message = ''): JsonResponse
{
return $this->success([], $message, 204);
}
Expand Down Expand Up @@ -104,7 +104,7 @@ public function localize(int $code = 200, array $headers = [], int $option = 0)
*
* @param string|null $message
*/
public function errorBadRequest(string $message = '')
public function errorBadRequest(string $message = ''): void
{
$this->fail($message, 400);
}
Expand All @@ -114,7 +114,7 @@ public function errorBadRequest(string $message = '')
*
* @param string $message
*/
public function errorUnauthorized(string $message = '')
public function errorUnauthorized(string $message = ''): void
{
$this->fail($message, 401);
}
Expand All @@ -124,7 +124,7 @@ public function errorUnauthorized(string $message = '')
*
* @param string $message
*/
public function errorForbidden(string $message = '')
public function errorForbidden(string $message = ''): void
{
$this->fail($message, 403);
}
Expand All @@ -134,7 +134,7 @@ public function errorForbidden(string $message = '')
*
* @param string $message
*/
public function errorNotFound(string $message = '')
public function errorNotFound(string $message = ''): void
{
$this->fail($message, 404);
}
Expand All @@ -144,7 +144,7 @@ public function errorNotFound(string $message = '')
*
* @param string $message
*/
public function errorMethodNotAllowed(string $message = '')
public function errorMethodNotAllowed(string $message = ''): void
{
$this->fail($message, 405);
}
Expand All @@ -154,7 +154,7 @@ public function errorMethodNotAllowed(string $message = '')
*
* @param string $message
*/
public function errorInternal(string $message = '')
public function errorInternal(string $message = ''): void
{
$this->fail($message);
}
Expand All @@ -164,21 +164,15 @@ public function errorInternal(string $message = '')
*
* @param string $message
* @param int $code
* @param array|null $errors
* @param array $header
* @param int $options
* @param null $errors
* @param array $headers
* @param int $option
* @return JsonResponse
*
* @throws HttpResponseException
*/
public function fail(string $message = '', int $code = 500, $errors = null, array $header = [], int $options = 0)
public function fail(string $message = '', int $code = 500, $errors = null, array $headers = [], int $option = 0): JsonResponse
{
$response = Format::response(
Format::data(null, $message, $code, $errors),
Config::get('response.error_code') ?: $code,
$header,
$options
);
$response = Format::response(null, $message, $code, $errors, $headers, $option, 'fail');

if (is_null($errors)) {
$response->throwResponse();
Expand All @@ -190,7 +184,7 @@ public function fail(string $message = '', int $code = 500, $errors = null, arra
/**
* Return a success response.
*
* @param JsonResource|array|mixed $data
* @param mixed $data
* @param string $message
* @param int $code
* @param array $headers
Expand All @@ -199,6 +193,6 @@ public function fail(string $message = '', int $code = 500, $errors = null, arra
*/
public function success($data = [], string $message = '', int $code = 200, array $headers = [], int $option = 0)
{
return Format::response(Format::data($data,$message,$code), $code, $headers, $option);
return Format::response(...func_get_args());
}
}
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 data($data, ?string $message, int $code, $errors = null): array
public function data(mixed $data, ?string $message, int $code, $errors = null): array
{
return [
'status' => $this->formatStatus($code),
Expand Down

0 comments on commit 7bdfaf0

Please sign in to comment.