<?php<448901948@qq.com>
namespace think\paginator;
use Exception;
use think\Paginator;
class Collection extends \think\Collection
{
protected $paginator;
public function __construct($items = [], Paginator $paginator = null)
{
$this->paginator = $paginator;
parent::__construct($items);
}
public static function make($items = [], Paginator $paginator = null)
{
return new static($items, $paginator);
}
public function toArray()
{
if ($this->paginator) {
try {
$total = $this->total();
} catch (Exception $e) {
$total = null;
}
return [
'total' => $total,
'per_page' => $this->listRows(),
'current_page' => $this->currentPage(),
'data' => parent::toArray(),
];
} else {
return parent::toArray();
}
}
public function __call($method, $args)
{
if ($this->paginator && method_exists($this->paginator, $method)) {
return call_user_func_array([$this->paginator, $method], $args);
} else {
throw new Exception('method not exists:' . __CLASS__ . '->' . $method);
}
}
}