[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 namespace RESTful; 4 5 class Pagination implements \IteratorAggregate, \ArrayAccess 6 { 7 public $resource, 8 $uri; 9 10 protected $_page, 11 $_offset = 0, 12 $_size = 25; 13 14 public function __construct($resource, $uri, $data = null) 15 { 16 $this->resource = $resource; 17 $this->uri = $uri; 18 if ($data != null) { 19 $this->_page = new Page($resource, $uri, $data); 20 } else { 21 $this->_page = null; 22 } 23 } 24 25 protected function _getPage($offset = null) 26 { 27 if ($this->_page == null) { 28 $this->_offset = ($offset == null) ? 0 : $offset * $this->_size; 29 $uri = $this->_buildUri(); 30 $this->_page = new Page($this->resource, $uri); 31 } elseif ($offset != null) { 32 $offset = $offset * $this->_size; 33 if ($offset != $this->_offset) { 34 $this->_offset = $offset; 35 $uri = $this->_buildUri(); 36 $this->_page = new Page($this->resource, $uri); 37 } 38 } 39 40 return $this->_page; 41 } 42 43 public function total() 44 { 45 return floor($this->_getPage()->total / $this->_size); 46 } 47 48 protected function _buildUri($offset = null) 49 { 50 # TODO: hacky but works for now 51 $offset = ($offset == null) ? $this->_offset : $offset; 52 if (strpos($this->uri, '?') === false) { 53 $uri = $this->uri . '?'; 54 } else { 55 $uri = $this->uri . '&'; 56 } 57 $uri = $uri . 'offset=' . strval($offset); 58 59 return $uri; 60 } 61 62 // IteratorAggregate 63 public function getIterator() 64 { 65 $uri = $this->_buildUri($offset = 0); 66 67 return new PaginationIterator($this->resource, $uri); 68 } 69 70 // ArrayAccess 71 public function offsetSet($offset, $value) 72 { 73 throw new \BadMethodCallException(get_class($this) . ' array access is read-only'); 74 } 75 76 public function offsetExists($offset) 77 { 78 return (0 <= $offset && $offset < $this->total()); 79 } 80 81 public function offsetUnset($offset) 82 { 83 throw new \BadMethodCallException(get_class($this) . ' array access is read-only'); 84 } 85 86 public function offsetGet($offset) 87 { 88 return $this->_getPage($offset); 89 } 90 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |