From 2e436e1facd9447752f387d86d00632805a9c169 Mon Sep 17 00:00:00 2001 From: adrianzofcin Date: Wed, 18 Jan 2023 14:03:17 +0100 Subject: [PATCH 1/2] ApiResponse.php changed collection parameter type from mixed to LengthAwarePaginator --- src/Helpers/ApiResponse.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Helpers/ApiResponse.php b/src/Helpers/ApiResponse.php index 932dfbe..5923069 100644 --- a/src/Helpers/ApiResponse.php +++ b/src/Helpers/ApiResponse.php @@ -2,6 +2,8 @@ namespace Wame\ApiResponse\Helpers; +use Illuminate\Pagination\LengthAwarePaginator; + class ApiResponse { /** @@ -72,11 +74,11 @@ public static function errors(mixed $errors): static /** * Response Data with Pagination * - * @param mixed $pagination + * @param LengthAwarePaginator $pagination * @param null $resource * @return static */ - public static function collection(mixed $pagination, $resource = null): static + public static function collection(LengthAwarePaginator $pagination, $resource = null): static { if ($resource) static::$data = (array)($resource::collection($pagination))->toResponse(app('request'))->getData(); else static::$data = $pagination->toArray(); From bd319255954e58534fcd383fc410e576431a3133 Mon Sep 17 00:00:00 2001 From: adrianzofcin <75702986+fetaciik@users.noreply.github.com> Date: Thu, 19 Jan 2023 11:49:59 +0100 Subject: [PATCH 2/2] Update README.md --- README.md | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index decaf51..a6b12f8 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ Response: 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); +return ApiResponse::code('1.2')->message('Hello')->response(201); ``` Response: @@ -56,7 +56,7 @@ Response: ```json { "data": null, - "code": "1.1.1", + "code": "1.2", "errors": null, "message": "Hello" } @@ -67,9 +67,9 @@ If you don't use `message()` function but use `code()` function, and it will try 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', 'user')->response(201); // return "message": "user.1.2" 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" +return ApiResponse::code('1.2')->response(201); // When not provided second parameter it will use default and return "message": "api.1.2" ``` Response: @@ -77,16 +77,27 @@ Response: ```json { "data": null, - "code": "1.1.1", + "code": "1.2", "errors": null, - "message": "user.1.1.1" + "message": "user.1.2" +} +``` + +When not provided second parameter + +```json +{ + "data": null, + "code": "1.2", + "errors": null, + "message": "api.1.2" } ``` You can pass data using `data()` function. ```php -return ApiResponse::data(['id' => 1, 'name' => 'Jhon Jhonson'])->code('1.2.1')->message('Hello')->response(201); +return ApiResponse::data(['id' => 1, 'name' => 'Jhon Jhonson'])->code('1.2')->message('Hello')->response(201); ``` Response: @@ -97,7 +108,7 @@ Response: "id": 1, "name": "Jhon Jhonson" }, - "code": "1.1.1", + "code": "1.2", "errors": null, "message": "Hello" } @@ -128,7 +139,7 @@ You can use this function by passing paginated data, and you can also pass Resou ```php $users = User::paginate(10); -return ApiResponse::collection($users, UserResource::class)->code('1.2.1')->message('Hello')->response(201); +return ApiResponse::collection($users, UserResource::class)->code('1.2')->message('Hello')->response(201); ``` Response: @@ -187,8 +198,8 @@ Response: "to": 2, "total": 6 }, - "code": "1.2.1", + "code": "1.2", "errors": null, "message": "Hello" } -``` \ No newline at end of file +```