mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-08 16:23:12 +08:00
Updated VisitRepository::findUnlocatedVisits methods so that it paginates the amount of elements loaded in memory
This commit is contained in:
38
module/Common/src/Paginator/ImplicitLoopPaginator.php
Normal file
38
module/Common/src/Paginator/ImplicitLoopPaginator.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Common\Paginator;
|
||||
|
||||
use IteratorAggregate;
|
||||
use Zend\Paginator\Paginator;
|
||||
|
||||
class ImplicitLoopPaginator implements IteratorAggregate
|
||||
{
|
||||
/** @var Paginator */
|
||||
private $paginator;
|
||||
/** @var callable */
|
||||
private $valueParser;
|
||||
|
||||
public function __construct(Paginator $paginator, callable $valueParser = null)
|
||||
{
|
||||
$this->paginator = $paginator;
|
||||
$this->valueParser = $valueParser ?? function ($value) {
|
||||
return $value;
|
||||
};
|
||||
}
|
||||
|
||||
public function getIterator(): iterable
|
||||
{
|
||||
$totalPages = $this->paginator->count();
|
||||
$processedPages = 0;
|
||||
|
||||
do {
|
||||
$processedPages++;
|
||||
$this->paginator->setCurrentPageNumber($processedPages);
|
||||
|
||||
foreach ($this->paginator as $key => $value) {
|
||||
yield $key => ($this->valueParser)($value);
|
||||
}
|
||||
} while ($processedPages < $totalPages);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user