Skip to content

Commit

Permalink
feat: support enum locale message
Browse files Browse the repository at this point in the history
  • Loading branch information
jiannei committed Oct 13, 2023
1 parent 6636310 commit fb2fa6f
Show file tree
Hide file tree
Showing 19 changed files with 603 additions and 630 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
include:
- laravel: 10.*
testbench: 8.*
phpunit: ^10.0
pest: ^2.23

steps:
- name: Checkout code
Expand All @@ -46,9 +46,9 @@ jobs:

- name: Install dependencies - L${{ matrix.laravel }}
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "phpunit/phpunit:${{ matrix.phpunit }}" --no-interaction --no-update
composer require --dev "jiannei/laravel-enum" --no-interaction --no-update
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" "pestphp/pest:${{ matrix.pest }}" --no-interaction --no-update
composer require --dev "jiannei/laravel-enum:dev-main" --no-interaction --no-update
composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
- name: Execute tests
run: vendor/bin/phpunit
run: vendor/bin/pest
13 changes: 9 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
},
"require-dev": {
"orchestra/testbench": "^8.0",
"jiannei/laravel-enum": "^3.0",
"phpunit/phpunit": "^10.0"
"pestphp/pest": "^2.23",
"jiannei/laravel-enum": "dev-main"
},
"autoload": {
"psr-4": {
Expand All @@ -38,8 +38,13 @@
}
},
"scripts": {
"test": "vendor/bin/phpunit"
"test": "vendor/bin/pest"
},
"minimum-stability": "dev",
"prefer-stable" : true
"prefer-stable" : true,
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
}
}
11 changes: 2 additions & 9 deletions config/response.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,8 @@

'error_code' => false,

// You can use enumerations to define the code when the response is returned,
// and set the response message according to the locale
//
// The following two enumeration packages are good choices
//
// https://github.com/Jiannei/laravel-enum
// https://github.com/BenSampo/laravel-enum

'enum' => '', // \Jiannei\Enum\Laravel\Repositories\Enums\HttpStatusCodeEnum::class
// lang/zh_CN/enums.php
'locale' => 'enums',// enums.\Jiannei\Enum\Laravel\Support\Enums\HttpStatusCode::class

