SHOW:
|
|
- or go back to the newest paste.
| 1 | <?php | |
| 2 | use yii\data\Pagination; | |
| 3 | ||
| 4 | /** | |
| 5 | * In base model class need to add method paginate | |
| 6 | * | |
| 7 | * public function paginate($perPage = 15, $page = null) | |
| 8 | * {
| |
| 9 | * return new PaginationCollection($this, $perPage, $page); | |
| 10 | * } | |
| 11 | */ | |
| 12 | ||
| 13 | class PaginationCollection implements \Iterator, \ArrayAccess | |
| 14 | {
| |
| 15 | /** | |
| 16 | * result rows | |
| 17 | * | |
| 18 | * @access protected | |
| 19 | * @var array | |
| 20 | */ | |
| 21 | protected $result = []; | |
| 22 | ||
| 23 | /** | |
| 24 | * Per page results | |
| 25 | * | |
| 26 | * @access protected | |
| 27 | * @var int | |
| 28 | */ | |
| 29 | protected $perPage = 15; | |
| 30 | ||
| 31 | /** | |
| 32 | * Current page | |
| 33 | * | |
| 34 | * @access protected | |
| 35 | * @var int | |
| 36 | */ | |
| 37 | protected $page = 1; | |
| 38 | ||
| 39 | /** | |
| 40 | * Count results | |
| 41 | * | |
| 42 | * @access protected | |
| 43 | * @var int | |
| 44 | */ | |
| 45 | protected $count = 0; | |
| 46 | ||
| 47 | /** | |
| 48 | * Pagination object | |
| 49 | * | |
| 50 | * @access protected | |
| 51 | * @var object | |
| 52 | */ | |
| 53 | protected $pagination; | |
| 54 | ||
| 55 | /** | |
| 56 | * Constructor | |
| 57 | * | |
| 58 | * @access public | |
| 59 | * @param object $query | |
| 60 | * @param int $perPage | |
| 61 | * @param int $page | |
| 62 | */ | |
| 63 | public function __construct($query, $perPage, $page) | |
| 64 | {
| |
| 65 | $this->page = $this->getRequestPage($page); | |
| 66 | $this->perPage = (int) $perPage; | |
| 67 | $this->count = $query->count(); | |
| 68 | $this->pagination = $this->createPagination(); | |
| 69 | $this->result = $query | |
| 70 | ->offset($this->pagination->offset) | |
| 71 | - | ->limit($this->pagination->limit) |
| 71 | + | ->limit($this->pagination->limit) |
| 72 | - | ->all(); |
| 72 | + | ->all(); |
| 73 | } | |
| 74 | ||
| 75 | /** | |
| 76 | * Getter | |
| 77 | * | |
| 78 | * @access public | |
| 79 | * @param mixed $key | |
| 80 | * @return mixed | |
| 81 | */ | |
| 82 | public function __get($key) | |
| 83 | {
| |
| 84 | return isset($this->result[$key]) ? $this->result[$key] : null; | |
| 85 | } | |
| 86 | ||
| 87 | /** | |
| 88 | * Setter | |
| 89 | * | |
| 90 | * @access public | |
| 91 | * @param string $key | |
| 92 | * @param mixed $value | |
| 93 | */ | |
| 94 | public function __set($key, $value) | |
| 95 | {
| |
| 96 | $this->result[$key] = $value; | |
| 97 | } | |
| 98 | ||
| 99 | /** | |
| 100 | * Magic isset | |
| 101 | * | |
| 102 | * @access public | |
| 103 | * @param string $key | |
| 104 | * @return bool | |
| 105 | */ | |
| 106 | public function __isset($key) | |
| 107 | {
| |
| 108 | return isset($this->result[$key]); | |
| 109 | } | |
| 110 | ||
| 111 | /** | |
| 112 | * Magic unset | |
| 113 | * | |
| 114 | * @access public | |
| 115 | * @param string $key | |
| 116 | */ | |
| 117 | public function __unset($key) | |
| 118 | {
| |
| 119 | if ($this->__isset($key)) {
| |
| 120 | unset($this->result[$key]); | |
| 121 | } | |
| 122 | } | |
| 123 | ||
| 124 | /** | |
| 125 | * Invoke method, get pagination | |
| 126 | * | |
| 127 | * @access public | |
| 128 | * @return object | |
| 129 | */ | |
| 130 | public function __invoke() | |
| 131 | {
| |
| 132 | return $this->getPagination(); | |
| 133 | } | |
| 134 | ||
| 135 | /** | |
| 136 | * Get pagination object | |
| 137 | * | |
| 138 | * @access public | |
| 139 | * @return object | |
| 140 | */ | |
| 141 | public function getPagination() | |
| 142 | {
| |
| 143 | if (! $this->pagination) {
| |
| 144 | $this->pagination = $this->createPagination(); | |
| 145 | } | |
| 146 | return $this->pagination; | |
| 147 | } | |
| 148 | ||
| 149 | /** | |
| 150 | * Iterator rewind | |
| 151 | * | |
| 152 | * @access public | |
| 153 | * @return void | |
| 154 | */ | |
| 155 | public function rewind() | |
| 156 | {
| |
| 157 | reset($this->result); | |
| 158 | } | |
| 159 | ||
| 160 | /** | |
| 161 | * Iterator current element | |
| 162 | * | |
| 163 | * @access public | |
| 164 | * @return mixed | |
| 165 | */ | |
| 166 | public function current() | |
| 167 | {
| |
| 168 | return current($this->result); | |
| 169 | } | |
| 170 | ||
| 171 | /** | |
| 172 | * Iterator key | |
| 173 | * | |
| 174 | * @access public | |
| 175 | * @return string | |
| 176 | */ | |
| 177 | public function key() | |
| 178 | {
| |
| 179 | return key($this->result); | |
| 180 | } | |
| 181 | ||
| 182 | /** | |
| 183 | * Iterator next element | |
| 184 | * | |
| 185 | * @access public | |
| 186 | * @return mixed | |
| 187 | */ | |
| 188 | public function next() | |
| 189 | {
| |
| 190 | return next($this->result); | |
| 191 | } | |
| 192 | ||
| 193 | /** | |
| 194 | * Iterator valid | |
| 195 | * | |
| 196 | * @return bool | |
| 197 | */ | |
| 198 | public function valid() | |
| 199 | {
| |
| 200 | $key = key($this->result); | |
| 201 | return ($key !== NULL && $key !== FALSE); | |
| 202 | } | |
| 203 | ||
| 204 | /** | |
| 205 | * ArrayAccess exists | |
| 206 | * | |
| 207 | * @access public | |
| 208 | * @param string $key | |
| 209 | * @return bool | |
| 210 | */ | |
| 211 | public function offsetExists($key) | |
| 212 | {
| |
| 213 | return $this->__isset($key); | |
| 214 | } | |
| 215 | ||
| 216 | /** | |
| 217 | * ArrayAccess get element | |
| 218 | * | |
| 219 | * @access public | |
| 220 | * @param string $key | |
| 221 | * @return mixed | |
| 222 | */ | |
| 223 | public function offsetGet($key) | |
| 224 | {
| |
| 225 | return $this->__get($key); | |
| 226 | } | |
| 227 | ||
| 228 | /** | |
| 229 | * ArrayAccess set | |
| 230 | * | |
| 231 | * @access public | |
| 232 | * @param string $key | |
| 233 | * @param mixed $value | |
| 234 | */ | |
| 235 | public function offsetSet($key, $value) | |
| 236 | {
| |
| 237 | return $this->__set($key, $value); | |
| 238 | } | |
| 239 | ||
| 240 | /** | |
| 241 | * ArrayAcess unset | |
| 242 | * | |
| 243 | * @access public | |
| 244 | * @param string $key | |
| 245 | * @return void | |
| 246 | */ | |
| 247 | public function offsetUnset($key) | |
| 248 | {
| |
| 249 | return $this->__unset($key); | |
| 250 | } | |
| 251 | ||
| 252 | /** | |
| 253 | * Create pagination | |
| 254 | * | |
| 255 | * @access protected | |
| 256 | * @return object | |
| 257 | */ | |
| 258 | protected function createPagination() | |
| 259 | {
| |
| 260 | return new Pagination(['pageSize' => $this->perPage, 'page' => $this->page, 'totalCount' => $this->count]); | |
| 261 | } | |
| 262 | ||
| 263 | /** | |
| 264 | * Get request page | |
| 265 | * | |
| 266 | * @access protected | |
| 267 | * @param int $page | |
| 268 | * @return int | |
| 269 | */ | |
| 270 | protected function getRequestPage($page) | |
| 271 | {
| |
| 272 | if (! $page) {
| |
| 273 | $page = (isset($_REQUEST['page'])) ? $_REQUEST['page'] : 1; | |
| 274 | } | |
| 275 | return (int) $page * 1; | |
| 276 | } | |
| 277 | } |