Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianzofcin committed Jan 20, 2023
2 parents 1828a14 + bd31925 commit 3623a56
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
33 changes: 22 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ 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:

```json
{
"data": null,
"code": "1.1.1",
"code": "1.2",
"errors": null,
"message": "Hello"
}
Expand All @@ -67,26 +67,37 @@ 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:

```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:
Expand All @@ -97,7 +108,7 @@ Response:
"id": 1,
"name": "Jhon Jhonson"
},
"code": "1.1.1",
"code": "1.2",
"errors": null,
"message": "Hello"
}
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -187,8 +198,8 @@ Response:
"to": 2,
"total": 6
},
"code": "1.2.1",
"code": "1.2",
"errors": null,
"message": "Hello"
}
```
```
6 changes: 4 additions & 2 deletions src/Helpers/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Wame\ApiResponse\Helpers;

use Illuminate\Pagination\LengthAwarePaginator;

class ApiResponse
{
/**
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 3623a56

Please sign in to comment.