|
| 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 | +} |
0 commit comments