Skip to content

Commit

Permalink
Implement the aggragation of paginations
Browse files Browse the repository at this point in the history
  • Loading branch information
cerbero90 committed Dec 28, 2023
1 parent 682f952 commit ff35519
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 9 deletions.
21 changes: 21 additions & 0 deletions src/Exceptions/UnsupportedPaginationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Cerbero\LazyJsonPages\Exceptions;

use Cerbero\LazyJsonPages\Dtos\Config;

/**
* The exception to throw when a pagination is not supported.
*/
class UnsupportedPaginationException extends LazyJsonPagesException
{
/**
* Instantiate the class.
*/
public function __construct(public readonly Config $config)
{
parent::__construct('The provided configuration does not match with any supported pagination.');
}
}
45 changes: 44 additions & 1 deletion src/Paginations/AnyPagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,50 @@

namespace Cerbero\LazyJsonPages\Paginations;

use Cerbero\LazyJsonPages\Exceptions\UnsupportedPaginationException;
use Traversable;

/**
* The aggregator of paginations.
*/
class AnyPagination extends Pagination
{
//
/**
* The supported paginations.
*
* @var class-string<Pagination>[]
*/
protected array $supportedPaginations = [

Check failure on line 20 in src/Paginations/AnyPagination.php

View workflow job for this annotation

GitHub Actions / Linting

Property Cerbero\LazyJsonPages\Paginations\AnyPagination::$supportedPaginations (array<class-string<Cerbero\LazyJsonPages\Paginations\Pagination>>) does not accept default value of type array<int, string>.
CursorPagination::class,

Check failure on line 21 in src/Paginations/AnyPagination.php

View workflow job for this annotation

GitHub Actions / Linting

Class Cerbero\LazyJsonPages\Paginations\CursorPagination not found.
CustomPagination::class,

Check failure on line 22 in src/Paginations/AnyPagination.php

View workflow job for this annotation

GitHub Actions / Linting

Class Cerbero\LazyJsonPages\Paginations\CustomPagination not found.
LengthAwarePagination::class,

Check failure on line 23 in src/Paginations/AnyPagination.php

View workflow job for this annotation

GitHub Actions / Linting

Class Cerbero\LazyJsonPages\Paginations\LengthAwarePagination not found.
LinkHeaderPagination::class,

Check failure on line 24 in src/Paginations/AnyPagination.php

View workflow job for this annotation

GitHub Actions / Linting

Class Cerbero\LazyJsonPages\Paginations\LinkHeaderPagination not found.
OffsetPagination::class,

Check failure on line 25 in src/Paginations/AnyPagination.php

View workflow job for this annotation

GitHub Actions / Linting

Class Cerbero\LazyJsonPages\Paginations\OffsetPagination not found.
];

/**
* Determine whether the configuration matches this pagination.
*/
public function matches(): bool
{
return true;
}

/**
* Yield the paginated items.
*
* @return Traversable<string|int, mixed>
*/
public function getIterator(): Traversable
{
foreach ($this->supportedPaginations as $class) {
$pagination = new $class($this->source, $this->config);

if ($pagination->matches()) {
return $pagination;
}
}

throw new UnsupportedPaginationException($this->config);
}
}
22 changes: 14 additions & 8 deletions src/Paginations/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,26 @@
use Traversable;

/**
* The abstract implementation of a pagination.
*
* @implements IteratorAggregate<string|int, mixed>
*/
abstract class Pagination implements IteratorAggregate
{
final public function __construct(
protected readonly AnySource $source,
protected readonly Config $config,
) {}
/**
* Determine whether the configuration matches this pagination.
*/
abstract public function matches(): bool;

/**
* Yield the paginated items.
*
* @return Traversable<string|int, mixed>
*/
public function getIterator(): Traversable
{
yield 1;
}
abstract public function getIterator(): Traversable;

final public function __construct(
protected readonly AnySource $source,
protected readonly Config $config,
) {}
}

0 comments on commit ff35519

Please sign in to comment.