Whilst I definitely like the idea of the ->next() method, I think this could be simplified/improved such that "consumers" can just loop over all the records without having to worry about the pagination. We've used similar approaches where we would expose service classes that returned the results and expose methods for checking/getting the next page.
Rather than wrapping the foreach in a do/while loop, what if we could just loop the all results?
As a simple example, let's create a service class to fetch Pokémon from the PokeAPI: 1 ($page - 1) * 20, 15 'limit' => 20, 16 ]); 17 18 foreach ($response['results'] as $result) { 19 yield $result; 20 }
By using the Generator returned from the Pokemon class, we can loop over all the Pokémon without needing to manually fetch further pages.