diff --git a/src/Endpoints/CollectionEndpointAbstract.php b/src/Endpoints/CollectionEndpointAbstract.php index 81e05787..65987623 100644 --- a/src/Endpoints/CollectionEndpointAbstract.php +++ b/src/Endpoints/CollectionEndpointAbstract.php @@ -56,32 +56,7 @@ protected function rest_iterator(?string $from = null, ?int $limit = null, array /** @var CursorCollection $page */ $page = $this->rest_list($from, $limit, $filters); - return $this->iterate($page, $iterateBackwards); - } - - /** - * Iterate over a CursorCollection and yield its elements. - * - * @param CursorCollection $page - * @param bool $iterateBackwards - * - * @return Generator - */ - protected function iterate(CursorCollection $page, bool $iterateBackwards = false): Generator - { - while (true) { - foreach ($page as $item) { - yield $item; - } - - if (($iterateBackwards && ! $page->hasPrevious()) || ! $page->hasNext()) { - break; - } - - $page = $iterateBackwards - ? $page->previous() - : $page->next(); - } + return $page->getAutoIterator($iterateBackwards); } /** diff --git a/src/Endpoints/SubscriptionEndpoint.php b/src/Endpoints/SubscriptionEndpoint.php index 690992fb..06930027 100644 --- a/src/Endpoints/SubscriptionEndpoint.php +++ b/src/Endpoints/SubscriptionEndpoint.php @@ -259,6 +259,6 @@ public function iterator(?string $from = null, ?int $limit = null, array $parame { $page = $this->page($from, $limit, $parameters); - return $this->iterate($page, $iterateBackwards); + return $page->getAutoIterator($iterateBackwards); } } diff --git a/src/Resources/CursorCollection.php b/src/Resources/CursorCollection.php index e3265335..36e4d40d 100644 --- a/src/Resources/CursorCollection.php +++ b/src/Resources/CursorCollection.php @@ -2,6 +2,7 @@ namespace Mollie\Api\Resources; +use Generator; use Mollie\Api\MollieApiClient; abstract class CursorCollection extends BaseCollection @@ -36,7 +37,7 @@ abstract protected function createResourceObject(); */ final public function next() { - if (! $this->hasNext()) { + if (!$this->hasNext()) { return null; } @@ -59,7 +60,7 @@ final public function next() */ final public function previous() { - if (! $this->hasPrevious()) { + if (!$this->hasPrevious()) { return null; } @@ -93,4 +94,30 @@ public function hasPrevious() { return isset($this->_links->previous->href); } + + /** + * Iterate over a CursorCollection and yield its elements. + * + * @param boolean $iterateBackwards + * + * @return Generator + */ + public function getAutoIterator(bool $iterateBackwards = false): Generator + { + $page = $this; + + while (true) { + foreach ($page as $item) { + yield $item; + } + + if (($iterateBackwards && !$page->hasPrevious()) || !$page->hasNext()) { + break; + } + + $page = $iterateBackwards + ? $page->previous() + : $page->next(); + } + } }