From cf6836b56d141c3345974f8d3c6ba738439c1c09 Mon Sep 17 00:00:00 2001 From: yunwuxin <448901948@qq.com> Date: Sun, 15 May 2016 10:37:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E5=88=86=E9=A1=B5=E7=B1=BB?= =?UTF-8?q?=E7=AE=80=E6=B4=81=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/Paginator.php | 10 ++++--- library/think/paginator/Collection.php | 40 ++++++++++++++++---------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/library/think/Paginator.php b/library/think/Paginator.php index 4f1772ae..e891798f 100644 --- a/library/think/Paginator.php +++ b/library/think/Paginator.php @@ -53,12 +53,13 @@ abstract class Paginator $this->simple = $simple; $this->listRows = $listRows; - $this->items = PaginatorCollection::make($items, $this); - if ($simple) { + if (!$items instanceof Collection) { + $items = Collection::make($items); + } $this->currentPage = $this->setCurrentPage($currentPage); - $this->hasMore = count($this->items) > ($this->listRows); - $this->items = $this->items->slice(0, $this->listRows); + $this->hasMore = count($items) > ($this->listRows); + $items = $items->slice(0, $this->listRows); } else { $this->total = $total; $this->lastPage = (int)ceil($total / $listRows); @@ -66,6 +67,7 @@ abstract class Paginator $this->hasMore = $this->currentPage < $this->lastPage; } + $this->items = PaginatorCollection::make($items, $this); } public function items() diff --git a/library/think/paginator/Collection.php b/library/think/paginator/Collection.php index a7939a86..49bd59c0 100644 --- a/library/think/paginator/Collection.php +++ b/library/think/paginator/Collection.php @@ -32,9 +32,6 @@ class Collection extends \think\Collection public function __construct($items = [], Paginator $paginator = null) { - if (!$paginator instanceof Paginator) { - throw new \RuntimeException('Paginator Required!'); - } $this->paginator = $paginator; parent::__construct($items); } @@ -44,26 +41,39 @@ class Collection extends \think\Collection return new static($items, $paginator); } + public function setPaginator(Paginator $paginator) + { + $this->paginator = $paginator; + } + + public function getPaginator() + { + return $this->paginator; + } public function toArray() { - try { - $total = $this->total(); - } catch (Exception $e) { - $total = null; - } + 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() - ]; + return [ + 'total' => $total, + 'per_page' => $this->listRows(), + 'current_page' => $this->currentPage(), + 'data' => parent::toArray() + ]; + } else { + return parent::toArray(); + } } public function __call($method, $args) { - if (method_exists($this->paginator, $method)) { + if ($this->paginator && method_exists($this->paginator, $method)) { return call_user_func_array([$this->paginator, $method], $args); } else { throw new Exception(__CLASS__ . ':' . $method . ' method not exist');