[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/externals/twilio-php/Services/Twilio/ -> AutoPagingIterator.php (source)

   1  <?php
   2  
   3  class Services_Twilio_AutoPagingIterator
   4      implements Iterator
   5  {
   6      protected $generator;
   7      protected $args;
   8      protected $items;
   9  
  10      private $_args;
  11  
  12      public function __construct($generator, $page, $size, $filters) {
  13          $this->generator = $generator;
  14          $this->page = $page;
  15          $this->size = $size;
  16          $this->filters = $filters;
  17          $this->items = array();
  18  
  19          // Save a backup for rewind()
  20          $this->_args = array(
  21              'page' => $page,
  22              'size' => $size,
  23              'filters' => $filters,
  24          );
  25      }
  26  
  27      public function current()
  28      {
  29          return current($this->items);
  30      }
  31  
  32      public function key()
  33      {
  34          return key($this->items);
  35      }
  36  
  37      /*
  38       * Return the next item in the list, making another HTTP call to the next
  39       * page of resources if necessary.
  40       */
  41      public function next()
  42      {
  43          try {
  44              $this->loadIfNecessary();
  45              return next($this->items);
  46          }
  47          catch (Services_Twilio_RestException $e) {
  48              // 20006 is an out of range paging error, everything else is valid
  49              if ($e->getCode() != 20006) {
  50                  throw $e;
  51              }
  52          }
  53      }
  54  
  55      /*
  56       * Restore everything to the way it was before we began paging. This gets
  57       * called at the beginning of any foreach() loop
  58       */
  59      public function rewind()
  60      {
  61          foreach ($this->_args as $arg => $val) {
  62              $this->$arg = $val;
  63          }
  64          $this->items = array();
  65          $this->next_page_uri = null;
  66      }
  67  
  68      public function count()
  69      {
  70          throw new BadMethodCallException('Not allowed');
  71      }
  72  
  73      public function valid()
  74      {
  75          try {
  76              $this->loadIfNecessary();
  77              return key($this->items) !== null;
  78          }
  79          catch (Services_Twilio_RestException $e) {
  80              // 20006 is an out of range paging error, everything else is valid
  81              if ($e->getCode() != 20006) {
  82                  throw $e;
  83              }
  84          }
  85          return false;
  86      }
  87  
  88      /*
  89       * Fill $this->items with a new page from the API, if necessary.
  90       */
  91      protected function loadIfNecessary()
  92      {
  93          if (// Empty because it's the first time or last page was empty
  94              empty($this->items)
  95              // null key when the items list is iterated over completely
  96              || key($this->items) === null
  97          ) {
  98              $page = call_user_func_array($this->generator, array(
  99                  $this->page,
 100                  $this->size,
 101                  $this->filters,
 102                  $this->next_page_uri,
 103              ));
 104              $this->next_page_uri = $page->next_page_uri;
 105              $this->items = $page->getItems();
 106              $this->page = $this->page + 1;
 107          }
 108      }
 109  }


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