8000 [Refactor] Update page query parameters to use value object · alescx/laravel-json-api-core@d466004 · GitHub
[go: up one dir, main page]

Skip to content

Commit d466004

Browse files
committed
[Refactor] Update page query parameters to use value object
1 parent 1125457 commit d466004

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,23 @@ All notable changes to this project will be documented in this file. This projec
55

66
## Unreleased
77

8+
### Changed
9+
10+
- **BREAKING** Removed the iterable type-hint from the `Contracts\Pagination\Page::withQuery()` method. The value passed
11+
can be any value that can be cast to a `Core\Query\QueryParameters` object. This change also affects the
12+
`Core\Pagination\AbstactPage::withQuery()` method, that has been updated to remove the type-hint. This will affect any
13+
child classes that have overloaded this method.
14+
- **BREAKING** Remove the iterable type-hint from the `Core\Resources\ResourceCollection::withQuery()` method. The value
15+
passed can be any value that can be cast to a `Core\Query\QueryParameters` object.
16+
817
### Fixed
918

1019
- Ensure internal response classes all consistently use the `links()` method when passing links to the encoder. This
1120
fixes a bug whereby pagination links were not added to the compound document for related resources and relationship
1221
identifier responses.
22+
- Query parameters passed to the abstract page object were not correctly encoding to a query string. This is because the
23+
`collect()` method was being used, which meant `QueryParameters::toArray()` would be used to serialize query
24+
parameters. This has now been updated to use `QueryParameters::toQuery()` instead, which is the correct method to use.
1325

1426
## [1.0.0-beta.3] - 2021-04-26
1527

src/Contracts/Pagination/Page.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Countable;
2323
use Illuminate\Contracts\Support\Responsable;
2424
use IteratorAggregate;
25+
use LaravelJsonApi\Contracts\Query\QueryParameters;
2526
use LaravelJsonApi\Core\Document\Links;
2627

2728
interface Page extends IteratorAggregate, Countable, Responsable
@@ -44,8 +45,8 @@ public function links(): Links;
4445
/**
4546
* Specify the query string parameters that should be present on pagination links.
4647
*
47-
* @param iterable $query
48+
* @param QueryParameters|array|mixed $query
4849
* @return $this
4950
*/
50-
public function withQuery(iterable $query): self;
51+
public function withQuery($query): self;
5152
}

src/Core/Pagination/AbstractPage.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@
2020
namespace LaravelJsonApi\Core\Pagination;
2121

2222
use Illuminate\Support\Arr;
23+
use Illuminate\Support\Collection;
2324
use LaravelJsonApi\Contracts\Pagination\Page as PageContract;
2425
use LaravelJsonApi\Core\Document\Link;
2526
use LaravelJsonApi\Core\Document\Links;
2627
use LaravelJsonApi\Core\Json\Hash;
28+
use LaravelJsonApi\Core\Query\QueryParameters;
2729
use LaravelJsonApi\Core\Responses\Internal\PaginatedResourceResponse;
30+
use function ksort;
2831

2932
abstract class AbstractPage implements PageContract
3033
{
@@ -53,9 +56,9 @@ abstract class AbstractPage implements PageContract
5356
/**
5457
* The query parameters that must be used for links.
5558
*
56-
* @var array|null
59+
* @var QueryParameters|null
5760
*/
58-
private ?array $queryParameters = null;
61+
private ?QueryParameters $queryParameters = null;
5962

6063
/**
6164
* Get the link to the first page.
@@ -177,9 +180,9 @@ public function withMeta(bool $bool): self
177180
/**
178181
* @inheritDoc
179182
*/
180-
public function withQuery(iterable $query): PageContract
183+
public function withQuery($query): PageContract
181184
{
182-
$this->queryParameters = collect($query)->all();
185+
$this->queryParameters = QueryParameters::cast($query);
183186

184187
return $this;
185188
}
@@ -246,7 +249,7 @@ protected function buildQuery(array $page): array
246249
{
247250
ksort($page);
248251

249-
return collect($this->queryParameters)
252+
return Collection::make($this->queryParameters ? $this->queryParameters->toQuery() : [])
250253
->put('page', $page)
251254
->sortKeys()
252255
->all();

src/Core/Resources/ResourceCollection.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use LaravelJsonApi\Contracts\Pagination\Page as PageContract;
2828
use LaravelJsonApi\Core\Document\Links;
2929
use LaravelJsonApi\Core\Pagination\Page;
30+
use LaravelJsonApi\Core\Query\QueryParameters;
3031
use LaravelJsonApi\Core\Responses\Internal\PaginatedResourceResponse;
3132
use LaravelJsonApi\Core\Responses\Internal\ResourceCollectionResponse;
3233
use function count;
@@ -45,9 +46,9 @@ class ResourceCollection implements Responsable, IteratorAggregate, Countable
4546
protected bool $preserveAllQueryParameters = false;
4647

4748
/**
48-
* @var array|null
49+
* @var QueryParameters|null
4950
*/
50-
protected ?array $queryParameters = null;
51+
protected ?QueryParameters $queryParameters = null;
5152

5253
/**
5354
* ResourceCollection constructor.
@@ -74,13 +75,13 @@ public function preserveQuery(): self
7475
/**
7576
* Specify the query string parameters that should be present on pagination links.
7677
*
77-
* @param iterable $query
78+
* @param mixed $query
7879
* @return $this
7980
*/
80-
public function withQuery(iterable $query): self
81+
public function withQuery($query): self
8182
{
8283
$this->preserveAllQueryParameters = false;
83-
$this->queryParameters = \collect($query)->all();
84+
$this->queryParameters = QueryParameters::cast($query);
8485

8586
return $this;
8687
}
@@ -161,7 +162,7 @@ protected function preparePaginationResponse($request): ResourceCollectionRespon
161162

162163
if ($this->preserveAllQueryParameters) {
163164
$this->resources->withQuery($request->query());
164-
} else if (\is_array($this->queryParameters)) {
165+
} else if ($this->queryParameters) {
165166
$this->resources->withQuery($this->queryParameters);
166167
}
167168

0 commit comments

Comments
 (0)
0