husail / http-iterator
Http Iterator
v1.0.1
2025-03-17 04:45 UTC
Requires
- php: ^7.4|^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.64
- pestphp/pest: ^2.0
- phpstan/phpstan: ^2.1
README
The HttpIterator
package provides a PHP iterator that simplifies the processing of large volumes of data from Http requests, particularly useful in scenarios with page-based results limitation.
Installation
composer require husail/http-iterator
Basic Usage
use Husail\HttpIterator\HttpIterator; use Illuminate\Support\Facades\Http; HttpIterator::run(10, 1, function ($iterator) { $response = Http::get('http://example.com'); if (!$iterator->hasInitialized()) { $iterator->setTotalResult($response->json('total_elements')); } TransactionRepository::sync($response->json('data')) //... $iterator->nextPage(); });
API Reference
run(int $perPage, int $currentPage, callable $callableRun, callable $callableException)
: Execute the iterator with the provided callables.nextPage()
: Move to the next page.lastPage()
: Move to the last page.toPage(int $page)
: Move to a specific page.setPerPage(int $length)
: Set the number of results per page.setTotalResult(int $length)
: Set the total number of results.currentPage()
: Get the current page.perPage()
: Get the number of results per page.totalResult()
: Get the total number of results.totalPages()
: Get the total number of pages.finish()
: Mark the iterator as finished.initialized()
: Check if the iterator has been initialized.hasFinished()
: Check if the iterator has finished iterating.hasNextPage()
: Check if there is a next page.
License
This package is open-source software licensed under the MIT License - see the LICENSE file for details.