[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/externals/restful/src/RESTful/ -> Itemization.php (source)

   1  <?php
   2  
   3  namespace RESTful;
   4  
   5  class Itemization 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      protected function _getItem($offset)
  44      {
  45          $page_offset = floor($offset / $this->_size);
  46          $page = $this->_getPage($page_offset);
  47  
  48          return $page->items[$offset - $page->offset];
  49      }
  50  
  51      public function total()
  52      {
  53          return $this->_getPage()->total;
  54      }
  55  
  56      protected function _buildUri($offset = null)
  57      {
  58          # TODO: hacky but works for now
  59          $offset = ($offset == null) ? $this->_offset : $offset;
  60          if (strpos($this->uri, '?') === false) {
  61              $uri = $this->uri . '?';
  62          } else {
  63              $uri = $this->uri . '&';
  64          }
  65          $uri = $uri . 'offset=' . strval($offset);
  66  
  67          return $uri;
  68      }
  69  
  70      // IteratorAggregate
  71      public function getIterator()
  72      {
  73          $uri = $this->_buildUri($offset = 0);
  74          $uri = $this->_buildUri($offset = 0);
  75  
  76          return new ItemizationIterator($this->resource, $uri);
  77      }
  78  
  79      // ArrayAccess
  80      public function offsetSet($offset, $value)
  81      {
  82          throw new \BadMethodCallException(get_class($this) . ' array access is read-only');
  83      }
  84  
  85      public function offsetExists($offset)
  86      {
  87          return (0 <= $offset && $offset < $this->total());
  88      }
  89  
  90      public function offsetUnset($offset)
  91      {
  92          throw new \BadMethodCallException(get_class($this) . ' array access is read-only');
  93      }
  94  
  95      public function offsetGet($offset)
  96      {
  97          return $this->_getItem($offset);
  98      }
  99  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1