123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <?php
- namespace Predis\Collection\Iterator;
- use Iterator;
- use Predis\ClientInterface;
- use Predis\NotSupportedException;
- abstract class CursorBasedIterator implements Iterator
- {
- protected $client;
- protected $match;
- protected $count;
- protected $valid;
- protected $fetchmore;
- protected $elements;
- protected $cursor;
- protected $position;
- protected $current;
-
- public function __construct(ClientInterface $client, $match = null, $count = null)
- {
- $this->client = $client;
- $this->match = $match;
- $this->count = $count;
- $this->reset();
- }
-
- protected function requiredCommand(ClientInterface $client, $commandID)
- {
- if (!$client->getProfile()->supportsCommand($commandID)) {
- throw new NotSupportedException("The specified server profile does not support the `$commandID` command.");
- }
- }
-
- protected function reset()
- {
- $this->valid = true;
- $this->fetchmore = true;
- $this->elements = array();
- $this->cursor = 0;
- $this->position = -1;
- $this->current = null;
- }
-
- protected function getScanOptions()
- {
- $options = array();
- if (strlen($this->match) > 0) {
- $options['MATCH'] = $this->match;
- }
- if ($this->count > 0) {
- $options['COUNT'] = $this->count;
- }
- return $options;
- }
-
- protected abstract function executeCommand();
-
- protected function fetch()
- {
- list($cursor, $elements) = $this->executeCommand();
- if (!$cursor) {
- $this->fetchmore = false;
- }
- $this->cursor = $cursor;
- $this->elements = $elements;
- }
-
- protected function extractNext()
- {
- $this->position++;
- $this->current = array_shift($this->elements);
- }
-
- public function rewind()
- {
- $this->reset();
- $this->next();
- }
-
- public function current()
- {
- return $this->current;
- }
-
- public function key()
- {
- return $this->position;
- }
-
- public function next()
- {
- if (!$this->elements && $this->fetchmore) {
- $this->fetch();
- }
- if ($this->elements) {
- $this->extractNext();
- } else if ($this->cursor) {
- $this->next();
- } else {
- $this->valid = false;
- }
- }
-
- public function valid()
- {
- return $this->valid;
- }
- }
|