generated from cerbero90/skeleton
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement link header aware pagination
- Loading branch information
Showing
7 changed files
with
153 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Cerbero\LazyJsonPages\Paginations; | ||
|
||
use Cerbero\LazyJsonPages\Concerns\YieldsItemsByCursor; | ||
use Cerbero\LazyJsonPages\Concerns\YieldsItemsByLength; | ||
use Cerbero\LazyJsonPages\Exceptions\InvalidLinkHeaderException; | ||
use Generator; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Traversable; | ||
|
||
/** | ||
* The pagination using a Link header. | ||
*/ | ||
class LinkHeaderAwarePagination extends Pagination | ||
{ | ||
use YieldsItemsByCursor; | ||
use YieldsItemsByLength; | ||
|
||
/** | ||
* The Link header format. | ||
*/ | ||
public const FORMAT = '~<\s*(?<uri>[^\s>]+)\s*>.*?"\s*(?<rel>[^\s"]+)\s*"~'; | ||
|
||
/** | ||
* Determine whether the configuration matches this pagination. | ||
*/ | ||
public function matches(): bool | ||
{ | ||
return $this->config->hasLinkHeader | ||
&& $this->config->totalItemsKey === null | ||
&& $this->config->totalPagesKey === null | ||
&& $this->config->lastPageKey === null; | ||
} | ||
|
||
/** | ||
* Yield the paginated items. | ||
* | ||
* @return Traversable<int, mixed> | ||
* @throws InvalidLinkHeaderException | ||
*/ | ||
public function getIterator(): Traversable | ||
{ | ||
$links = $this->parseLinkHeader($this->source->response()->getHeaderLine('link')); | ||
|
||
yield from match (true) { | ||
isset($links['last']) => $this->yieldItemsByLastPage($links['last']), | ||
isset($links['next']) => $this->yieldItemsByNextLink(), | ||
default => $this->yieldItemsFrom($this->source->pullResponse()), | ||
}; | ||
} | ||
|
||
/** | ||
* Retrieve the parsed Link header. | ||
* | ||
* @template TParsed of array{last?: int, next?: string|int} | ||
* @template TRelation of string|null | ||
* | ||
* @param TRelation $relation | ||
* @return (TRelation is null ? TParsed : string|int|null) | ||
*/ | ||
protected function parseLinkHeader(string $linkHeader, ?string $relation = null): array|string|int|null | ||
{ | ||
$links = []; | ||
|
||
preg_match_all(static::FORMAT, $linkHeader, $matches, PREG_SET_ORDER); | ||
|
||
foreach ($matches as $match) { | ||
$links[$match['rel']] = $this->toPage($match['uri'], $match['rel'] != 'next'); | ||
} | ||
|
||
return $relation ? ($links[$relation] ?? null) : $links; | ||
} | ||
|
||
/** | ||
* Yield the paginated items by the given last page. | ||
* | ||
* @return Generator<int, mixed> | ||
*/ | ||
protected function yieldItemsByLastPage(int $lastPage): Generator | ||
{ | ||
yield from $this->yieldItemsUntilPage(function(ResponseInterface $response) use ($lastPage) { | ||
yield from $this->yieldItemsFrom($response); | ||
|
||
return $this->config->firstPage === 0 ? $lastPage + 1 : $lastPage; | ||
}); | ||
} | ||
|
||
/** | ||
* Yield the paginated items by the given next link. | ||
* | ||
* @return Generator<int, mixed> | ||
*/ | ||
protected function yieldItemsByNextLink(): Generator | ||
{ | ||
yield from $this->yieldItemsByCursor(function(ResponseInterface $response) { | ||
yield from $this->yieldItemsFrom($response); | ||
|
||
return $this->parseLinkHeader($response->getHeaderLine('link'), 'next'); | ||
}); | ||
} | ||
} |
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