diff --git a/src/Exceptions/UnsupportedPaginationException.php b/src/Exceptions/UnsupportedPaginationException.php new file mode 100644 index 0000000..5cfb6ee --- /dev/null +++ b/src/Exceptions/UnsupportedPaginationException.php @@ -0,0 +1,21 @@ +[] + */ + protected array $supportedPaginations = [ + CursorPagination::class, + CustomPagination::class, + LengthAwarePagination::class, + LinkHeaderPagination::class, + OffsetPagination::class, + ]; + + /** + * Determine whether the configuration matches this pagination. + */ + public function matches(): bool + { + return true; + } + + /** + * Yield the paginated items. + * + * @return Traversable + */ + 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); + } } diff --git a/src/Paginations/Pagination.php b/src/Paginations/Pagination.php index 2c19a3e..b66424a 100644 --- a/src/Paginations/Pagination.php +++ b/src/Paginations/Pagination.php @@ -10,20 +10,26 @@ use Traversable; /** + * The abstract implementation of a pagination. + * * @implements IteratorAggregate */ 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 */ - public function getIterator(): Traversable - { - yield 1; - } + abstract public function getIterator(): Traversable; + + final public function __construct( + protected readonly AnySource $source, + protected readonly Config $config, + ) {} }