8000 First pass at pagination support. · brysem/laravel-json-api@dc21e30 · GitHub
[go: up one dir, main page]

Skip to content

Commit dc21e30

Browse files
committed
First pass at pagination support.
1 parent c68eb35 commit dc21e30

File tree

4 files changed

+614
-1
lines changed

4 files changed

+614
-1
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2016 Cloud Creativity Limited
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace CloudCreativity\JsonApi\Http\Controllers;
20+
21+
use CloudCreativity\JsonApi\Pagination\Paginator;
22+
use Illuminate\Pagination\AbstractPaginator;
23+
use Neomerx\JsonApi\Contracts\Parameters\ParametersInterface;
24+
25+
trait PaginatedTrait
26+
{
27+
28+
/**
29+
* @return ParametersInterface
30+
*/
31+
abstract public function getParameters();
32+
33+
/**
34+
* @param string|null $pageKey
35+
* @param string|null $perPageKey
36+
* @param string|null $totalKey
37+
* @param string|null $lastPageKey
38+
* @param string|null $firstItemKey
39+
* @param string|null $lastItemKey
40+
* @return Paginator
41+
*/
42+
public function getPaginator(
43+
$pageKey = null,
44+
$perPageKey = null,
45+
$totalKey = null,
46+
$lastPageKey = null,
47+
$firstItemKey = null,
48+
$lastItemKey = null
49+
) {
50+
$paginator = new Paginator(
51+
$this->getParameters(),
52+
$pageKey,
53+
$perPageKey,
54+
$totalKey,
55+
$lastPageKey,
56+
$firstItemKey,
57+
$lastItemKey
58+
);
59+
60+
// Override the page resolver so that it uses the JSON-API page parameter.
61+
AbstractPaginator::currentPageResolver(function () use ($paginator) {
62+
return $paginator->getPage();
63+
});
64+
65+
return $paginator;
66+
}
67+
}

src/Http/Responses/ResponsesHelper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
use CloudCreativity\JsonApi\Contracts\Error\ErrorCollectionInterface;
2222
use CloudCreativity\JsonApi\Contracts\Integration\EnvironmentInterface;
23+
use Illuminate\Contracts\Pagination\Paginator;
2324
use Illuminate\Http\Response;
2425
use Illuminate\Support\Collection;
2526
use Neomerx\JsonApi\Contracts\Document\ErrorInterface;
@@ -98,7 +99,7 @@ public function meta($meta, $statusCode = Response::HTTP_OK, array $headers = []
9899
public function content($data, array $links = [], $meta = null, $statusCode = Response::HTTP_OK, array $headers = [])
99100
{
100101
/** Eloquent collections do not encode properly, so we'll get all just in case it's an Eloquent collection */
101-
if ($data instanceof Collection) {
102+
if ($data instanceof Collection || $data instanceof Paginator) {
102103
$data = $data->all();
103104
}
104105

0 commit comments

Comments
 (0)
0