Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- use yii\data\Pagination;
- /**
- * In base model class need to add method paginate
- *
- * public function paginate($perPage = 15, $page = null)
- * {
- * return new PaginationCollection($this, $perPage, $page);
- * }
- */
- class PaginationCollection implements \Iterator, \ArrayAccess
- {
- /**
- * result rows
- *
- * @access protected
- * @var array
- */
- protected $result = [];
- /**
- * Per page results
- *
- * @access protected
- * @var int
- */
- protected $perPage = 15;
- /**
- * Current page
- *
- * @access protected
- * @var int
- */
- protected $page = 1;
- /**
- * Count results
- *
- * @access protected
- * @var int
- */
- protected $count = 0;
- /**
- * Pagination object
- *
- * @access protected
- * @var object
- */
- protected $pagination;
- /**
- * Constructor
- *
- * @access public
- * @param object $query
- * @param int $perPage
- * @param int $page
- */
- public function __construct($query, $perPage, $page)
- {
- $this->page = $this->getRequestPage($page);
- $this->perPage = (int) $perPage;
- $this->count = $query->count();
- $this->pagination = $this->createPagination();
- $this->result = $query
- ->offset($this->pagination->offset)
- ->limit($this->pagination->limit)
- ->all();
- }
- /**
- * Getter
- *
- * @access public
- * @param mixed $key
- * @return mixed
- */
- public function __get($key)
- {
- return isset($this->result[$key]) ? $this->result[$key] : null;
- }
- /**
- * Setter
- *
- * @access public
- * @param string $key
- * @param mixed $value
- */
- public function __set($key, $value)
- {
- $this->result[$key] = $value;
- }
- /**
- * Magic isset
- *
- * @access public
- * @param string $key
- * @return bool
- */
- public function __isset($key)
- {
- return isset($this->result[$key]);
- }
- /**
- * Magic unset
- *
- * @access public
- * @param string $key
- */
- public function __unset($key)
- {
- if ($this->__isset($key)) {
- unset($this->result[$key]);
- }
- }
- /**
- * Invoke method, get pagination
- *
- * @access public
- * @return object
- */
- public function __invoke()
- {
- return $this->getPagination();
- }
- /**
- * Get pagination object
- *
- * @access public
- * @return object
- */
- public function getPagination()
- {
- if (! $this->pagination) {
- $this->pagination = $this->createPagination();
- }
- return $this->pagination;
- }
- /**
- * Iterator rewind
- *
- * @access public
- * @return void
- */
- public function rewind()
- {
- reset($this->result);
- }
- /**
- * Iterator current element
- *
- * @access public
- * @return mixed
- */
- public function current()
- {
- return current($this->result);
- }
- /**
- * Iterator key
- *
- * @access public
- * @return string
- */
- public function key()
- {
- return key($this->result);
- }
- /**
- * Iterator next element
- *
- * @access public
- * @return mixed
- */
- public function next()
- {
- return next($this->result);
- }
- /**
- * Iterator valid
- *
- * @return bool
- */
- public function valid()
- {
- $key = key($this->result);
- return ($key !== NULL && $key !== FALSE);
- }
- /**
- * ArrayAccess exists
- *
- * @access public
- * @param string $key
- * @return bool
- */
- public function offsetExists($key)
- {
- return $this->__isset($key);
- }
- /**
- * ArrayAccess get element
- *
- * @access public
- * @param string $key
- * @return mixed
- */
- public function offsetGet($key)
- {
- return $this->__get($key);
- }
- /**
- * ArrayAccess set
- *
- * @access public
- * @param string $key
- * @param mixed $value
- */
- public function offsetSet($key, $value)
- {
- return $this->__set($key, $value);
- }
- /**
- * ArrayAcess unset
- *
- * @access public
- * @param string $key
- * @return void
- */
- public function offsetUnset($key)
- {
- return $this->__unset($key);
- }
- /**
- * Create pagination
- *
- * @access protected
- * @return object
- */
- protected function createPagination()
- {
- return new Pagination(['pageSize' => $this->perPage, 'page' => $this->page, 'totalCount' => $this->count]);
- }
- /**
- * Get request page
- *
- * @access protected
- * @param int $page
- * @return int
- */
- protected function getRequestPage($page)
- {
- if (! $page) {
- $page = (isset($_REQUEST['page'])) ? $_REQUEST['page'] : 1;
- }
- return (int) $page * 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement