From 832c8c62faae7a6b82927bdb5ea14c1ffe75256e Mon Sep 17 00:00:00 2001 From: adrianzofcin Date: Tue, 17 Jan 2023 11:33:43 +0100 Subject: [PATCH] ApiResponse.php added message translation prefix as passable parameter README.md added example of usage --- README.md | 23 ++++++++++++++++++++++- src/Helpers/ApiResponse.php | 18 ++++++++++++++---- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 59a6531..decaf51 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ Response: } ``` -You can pass internal code using `message()` function that helps you find of response in case of error. +You can pass internal code using `code()` function that helps you find of response in case of error. ```php return ApiResponse::code('1.2.1')->message('Hello')->response(201); @@ -62,6 +62,27 @@ Response: } ``` +If you don't use `message()` function but use `code()` function, and it will try to translate your code to message. + +You can also set prefix of translation as second parameter *(Default is 'api')*. + +```php +return ApiResponse::code('1.2.1', 'user')->response(201); // return "message": "user.1.1.1" as in Response example + +return ApiResponse::code('1.2.1')->response(201); // When not presented second parameter it will use default and return "message": "api.1.1.1" +``` + +Response: + +```json +{ + "data": null, + "code": "1.1.1", + "errors": null, + "message": "user.1.1.1" +} +``` + You can pass data using `data()` function. ```php diff --git a/src/Helpers/ApiResponse.php b/src/Helpers/ApiResponse.php index 77d88f6..932dfbe 100644 --- a/src/Helpers/ApiResponse.php +++ b/src/Helpers/ApiResponse.php @@ -24,15 +24,21 @@ class ApiResponse */ private static string|null $message = null; + /** + * @var string|null + */ + private static string|null $codePrefix = null; + /** * Internal Response Code * * @param string $code * @return static */ - public static function code(string $code): static + public static function code(string $code, string $codePrefix = 'api'): static { static::$code = $code; + static::$codePrefix = $codePrefix; return new static; } @@ -99,20 +105,24 @@ public static function message(string $message): static */ public static function response(int $statusCode = 200): \Illuminate\Http\JsonResponse { + $message = null; + if (self::$message) $message = self::$message; + else $message = self::$code ? __(self::$codePrefix .'.' . self::$code) : null; + if (gettype(self::$data) === 'array') { if (key_exists('data', self::$data)) { $response = collect(self::$data); $response = $response->merge([ 'code' => self::$code, 'errors' => self::$errors, - 'message' => self::$message ?? __('api.' . self::$code), + 'message' => $message, ]); } else { $response = [ 'data' => self::$data, 'code' => self::$code, 'errors' => self::$errors, - 'message' => self::$message ?? __('api.' . self::$code), + 'message' => $message, ]; } } else { @@ -120,7 +130,7 @@ public static function response(int $statusCode = 200): \Illuminate\Http\JsonRes 'data' => self::$data, 'code' => self::$code, 'errors' => self::$errors, - 'message' => self::$message ?? __('api.' . self::$code), + 'message' => $message, ]; }