diff --git a/src/StrictArray.php b/src/StrictArray.php new file mode 100644 index 0000000..195fb98 --- /dev/null +++ b/src/StrictArray.php @@ -0,0 +1,87 @@ +class = $itemClass; + } + + public function offsetExists($offset) + { + return isset($this->data[$offset]); + } + + public function offsetGet($offset) + { + if(isset($this->data[$offset])){ + return $this->data[$offset]; + }else{ + return null; + } + } + + public function offsetSet($offset, $value) + { + if(is_a($value,$this->class)){ + $this->data[$offset] = $value; + return true; + } + throw new \Exception("StrictArray can only set {$this->class} object"); + } + + public function offsetUnset($offset) + { + if(isset($this->data[$offset])){ + unset($this->data[$offset]); + return true; + }else{ + return false; + } + } + + public function count() + { + return count($this->data); + } + + public function current() + { + return $this->data[$this->currentKey]; + } + + public function next() + { + $this->currentKey = array_shift($this->keys); + } + + public function key() + { + if($this->currentKey === null){ + $this->rewind(); + } + return $this->currentKey; + } + + public function valid() + { + return isset($this->data[$this->currentKey]); + } + + public function rewind() + { + $this->currentKey = null; + $this->keys = []; + $this->keys = array_keys($this->data); + $this->currentKey = array_shift($this->keys); + } +} \ No newline at end of file