8000 [Bugfix] Ensure Eloquent adapter applies default pagination · CodingSeo/laravel-json-api@95ede0f · GitHub
[go: up one dir, main page]

Skip to content

Commit 95ede0f

Browse files
committed
[Bugfix] Ensure Eloquent adapter applies default pagination
Fixes cloudcreativity#131
1 parent b5e59b1 commit 95ede0f

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ All notable changes to this project will be documented in this file. This projec
66

77
### Fixed
88
- [#138](https://github.com/cloudcreativity/laravel-json-api/issues/138)
9-
Generator commands not working since Laravel v5.5.28
9+
Generator commands not working since Laravel v5.5.28.
10+
- [#131](https://github.com/cloudcreativity/laravel-json-api/issues/131)
11+
Ensure Eloquent adapter uses default pagination parameters.
1012

1113
## [0.11.3] - 2017-12-01
1214

src/Store/EloquentAdapter.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Illuminate\Support\Collection;
2929
use Neomerx\JsonApi\Contracts\Encoder\Parameters\EncodingParametersInterface;
3030
use Neomerx\JsonApi\Contracts\Encoder\Parameters\SortParameterInterface;
31+
use Neomerx\JsonApi\Encoder\Parameters\EncodingParameters;
3132

3233
/**
3334
* Class EloquentAdapter
@@ -154,7 +155,9 @@ public function query(EncodingParametersInterface $parameters)
154155
throw new RuntimeException('Paging parameters exist but paging is not supported.');
155156
}
156157

157-
return $pagination->isEmpty() ? $this->all($query) : $this->paginate($query, $parameters);
158+
return $pagination->isEmpty() ?
159+
$this->all($query) :
160+
$this->paginate($query, $this->normalizeParameters($parameters, $pagination));
158161
}
159162

160163
/**
@@ -388,4 +391,26 @@ protected function columnForField($field, Model $model)
388391
return $model::$snakeAttributes ? Str::underscore($field) : Str::camelize($field);
389392
}
390393

394+
/**
395+
* Normalize parameters for pagination.
396+
*
397+
* This is a temporary solution for Issue #131 in the v0.11.x series.
398+
*
399+
* @param EncodingParametersInterface $parameters
400+
* @param Collection $extractedPagination
401+
* @return EncodingParameters
402+
* @see https://github.com/cloudcreativity/laravel-json-api/issues/131
403+
*/
404+
private function normalizeParameters(EncodingParametersInterface $parameters, Collection $extractedPagination)
405+
{
406+
return new EncodingParameters(
407+
$parameters->getIncludePaths(),
408+
$parameters->getFieldSets(),
409+
$parameters->getSortParameters(),
410+
$extractedPagination->all(),
411+
$parameters->getFilteringParameters(),
412+
$parameters->getUnrecognizedParameters()
413+
);
414+
}
415+
391416
}

tests/Integration/PaginationTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,30 @@ protected function setUp()
3939
$this->posts = factory(Post::class, 4)->create();
4040
}
4141

42+
/**
43+
* An adapter's default pagination is used if no pagination parameters are sent.
44+
*
45+
* @see https://github.com/cloudcreativity/laravel-json-api/issues/131
46+
*/
47+
public function testDefaultPagination()
48+
{
49+
$response = $this->doSearch();
50+
$response->assertSearchResponse()->assertContainsOnly(['posts' => $this->posts->modelKeys()]);
51+
52+
$response->assertJson([
53+
'meta' => [
54+
'page' => [
55+
'current-page' => 1,
56+
'per-page' => 10,
57+
'from' => 1,
58+
'to' => 4,
59+
'total' => 4,
60+
'last-page' => 1,
61+
],
62+
],
63+
]);
64+
}
65+
4266
public function testPage1()
4367
{
4468
$response = $this->doSearch(['page' => ['number' => 1, 'size' => 3]]);

tests/JsonApi/Posts/Adapter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
class Adapter extends EloquentAdapter
1212
{
1313

14+
/**
15+
* @var array
16+
*/
17+
protected $defaultPagination = [
18+
'number' => 1,
19+
'size' => 10,
20+
];
21+
1422
/**
1523
* Adapter constructor.
1624
*

0 commit comments

Comments
 (0)
0