Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add auto pagination iterator #701

Merged
merged 10 commits into from
Nov 6, 2023

Conversation

Naoray
Copy link
Collaborator

@Naoray Naoray commented Oct 26, 2023

This PR adds new iterator methods which return Generators in order to simply loop over all results from provided by the Mollie API. It takes a similar approach as the basic implementation of the iteration API on the Mollie NodeJS repo.

Essentially all endpoints that extend the CollectionEndpointAbstract and provide a Collection that extends CursorCollection (all that support cursor pagination) have been given iterator() methods (or variations of it). The iterator() methods take the same params as the page() method (or variations of the page method).

Here is an overview of how the iteration methods map to previous existing page like methods:

  • page() -> iterator()
  • listFor() -> iteratorFor()
  • listForId() -> iteratorForId()

Basic Usage

// instead of doing
$page = $client->orders->page();

while ($page->hasNext()) {
    foreach ($page as $order) {
        echo($order->id);
    }

    $page = $page->next();
}

// we can now loop over all orders directly
foreach ($client->orders->iterator() as $order) {
    echo($order->id);
}

Backwards looping

// instead of doing
$page = $client->orders->page(’some_order_id');

while ($page->hasPrevious()) {
    foreach ($page as $order) {
        echo($order->id);
    }

    $page = $page->previous();
}

// passing iterateBackwards: true or simply using the fourth param will iterate backwards 
foreach ($client->orders->iterator(iterateBackwards: true) as $order) {
    echo($order->id);
}

@Naoray Naoray requested a review from sandervanhooft October 26, 2023 10:21
@fjbender
Copy link
Contributor

Nice!

Named arguments would require PHP 8.0 though, right? It's just the example you used here, but still worth noting.

Additionally, having those examples in the examples/ directory would be nice IMHO.

@Naoray
Copy link
Collaborator Author

Naoray commented Oct 27, 2023

Nice!

Named arguments would require PHP 8.0 though, right? It's just the example you used here, but still worth noting.

Additionally, having those examples in the examples/ directory would be nice IMHO.

@fjbender yes the named arguments is just an example. You could also use the functionality like

foreach ($client->orders->iterator(null, null, [], true) as $order) {
    echo($order->id);
}

I’ve added the code to the examples.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants