husail / http-iterator
Http Iterator
v1.0.0
2024-05-16 22:44 UTC
Requires
- php: ^7.4|^8.0
Requires (Dev)
- pestphp/pest: ^2.0
README
《HttpIterator》包提供了一个PHP迭代器,简化了从Http请求处理大量数据,尤其在分页结果限制的场合非常有用。
安装
composer require husail/http-iterator
基本用法
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参考
run(int $perPage, int $currentPage, callable $callableRun, callable $callableException)
:使用提供的回调函数执行迭代器。nextPage()
:转到下一页。lastPage()
:转到最后一页。toPage(int $page)
:转到指定页面。setPerPage(int $length)
:设置每页的结果数量。setTotalResult(int $length)
:设置总结果数量。currentPage()
:获取当前页。perPage()
:获取每页结果数量。totalResult()
:获取总结果数量。totalPages()
:获取总页数。finish()
:标记迭代器为完成。initialized()
:检查迭代器是否已初始化。hasFinished()
:检查迭代器是否已完成迭代。hasNextPage()
:检查是否有下一页。
许可证
本软件包是开源软件,采用MIT许可证,详情请参阅LICENSE文件。