Skip to content

Commit 67fc175

Browse files
committed
Paginator for index
1 parent a9a3798 commit 67fc175

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/Controllers/RestResponse.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
use Binaryk\LaravelRestify\Contracts\RestifySearchable;
66
use Binaryk\LaravelRestify\Repositories\Repository;
7+
use Binaryk\LaravelRestify\Repositories\RepositoryCollection;
78
use Illuminate\Contracts\Routing\ResponseFactory;
89
use Illuminate\Contracts\Support\Arrayable;
910
use Illuminate\Contracts\Support\Responsable;
1011
use Illuminate\Database\Eloquent\Model;
1112
use Illuminate\Http\JsonResponse;
13+
use Illuminate\Pagination\AbstractPaginator;
1214
use Illuminate\Support\Arr;
1315
use Illuminate\Support\Facades\App;
1416
use Throwable;
@@ -200,7 +202,7 @@ public function errors($errors)
200202
*/
201203
public function addError($message)
202204
{
203-
if (! isset($this->errors)) {
205+
if (!isset($this->errors)) {
204206
$this->errors = [];
205207
}
206208

@@ -285,7 +287,7 @@ public function __get($key)
285287
return $this->$key;
286288
}
287289

288-
$code = 'static::REST_RESPONSE_'.strtoupper($key).'_CODE';
290+
$code = 'static::REST_RESPONSE_' . strtoupper($key) . '_CODE';
289291

290292
return defined($code) ? constant($code) : null;
291293
}
@@ -300,7 +302,7 @@ public function __get($key)
300302
*/
301303
public function __call($func, $args)
302304
{
303-
$code = 'static::REST_RESPONSE_'.strtoupper($func).'_CODE';
305+
$code = 'static::REST_RESPONSE_' . strtoupper($func) . '_CODE';
304306

305307
if (defined($code)) {
306308
return $this->code(constant($code));
@@ -318,7 +320,7 @@ public function __call($func, $args)
318320
*/
319321
public function respond($response = null)
320322
{
321-
if (! func_num_args()) {
323+
if (!func_num_args()) {
322324
$response = new \stdClass();
323325
$response->data = new \stdClass();
324326

@@ -654,4 +656,14 @@ public static function created()
654656
{
655657
return (new self())->code(201);
656658
}
659+
660+
public static function index(AbstractPaginator $paginator)
661+
{
662+
return response()->json([
663+
'meta' => RepositoryCollection::meta($paginator->toArray()),
664+
'links' => RepositoryCollection::paginationLinks($paginator->toArray()),
665+
'data' => $paginator->getCollection(),
666+
]
667+
);
668+
}
657669
}

tests/Feature/REST/RestControllerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,19 @@ public function test_broker_exists()
120120
{
121121
$this->assertInstanceOf(PasswordBroker::class, $this->controller->broker());
122122
}
123+
124+
public function test_abstract_paginator_response()
125+
{
126+
factory(User::class, 1)->create();
127+
128+
$paginator = User::query()->paginate(5);
129+
130+
$response = RestResponse::index(
131+
$paginator
132+
);
133+
134+
$this->assertObjectHasAttribute('meta', $response->getData());
135+
$this->assertObjectHasAttribute('data', $response->getData());
136+
$this->assertObjectHasAttribute('links', $response->getData());
137+
}
123138
}

0 commit comments

Comments
 (0)