// You can set some attributes (eg:code/message/header/options) for the exception, and it will override the default attributes of the exception
'exception' => [
Expand Down
8 changes: 4 additions & 4 deletions src/Support/Facades/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@
* @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 JsonResponse localize(int|\BackedEnum $code = 200, array $headers = [], int $option = 0)
* @method static JsonResponse ok(string $message = '', int|\BackedEnum $code = 200, array $headers = [], int $option = 0)
* @method static JsonResponse success($data = null, string $message = '', int|\BackedEnum $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 = '')
* @method static void errorNotFound(string $message = '')
* @method static void errorMethodNotAllowed(string $message = '')
* @method static void errorInternal(string $message = '')
* @method static JsonResponse fail(string $message = '', int $code = 500, $errors = null, array $header = [], int $options = 0)
* @method static JsonResponse fail(string $message = '', int|\BackedEnum $code = 500, $errors = null, array $header = [], int $options = 0)
*
* @see \Jiannei\Response\Laravel\Response
*/
Expand Down
24 changes: 11 additions & 13 deletions src/Support/Format.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function response(
string $from = 'success'
): JsonResponse {
return new JsonResponse(
$this->data($data, $message, $code, $errors),
$this->data($data, $message, $code, $errors, $from),
$this->formatStatusCode($code, $from),
$headers,
$option
Expand All @@ -67,7 +67,7 @@ public function response(
* @param null $errors
* @return array
*/
public function data($data, ?string $message, int|\BackedEnum $code, $errors = null): array
public function data($data, ?string $message, int|\BackedEnum $code, $errors = null, $from = 'success'): array
{
$data = match (true) {
$data instanceof ResourceCollection => $this->resourceCollection($data),
Expand All @@ -78,7 +78,7 @@ public function data($data, ?string $message, int|\BackedEnum $code, $errors = n
};

return $this->formatDataFields([
'status' => $this->formatStatus($code),
'status' => $this->formatStatus($code,$from),
'code' => $this->formatBusinessCode($code),
'message' => $this->formatMessage($code, $message),
'data' => $data ?: (object) $data,
Expand Down Expand Up @@ -134,10 +134,11 @@ public function jsonResource(JsonResource $resource): array
*/
protected function formatMessage(int|\BackedEnum $code, ?string $message): ?string
{
$localizationKey = Config::get('response.localization', 'response');
$localizationKey = join('.', [Config::get('response.locale', 'enums'), $this->formatBusinessCode($code)]);

return match (true) {
!$message && Lang::has($localizationKey.$code) => Lang::get($localizationKey),
!$message && Lang::has($localizationKey) => Lang::get($localizationKey),
$code instanceof \BackedEnum && method_exists($code, 'description') => $code->description(),
default => $message
};
}
Expand All @@ -150,7 +151,7 @@ protected function formatMessage(int|\BackedEnum $code, ?string $message): ?stri
*/
protected function formatBusinessCode(int|\BackedEnum $code): int
{
return enum_exists($code) ? $code->value : $code;
return $code instanceof \BackedEnum ? $code->value : $code;
}

/**
Expand All @@ -159,9 +160,9 @@ protected function formatBusinessCode(int|\BackedEnum $code): int
* @param int|\BackedEnum $code
* @return string
*/
protected function formatStatus(int|\BackedEnum $code): string
protected function formatStatus(int|\BackedEnum $code, string $from = 'success'): string
{
$statusCode = $this->formatStatusCode($code);
$statusCode = $this->formatStatusCode($code, $from);

return match (true) {
($statusCode >= 400 && $statusCode <= 499) => 'error',// client error
Expand All @@ -179,12 +180,9 @@ protected function formatStatus(int|\BackedEnum $code): string
*/
protected function formatStatusCode(int|\BackedEnum $code, string $from = 'success'): int
{
$code = match (true) {
$from === 'fail' => (Config::get('response.error_code') ?: $code),
default => $this->formatBusinessCode($code)
};
$code = $from === 'fail' ? (Config::get('response.error_code') ?: $code) : $code;

return (int) substr($code, 0, 3);
return (int) substr($this->formatBusinessCode($code), 0, 3);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Support/Traits/JsonResponseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function noContent(string $message = ''): JsonResponse
* Alias of success method, no need to specify data parameter.
*
* @param string $message
* @param int $code
* @param int|\BackedEnum $code
* @param array $headers
* @param int $option
* @return JsonResponse
Expand All @@ -81,7 +81,7 @@ public function ok(string $message = '', int|\BackedEnum $code = 200, array $hea
* Alias of the successful method, no need to specify the message and data parameters.
* You can use ResponseCodeEnum to localize the message.
*
* @param int $code
* @param int|\BackedEnum $code
* @param array $headers
* @param int $option
* @return JsonResponse
Expand Down Expand Up @@ -177,13 +177,13 @@ public function fail(string $message = '', int|\BackedEnum $code = 500, $errors
*
* @param mixed $data
* @param string $message
* @param int $code
* @param int|\BackedEnum $code
* @param array $headers
* @param int $option
* @return JsonResponse
*/
public function success($data = [], string $message = '', int|\BackedEnum $code = 200, array $headers = [], int $option = 0)
{
return Format::response(...func_get_args());
return Format::response($data, $message, $code, null, $headers, $option);
}
}
32 changes: 32 additions & 0 deletions tests/Enums/ResponseEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Jiannei\Response\Laravel\Tests\Enums;

use Jiannei\Enum\Laravel\Support\Traits\EnumEnhance;

enum ResponseEnum: int
{
use EnumEnhance;

// 业务操作正确码:1xx、2xx、3xx 开头,后拼接 3 位
// 200 + 001 => 200001,也就是有 001 ~ 999 个编号可以用来表示业务成功的情况,当然你可以根据实际需求继续增加位数,但必须要求是 200 开头
// 举个栗子:你可以定义 001 ~ 099 表示系统状态;100 ~ 199 表示授权业务;200 ~ 299 表示用户业务...
case SERVICE_REGISTER_SUCCESS = 200101;
case SERVICE_LOGIN_SUCCESS = 200102;

// 业务操作错误码(外部服务或内部服务调用...)
case SERVICE_REGISTER_ERROR = 500101;
case SERVICE_LOGIN_ERROR = 500102;

// 客户端错误码:400 ~ 499 开头,后拼接 3 位
case CLIENT_PARAMETER_ERROR = 400001;
case CLIENT_CREATED_ERROR = 400002;
case CLIENT_DELETED_ERROR = 400003;

// 服务端操作错误码:500 ~ 599 开头,后拼接 3 位
case SYSTEM_ERROR = 500001;
case SYSTEM_UNAVAILABLE = 500002;
case SYSTEM_CACHE_CONFIG_ERROR = 500003;
case SYSTEM_CACHE_MISSED_ERROR = 500004;
case SYSTEM_CONFIG_ERROR = 500005;
}
151 changes: 0 additions & 151 deletions tests/FailTest.php

This file was deleted.

5 changes: 5 additions & 0 deletions tests/Feature/ExampleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

test('example', function () {
expect(true)->toBeTrue();
});
Loading

0 comments on commit fb2fa6f

Please sign in to comment.