Overview

Namespaces

  • Deimos
    • Paginate
  • PHP

Classes

  • Deimos\Paginate\Pager
  • Deimos\Paginate\Paginate

Interfaces

  • Throwable

Exceptions

  • Exception
  • InvalidArgumentException
  • LogicException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: 
  3: namespace Deimos\Paginate;
  4: 
  5: abstract class Pager
  6: {
  7: 
  8:     /**
  9:      * @var int
 10:      */
 11:     protected $defaultTake = 50;
 12: 
 13:     /**
 14:      * @var array
 15:      */
 16:     protected $storage;
 17: 
 18:     /**
 19:      * @var int
 20:      */
 21:     protected $itemCount;
 22: 
 23:     /**
 24:      * @var int
 25:      */
 26:     protected $limit;
 27: 
 28:     /**
 29:      * @var int
 30:      */
 31:     protected $page;
 32: 
 33:     /**
 34:      * @var bool
 35:      */
 36:     protected $loaded;
 37: 
 38:     /**
 39:      * reset pager
 40:      */
 41:     protected function reset()
 42:     {
 43:         $this->loaded    = false;
 44:         $this->storage   = null;
 45:         $this->itemCount = null;
 46:         $this->limit = $this->defaultTake;
 47:         $this->page = 1;
 48:     }
 49: 
 50:     /**
 51:      * @return array
 52:      */
 53:     protected function slice()
 54:     {
 55:         return array_slice($this->storage, $this->offset(), $this->limit);
 56:     }
 57: 
 58:     /**
 59:      * @return int
 60:      */
 61:     public abstract function itemCount();
 62: 
 63:     /**
 64:      * @return bool
 65:      */
 66:     protected function isLoaded()
 67:     {
 68:         return $this->loaded === true;
 69:     }
 70: 
 71:     /**
 72:      * @return int
 73:      */
 74:     public function pageCount()
 75:     {
 76:         return (int)ceil($this->itemCount() / $this->limit);
 77:     }
 78: 
 79:     /**
 80:      * @param int $page
 81:      *
 82:      * @return bool
 83:      */
 84:     public function pageExists($page)
 85:     {
 86:         return $page > 0 && $this->pageCount() >= $page;
 87:     }
 88: 
 89:     /**
 90:      * @return int
 91:      */
 92:     public function currentPage()
 93:     {
 94:         if (!$this->offset())
 95:         {
 96:             return 1;
 97:         }
 98: 
 99:         return (int)($this->offset() / $this->limit + 1);
100:     }
101: 
102:     /**
103:      * @param int $page
104:      *
105:      * @return static
106:      */
107:     public function page($page)
108:     {
109:         $this->page = abs($page);
110: 
111:         return $this;
112:     }
113: 
114:     /**
115:      * @param int $limit
116:      *
117:      * @return static
118:      */
119:     public function limit($limit)
120:     {
121:         $this->limit = $limit;
122: 
123:         return $this;
124:     }
125: 
126:     /**
127:      * @return int
128:      */
129:     protected function offset()
130:     {
131:         return ($this->page - 1) * $this->limit;
132:     }
133: 
134: }
API documentation generated by ApiGen