argument registration
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / ObjectStorage.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * The storage for objects. It ensures the uniqueness of an object in the storage. It's a remake of the
27 * SplObjectStorage introduced in PHP 5.3.
28 *
29 * @package TYPO3
30 * @subpackage extbase
31 * @version $ID:$
32 */
33 class Tx_ExtBase_Persistence_ObjectStorage implements Iterator, Countable, ArrayAccess {
34
35 /**
36 * The array holding references of the stored objects
37 *
38 * @var array
39 */
40 private $storage = array();
41
42 /**
43 * Resets the array pointer of the storage
44 *
45 * @return void
46 */
47 public function rewind() {
48 reset($this->storage);
49 }
50
51 /**
52 * Checks if the array pointer of the storage points to a valid position
53 *
54 * @return void
55 */
56 public function valid() {
57 return $this->current() !== FALSE;
58 }
59
60 /**
61 * Returns the current key storage array
62 *
63 * @return void
64 */
65 public function key() {
66 return key($this->storage);
67 }
68
69 /**
70 * Returns the current value of the storage array
71 *
72 * @return void
73 */
74 public function current() {
75 return current($this->storage);
76 }
77
78 /**
79 * Returns the next position of the storage array
80 *
81 * @return void
82 */
83 public function next() {
84 next($this->storage);
85 }
86
87 /**
88 * Counts the elements in the storage array
89 *
90 * @return void
91 */
92 public function count() {
93 return count($this->storage);
94 }
95
96 /**
97 * Loads the array at a given offset. Nothing happens if the object already exists in the storage
98 *
99 * @param string $offset
100 * @param string $obj The object
101 * @return void
102 */
103 public function offsetSet($offset, $obj) {
104 if (!is_object($offset)) throw new Tx_ExtBase_Exception_InvalidArgumentType('Expected parameter 1 to be object, ' . gettype($offset) . ' given');
105 if (!is_object($obj)) throw new Tx_ExtBase_Exception_InvalidArgumentType('Expected parameter 2 to be object, ' . gettype($offset) . ' given');
106 if (!($offset === $obj)) throw new Tx_ExtBase_Exception_InvalidArgumentType('Parameter 1 and parameter 2 must be a reference to the same object.');
107 if (!$this->contains($obj)) {
108 $this->storage[spl_object_hash($offset)] = $obj;
109 }
110 }
111
112 /**
113 * Checks if a given offset exists in the storage
114 *
115 * @param string $offset
116 * @return boolean TRUE if the given offset exists; otherwise FALSE
117 */
118 public function offsetExists($offset) {
119 if (!is_object($offset)) throw new Tx_ExtBase_Exception_InvalidArgumentType('Expected parameter to be an object, ' . gettype($offset) . ' given');
120 return isset($this->storage[spl_object_hash($offset)]);
121 }
122
123 /**
124 * Unsets the storage at the given offset
125 *
126 * @param string $offset The offset
127 * @return void
128 */
129 public function offsetUnset($offset) {
130 if (!is_object($offset)) throw new Tx_ExtBase_Exception_InvalidArgumentType('Expected parameter to be an object, ' . gettype($offset) . ' given');
131 unset($this->storage[spl_object_hash($offset)]);
132 }
133
134 /**
135 * Returns the object at the given offset
136 *
137 * @param string $offset The offset
138 * @return Object The object
139 */
140 public function offsetGet($offset) {
141 if (!is_object($offset)) throw new Tx_ExtBase_Exception_InvalidArgumentType('Expected parameter to be an object, ' . gettype($offset) . ' given');
142 return isset($this->storage[spl_object_hash($offset)]) ? $this->storage[spl_object_hash($offset)] : NULL;
143 }
144
145 /**
146 * Checks if the storage contains the given object
147 *
148 * @param Object $object The object to be checked for
149 * @return boolean TRUE|FALSE Returns TRUE if the storage contains the object; otherwise FALSE
150 */
151 public function contains($object) {
152 if (!is_object($object)) throw new Tx_ExtBase_Exception_InvalidArgumentType('Expected parameter to be an object, ' . gettype($object) . ' given');
153 return array_key_exists(spl_object_hash($object), $this->storage);
154 }
155
156 /**
157 * Attaches an object to the storage
158 *
159 * @param Object $obj The Object to be attached
160 * @return void
161 */
162 public function attach($object) {
163 if (!is_object($object)) throw new Tx_ExtBase_Exception_InvalidArgumentType('Expected parameter to be an object, ' . gettype($object) . ' given');
164 if (!$this->contains($object)) {
165 $this->storage[spl_object_hash($object)] = $object;
166 }
167 }
168
169 /**
170 * Detaches an object from the storage
171 *
172 * @param Object $object The object to be removed from the storage
173 * @return void
174 */
175 public function detach($object) {
176 if (!is_object($object)) throw new Tx_ExtBase_Exception_InvalidArgumentType('Expected parameter to be an object, ' . gettype($object) . ' given');
177 unset($this->storage[spl_object_hash($object)]);
178 }
179
180 /**
181 * Returns this object storage as an array
182 *
183 * @return array The object storage
184 */
185 public function toArray() {
186 return $this->storage;
187 }
188
189 }
190
191 ?>