发布推送
This commit is contained in:
82
app/Tools/CollectOffsetLimit.php
Normal file
82
app/Tools/CollectOffsetLimit.php
Normal file
@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Tools;
|
||||
|
||||
class CollectOffsetLimit
|
||||
{
|
||||
private $offset = 0;
|
||||
private $limit = 2000;
|
||||
private $page = 0;
|
||||
|
||||
private $iTotalCount = null;
|
||||
|
||||
public $fCountFun;
|
||||
|
||||
|
||||
function setCountFun(\Closure $fun)
|
||||
{
|
||||
$this->fCountFun = $fun();
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
private function countPage()
|
||||
{
|
||||
if($this->iTotalCount === null && $this->fCountFun !== null){
|
||||
$this->iTotalCount = $this->fCountFun;
|
||||
}
|
||||
if(empty($this->page) && !empty($this->iTotalCount)){
|
||||
$this->page = ceil($this->iTotalCount / $this->limit);
|
||||
}
|
||||
}
|
||||
|
||||
function runWhile(\Closure $fun)
|
||||
{
|
||||
$this->countPage();
|
||||
if($this->iTotalCount === null) $this->iTotalCount = $this->fCountFun;
|
||||
if($this->iTotalCount === 0 | $this->page === 0) return [];
|
||||
for( $iPage = 1; $iPage <= $this->page; $iPage++){
|
||||
$this->offset = ($iPage - 1) * $this->limit;
|
||||
$fun($this->offset,$this->limit);
|
||||
}
|
||||
$this->offset = 0;
|
||||
|
||||
}
|
||||
|
||||
public function getLimit(): int
|
||||
{
|
||||
return $this->limit;
|
||||
}
|
||||
|
||||
public function setLimit(int $limit): void
|
||||
{
|
||||
$this->limit = $limit;
|
||||
}
|
||||
|
||||
public function getOffset(): int
|
||||
{
|
||||
return $this->offset;
|
||||
}
|
||||
|
||||
public function setOffset(int $offset): void
|
||||
{
|
||||
$this->offset = $offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return null
|
||||
*/
|
||||
public function getITotalCount()
|
||||
{
|
||||
return $this->iTotalCount;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param null $iTotalCount
|
||||
*/
|
||||
public function setITotalCount($iTotalCount): CollectOffsetLimit
|
||||
{
|
||||
$this->iTotalCount = $iTotalCount;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user