Skip to content

Commit

Permalink
ApiResponse.php added message translation prefix as passable parameter
Browse files Browse the repository at this point in the history
README.md added example of usage
  • Loading branch information
adrianzofcin committed Jan 17, 2023
1 parent 8786be2 commit 832c8c6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
18 changes: 14 additions & 4 deletions src/Helpers/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -99,28 +105,32 @@ 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 {
$response = [
'data' => self::$data,
'code' => self::$code,
'errors' => self::$errors,
'message' => self::$message ?? __('api.' . self::$code),
'message' => $message,
];
}

Expand Down

0 comments on commit 832c8c6

Please sign in to comment.