composer package updates
[openemr.git] / vendor / zendframework / zend-cache / src / Storage / Adapter / DbaIterator.php
blobabad29fe2115876283fd1e84b73794b02b91626a
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Cache\Storage\Adapter;
12 use Zend\Cache\Exception;
13 use Zend\Cache\Storage\IteratorInterface;
15 class DbaIterator implements IteratorInterface
17 /**
18 * The apc storage instance
20 * @var Dba
22 protected $storage;
24 /**
25 * The iterator mode
27 * @var int
29 protected $mode = IteratorInterface::CURRENT_AS_KEY;
31 /**
32 * The dba resource handle
34 * @var resource
36 protected $handle;
38 /**
39 * The length of the namespace prefix
41 * @var int
43 protected $prefixLength;
45 /**
46 * The current internal key
48 * @var string|bool
50 protected $currentInternalKey;
52 /**
53 * Constructor
55 * @param Dba $storage
56 * @param resource $handle
57 * @param string $prefix
59 public function __construct(Dba $storage, $handle, $prefix)
61 $this->storage = $storage;
62 $this->handle = $handle;
63 $this->prefixLength = strlen($prefix);
65 $this->rewind();
68 /**
69 * Get storage instance
71 * @return Dba
73 public function getStorage()
75 return $this->storage;
78 /**
79 * Get iterator mode
81 * @return int Value of IteratorInterface::CURRENT_AS_*
83 public function getMode()
85 return $this->mode;
88 /**
89 * Set iterator mode
91 * @param int $mode
92 * @return DbaIterator Provides a fluent interface
94 public function setMode($mode)
96 $this->mode = (int) $mode;
97 return $this;
100 /* Iterator */
103 * Get current key, value or metadata.
105 * @return mixed
106 * @throws Exception\RuntimeException
108 public function current()
110 if ($this->mode == IteratorInterface::CURRENT_AS_SELF) {
111 return $this;
114 $key = $this->key();
116 if ($this->mode == IteratorInterface::CURRENT_AS_VALUE) {
117 return $this->storage->getItem($key);
118 } elseif ($this->mode == IteratorInterface::CURRENT_AS_METADATA) {
119 return $this->storage->getMetadata($key);
122 return $key;
126 * Get current key
128 * @return string
129 * @throws Exception\RuntimeException
131 public function key()
133 if ($this->currentInternalKey === false) {
134 throw new Exception\RuntimeException("Iterator is on an invalid state");
137 // remove namespace prefix
138 return substr($this->currentInternalKey, $this->prefixLength);
142 * Move forward to next element
144 * @return void
145 * @throws Exception\RuntimeException
147 public function next()
149 if ($this->currentInternalKey === false) {
150 throw new Exception\RuntimeException("Iterator is on an invalid state");
153 $this->currentInternalKey = dba_nextkey($this->handle);
155 // Workaround for PHP-Bug #62492
156 if ($this->currentInternalKey === null) {
157 $this->currentInternalKey = false;
162 * Checks if current position is valid
164 * @return bool
166 public function valid()
168 return ($this->currentInternalKey !== false);
172 * Rewind the Iterator to the first element.
174 * @return void
175 * @throws Exception\RuntimeException
177 public function rewind()
179 if ($this->currentInternalKey === false) {
180 throw new Exception\RuntimeException("Iterator is on an invalid state");
183 $this->currentInternalKey = dba_firstkey($this->handle);
185 // Workaround for PHP-Bug #62492
186 if ($this->currentInternalKey === null) {
187 $this->currentInternalKey = false;