diff --git a/Array/Typed.php b/Array/Typed.php index 1fe96a5..652b288 100644 --- a/Array/Typed.php +++ b/Array/Typed.php @@ -5,13 +5,15 @@ Array_Typed version 1.0.3 */ -abstract class Array_Typed implements ArrayAccess, Countable { +abstract class Array_Typed implements ArrayAccess, Countable, Iterator { protected $container; protected $arraySize; + private $iterPos; // protected $typeSize; function __construct($n) { $this->arraySize = $n; $this->container = str_repeat("\0", $n * $this->typeSize); + $this->iterPos = 0; } // function toArray() { @@ -37,6 +39,22 @@ public function getSize() { public function count() { return $this->arraySize; } + // Iterator + public function rewind() { + $this->iterPos = 0; + } + public function current() { + return $this->_offsetGet($this->iterPos); + } + public function key() { + return $this->iterPos; + } + function next() { + $this->iterPos++; + } + function valid() { + return $this->offsetExists($this->iterPos); + } // public function offsetExists($offset) { if (($offset < 0) || ($this->arraySize <= $offset)) { diff --git a/test/iter_test.php b/test/iter_test.php new file mode 100644 index 0000000..599c4b5 --- /dev/null +++ b/test/iter_test.php @@ -0,0 +1,32 @@ +