-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jiannei
committed
Feb 4, 2021
1 parent
130d914
commit 56e9601
Showing
6 changed files
with
79 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace Jiannei\Response\Laravel\Support\Serializers; | ||
|
||
use Illuminate\Support\Facades\Config; | ||
use League\Fractal\Pagination\PaginatorInterface; | ||
use League\Fractal\Serializer\ArraySerializer as FractalArraySerializer; | ||
|
||
class ArraySerializer extends FractalArraySerializer | ||
{ | ||
/** | ||
* Serialize a collection. | ||
* | ||
* @param string $resourceKey | ||
* @param array $data | ||
* | ||
* @return array | ||
*/ | ||
public function collection($resourceKey, array $data) | ||
{ | ||
$paginationDataField = Config::get('response.format.paginated_resource.data_field', 'data'); | ||
|
||
return [$resourceKey ?: $paginationDataField => $data]; | ||
} | ||
|
||
/** | ||
* Serialize the paginator. | ||
* | ||
* @param PaginatorInterface $paginator | ||
* | ||
* @return array | ||
*/ | ||
public function paginator(PaginatorInterface $paginator) | ||
{ | ||
$currentPage = (int) $paginator->getCurrentPage(); | ||
$lastPage = (int) $paginator->getLastPage(); | ||
$isSimplePaginator = property_exists($paginator->getPaginator(), 'hasMore'); | ||
|
||
$pagination = [ | ||
'total' => (int) $paginator->getTotal(), | ||
'count' => (int) $paginator->getCount(), | ||
'per_page' => (int) $paginator->getPerPage(), | ||
'current_page' => $currentPage, | ||
'total_pages' => $lastPage, | ||
'links' => [], | ||
]; | ||
|
||
if ($currentPage > 1) { | ||
$pagination['links']['previous'] = $paginator->getUrl($currentPage - 1); | ||
} | ||
|
||
if ($currentPage < $lastPage || ($isSimplePaginator && $paginator->getPaginator()->hasMore)) { | ||
$pagination['links']['next'] = $paginator->getUrl($currentPage + 1); | ||
} | ||
|
||
if (empty($pagination['links'])) { | ||
$pagination['links'] = (object) []; | ||
} | ||
|
||
if ($isSimplePaginator) { | ||
unset($pagination['total'], $pagination['total_pages']); | ||
} | ||
|
||
return ['pagination' => $pagination]; | ||
} | ||
} |
File renamed without changes.