* renamed TcaMapper to ObjectRelationalMapper
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / TX_EXTMVC_Persistence_ObjectStorage.php
index ff2ef17..c96577e 100644 (file)
@@ -28,60 +28,70 @@ declare(ENCODING = 'utf-8');
  * @version $Id:$
  * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
  */
-class TX_EXTMVC_Persistence_ObjectStorage implements Iterator, Countable {
-       
+class TX_EXTMVC_Persistence_ObjectStorage implements Iterator, Countable, ArrayAccess {
+
        /**
-        * The array holding references to the stored objects.
-        *
-        * @var string
-        **/
+               * The array holding references to the stored objects.
+               *
+               * @var string
+               **/
        private $storage = array();
-       
-       /**
-        * An index for the Iterator interface
-        *
-        * @var string
-        **/
-       private $index = 0;
 
-       function rewind() {
-               rewind($this->storage);
+       public function rewind() {
+               reset($this->storage);
        }
 
-       function valid() {
-               return key($this->storage) !== false;
+       public function valid() {
+               return $this->current() !== FALSE;
        }
 
-       function key() {
-               return $this->index;
+       public function key() {
+               return key($this->storage);
        }
 
-       function current() {
+       public function current() {
                return current($this->storage);
        }
 
-       function next() {
+       public function next() {
                next($this->storage);
-               $this->index++;
        }
 
-       function count() {
+       public function count() {
                return count($this->storage);
        }
 
+       public function offsetSet($offset, $obj) {
+               if (is_object($obj) && !$this->contains($obj)) {
+                       $this->storage[$offset] = $obj;
+               }
+       }
+
+       public function offsetExists($offset) {
+               return isset($this->storage[$offset]);
+       }
+
+       public function offsetUnset($offset) {
+               unset($this->storage[$offset]);
+       }
+
+       public function offsetGet($offset) {
+               return isset($this->storage[$offset]) ? $this->storage[$offset] : NULL;
+       }
+
        /**
         * Does the Storage contains the given object
         *
         * @param Object $obj 
         * @return boolean TRUE|FALSE The result TRUE if the Storage contains the object; the result FALSE if not
         */
-       function contains($obj) {
+       public function contains($obj) {
                if (is_object($obj)) {
                        foreach($this->storage as $object) {
-                               if ($object === $obj) return true;
+                               if ($object === $obj) return TRUE;
                        }
                }
-               return false;
+               return FALSE;
        }
 
        /**
@@ -90,7 +100,7 @@ class TX_EXTMVC_Persistence_ObjectStorage implements Iterator, Countable {
         * @param Object $obj 
         * @return void
         */
-       function attach($obj) {
+       public function attach($obj) {
                if (is_object($obj) && !$this->contains($obj)) {
                        $this->storage[] = $obj;
                }
@@ -102,17 +112,37 @@ class TX_EXTMVC_Persistence_ObjectStorage implements Iterator, Countable {
         * @param Object $obj 
         * @return void
         */
-       function detach($obj) {
+       public function detach($obj) {
                if (is_object($obj)) {
-                       foreach($this->storage as $idx => $object) {
+                       foreach($this->storage as $key => $object) {
                                if ($object === $obj) {
-                                       unset($this->storage[$idx]);
+                                       unset($this->storage[$key]);
                                        $this->rewind();
                                        return;
                                }
                        }
                }
        }
+
+       /**
+        * Removes all object from the storage
+        *
+        * @return void
+        */
+       public function removeAll() {
+               $this->storage = array();
+       }
+       
+       /**
+        * Returns this object storage as an array
+        *
+        * @return array The object storage
+        * @author Jochen Rau <jochen.rau@typoplanet.de>
+        */
+       public function toArray() {
+               return $this->storage;
+       }
+
 }
 
 ?>
\ No newline at end of file