123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- class Services_Twilio_AutoPagingIterator
- implements Iterator
- {
- protected $generator;
- protected $args;
- protected $items;
- private $_args;
- public function __construct($generator, $page, $size, $filters) {
- $this->generator = $generator;
- $this->page = $page;
- $this->size = $size;
- $this->filters = $filters;
- $this->items = array();
-
- $this->_args = array(
- 'page' => $page,
- 'size' => $size,
- 'filters' => $filters,
- );
- }
- public function current()
- {
- return current($this->items);
- }
- public function key()
- {
- return key($this->items);
- }
-
- public function next()
- {
- try {
- $this->loadIfNecessary();
- return next($this->items);
- }
- catch (Services_Twilio_RestException $e) {
-
- if ($e->getCode() != 20006) {
- throw $e;
- }
- }
- }
-
- public function rewind()
- {
- foreach ($this->_args as $arg => $val) {
- $this->$arg = $val;
- }
- $this->items = array();
- $this->next_page_uri = null;
- }
- public function count()
- {
- throw new BadMethodCallException('Not allowed');
- }
- public function valid()
- {
- try {
- $this->loadIfNecessary();
- return key($this->items) !== null;
- }
- catch (Services_Twilio_RestException $e) {
-
- if ($e->getCode() != 20006) {
- throw $e;
- }
- }
- return false;
- }
-
- protected function loadIfNecessary()
- {
- if (
- empty($this->items)
-
- || key($this->items) === null
- ) {
- $page = call_user_func_array($this->generator, array(
- $this->page,
- $this->size,
- $this->filters,
- $this->next_page_uri,
- ));
- $this->next_page_uri = $page->next_page_uri;
- $this->items = $page->getItems();
- $this->page = $this->page + 1;
- }
- }
- }
|