Skip to content

Commit

Permalink
feat: support get data before response
Browse files Browse the repository at this point in the history
  • Loading branch information
jiannei committed Oct 13, 2023
1 parent 513f00d commit 0b1c2df
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/Support/Facades/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
use Illuminate\Support\Facades\Facade as IlluminateFacade;

/**
* @method static array|\Jiannei\Response\Laravel\Support\Format data($data = null)
* @method static \Jiannei\Response\Laravel\Support\Format data(mixed $data = null, string $message = '', int|\BackedEnum $code = 200, $error = null)
* @method static array paginator(AbstractPaginator|AbstractCursorPaginator|Paginator $resource)
* @method static array resourceCollection(ResourceCollection $collection)
* @method static array jsonResource(JsonResource $resource)
Expand Down
50 changes: 26 additions & 24 deletions src/Support/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,34 +39,36 @@ class Format
*/
public function response(): JsonResponse
{
return new JsonResponse($this->data, $this->statusCode);
return tap(new JsonResponse($this->data, $this->statusCode), function () {
$this->data = null;
$this->statusCode = 200;
});
}

public function get(): ?array
{
return $this->data;
}

/**
* Core format.
*
* @param $data
* @return array|$this
* @param null $data
* @param string $message
* @param int|\BackedEnum $code
* @param null $error
* @return Format
*/
public function data($data = null): static|array
public function data(mixed $data = null, string $message = '', int|\BackedEnum $code = 200, $error = null): static
{
if (is_null($data)) {
return $this->data;
}

$bizCode = $data['code'] ?? 200;
$oriData = $data['data'] ?? null;
$message = $data['message'] ?? '';
$error = $data['error'] ?? [];

return tap($this, function () use ($bizCode, $oriData, $message, $error) {
$this->statusCode = $this->formatStatusCode($this->formatBusinessCode($bizCode), $oriData);
return tap($this, function () use ($data, $message, $code, $error) {
$this->statusCode = $this->formatStatusCode($this->formatBusinessCode($code), $data);

$this->data = $this->formatDataFields([
'status' => $this->formatStatus($this->statusCode),
'code' => $this->formatBusinessCode($bizCode),
'message' => $this->formatMessage($this->formatBusinessCode($bizCode), $message),
'data' => $this->formatData($oriData),
'code' => $this->formatBusinessCode($code),
'message' => $this->formatMessage($this->formatBusinessCode($code), $message),
'data' => $this->formatData($data),
'error' => $this->formatError($error),
]);
});
Expand Down Expand Up @@ -142,7 +144,7 @@ protected function formatMessage(int $code, string $message = ''): ?string
$localizationKey = join('.', [Config::get('response.locale', 'enums'), $code]);

return match (true) {
! $message && Lang::has($localizationKey) => Lang::get($localizationKey),
!$message && Lang::has($localizationKey) => Lang::get($localizationKey),
default => $message
};
}
Expand Down Expand Up @@ -247,12 +249,12 @@ protected function formatMeta($collection): array
/**
* Format error.
*
* @param array $error
* @param array|null $error
* @return array|object
*/
protected function formatError(array $error): object|array
protected function formatError(?array $error): object|array
{
return Config::get('app.debug') ? $error : (object) [];
return $error ?: (object) [];
}

/**
Expand All @@ -266,7 +268,7 @@ protected function formatDataFields(array $data): array
$formatConfig = Config::get('response.format.config', []);

foreach ($formatConfig as $key => $config) {
if (! Arr::has($data, $key)) {
if (!Arr::has($data, $key)) {
continue;
}

Expand All @@ -279,7 +281,7 @@ protected function formatDataFields(array $data): array
$key = $alias;
}

if (! $show) {
if (!$show) {
$data = Arr::except($data, $key);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Support/Traits/JsonResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public function errorMethodNotAllowed(string $message = ''): JsonResponse
*/
public function fail(string $message = '', int|\BackedEnum $code = 500, $errors = null): JsonResponse
{
return Format::data(compact('message', 'code', 'errors'))->response();
return Format::data(message: $message, code: $code, error: $errors)->response();
}

/**
Expand All @@ -165,6 +165,6 @@ public function fail(string $message = '', int|\BackedEnum $code = 500, $errors
*/
public function success($data = [], string $message = '', int|\BackedEnum $code = 200)
{
return Format::data(compact('data', 'message', 'code'))->response();
return Format::data(data: $data, message: $message, code: $code)->response();
}
}
39 changes: 39 additions & 0 deletions tests/Unit/SuccessTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,42 @@
'error' => [],
]);
});

test('get formatted data before response', function () {
$data = Format::data([
'name' => 'Jiannei',
'email' => '[email protected]',
], '创建成功', 201);

expect($data->get())->toMatchArray([
'status' => 'success',
'code' => 201,
'message' => '创建成功',
'data' => [
'name' => 'Jiannei',
'email' => '[email protected]',
],
'error' => (object) [],
]);

$response = $data->response();

expect($response->status())->toEqual(201)
->and($response->getData(true))->toMatchArray([
'status' => 'success',
'code' => 201,
'message' => '创建成功',
'data' => [
'name' => 'Jiannei',
'email' => '[email protected]',
],
'error' => [],
]);
});

test('compare data and success', function () {
expect(Response::success()->status())->toEqual(Format::data(null, '', 200)->response()->status())
->and(Response::success()->content())->toEqual(Format::data(null, '', 200)->response()->content());
});


0 comments on commit 0b1c2df

Please sign in to comment.