generated from open-southeners/php-package-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Builder.php
42 lines (36 loc) · 1.38 KB
/
Builder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
namespace OpenSoutheners\LaravelApiable;
use Illuminate\Pagination\Paginator;
use OpenSoutheners\LaravelApiable\Support\Facades\Apiable;
/**
* @mixin \Illuminate\Database\Eloquent\Builder
*/
class Builder
{
public function jsonApiPaginate()
{
/**
* Paginate the given query using JSON:API.
*
* @param int|string $perPage
* @param array $columns
* @return \OpenSoutheners\LaravelApiable\Http\Resources\JsonApiCollection
*/
return function ($pageSize = null, $columns = ['*'], $page = null) {
$pageName = 'page[number]';
$page = $page ?: Paginator::resolveCurrentPage($pageName);
$pageSize = $pageSize ?: $this->model->getPerPage();
$requestedPageSize = (int) request('page.size', Apiable::config('pagination.default_size'));
if (! $pageSize || $pageSize < $requestedPageSize) {
$pageSize = $requestedPageSize;
}
$results = ($total = $this->toBase()->getCountForPagination())
? $this->forPage($page, $pageSize)->get($columns)
: $this->model->newCollection();
return Apiable::toJsonApi($this->paginator($results, $total, $pageSize, $page, [
'path' => Paginator::resolveCurrentPath(),
'pageName' => $pageName,
]));
};
}
}