Add Extbase 1.0.1 to TYPO3core. Do NOT make changes inside! See misc/core_svn_rules...
[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 Extbase
30 * @subpackage Persistence
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 protected $storage = array();
41
42 /**
43 *
44 * @var bool
45 */
46 protected $isInitialized = TRUE;
47
48 /**
49 * This is a template function to be overwritten by a concrete implementation. It enables you to implement
50 * a lazy load implementation.
51 *
52 * @return void
53 */
54 protected function initializeStorage() {
55 }
56
57 /**
58 * Returns the state of the initialization
59 *
60 * @return void
61 */
62 public function isInitialized() {
63 return $this->isInitialized;
64 }
65
66 /**
67 * Resets the array pointer of the storage
68 *
69 * @return void
70 */
71 public function rewind() {
72 $this->initializeStorage();
73 reset($this->storage);
74 }
75
76 /**
77 * Checks if the array pointer of the storage points to a valid position
78 *
79 * @return void
80 */
81 public function valid() {
82 $this->initializeStorage();
83 return $this->current() !== FALSE;
84 }
85
86 /**
87 * Returns the current key storage array
88 *
89 * @return void
90 */
91 public function key() {
92 $this->initializeStorage();
93 return key($this->storage);
94 }
95
96 /**
97 * Returns the current value of the storage array
98 *
99 * @return void
100 */
101 public function current() {
102 $this->initializeStorage();
103 return current($this->storage);
104 }
105
106 /**
107 * Returns the next position of the storage array
108 *
109 * @return void
110 */
111 public function next() {
112 $this->initializeStorage();
113 next($this->storage);
114 }
115
116 /**
117 * Counts the elements in the storage array
118 *
119 * @return void
120 */
121 public function count() {
122 $this->initializeStorage();
123 return count($this->storage);
124 }
125
126 /**
127 * Loads the array at a given offset. Nothing happens if the object already exists in the storage
128 *
129 * @param string $offset
130 * @param string $obj The object
131 * @return void
132 */
133 public function offsetSet($offset, $value) {
134 if (!is_object($offset)) throw new Tx_Extbase_MVC_Exception_InvalidArgumentType('Expected parameter 1 to be object, ' . gettype($offset) . ' given');
135 // TODO Check implementation again
136 // if (!is_object($obj)) throw new Tx_Extbase_MVC_Exception_InvalidArgumentType('Expected parameter 2 to be object, ' . gettype($offset) . ' given');
137 // if (!($offset === $obj)) throw new Tx_Extbase_MVC_Exception_InvalidArgumentType('Parameter 1 and parameter 2 must be a reference to the same object.');
138 $this->initializeStorage();
139 if (!$this->contains($offset)) {
140 $this->storage[spl_object_hash($offset)] = $value;
141 }
142 }
143
144 /**
145 * Checks if a given offset exists in the storage
146 *
147 * @param string $offset
148 * @return boolean TRUE if the given offset exists; otherwise FALSE
149 */
150 public function offsetExists($offset) {
151 $this->isObject($offset);
152 $this->initializeStorage();
153 return isset($this->storage[spl_object_hash($offset)]);
154 }
155
156 /**
157 * Unsets the storage at the given offset
158 *
159 * @param string $offset The offset
160 * @return void
161 */
162 public function offsetUnset($offset) {
163 $this->isObject($offset);
164 $this->initializeStorage();
165 unset($this->storage[spl_object_hash($offset)]);
166 }
167
168 /**
169 * Returns the object at the given offset
170 *
171 * @param string $offset The offset
172 * @return Object The object
173 */
174 public function offsetGet($offset) {
175 $this->isObject($offset);
176 $this->initializeStorage();
177 return isset($this->storage[spl_object_hash($offset)]) ? $this->storage[spl_object_hash($offset)] : NULL;
178 }
179
180 /**
181 * Checks if the storage contains the given object
182 *
183 * @param Object $object The object to be checked for
184 * @return boolean TRUE|FALSE Returns TRUE if the storage contains the object; otherwise FALSE
185 */
186 public function contains($object) {
187 $this->isObject($object);
188 $this->initializeStorage();
189 return array_key_exists(spl_object_hash($object), $this->storage);
190 }
191
192 /**
193 * Attaches an object to the storage
194 *
195 * @param Object $obj The Object to be attached
196 * @return void
197 */
198 public function attach($object, $value = NULL) {
199 $this->isObject($object);
200 $this->initializeStorage();
201 if (!$this->contains($object)) {
202 if ($value === NULL) {
203 $value = $object;
204 }
205 // TODO Revise this with Karsten
206 $this->storage[spl_object_hash($object)] = $value;
207 }
208 }
209
210 /**
211 * Detaches an object from the storage
212 *
213 * @param Object $object The object to be removed from the storage
214 * @return void
215 */
216 public function detach($object) {
217 $this->isObject($object);
218 $this->initializeStorage();
219 unset($this->storage[spl_object_hash($object)]);
220 }
221
222 /**
223 * Attach all objects to the storage
224 *
225 * @param array $objects The objects to be attached to the storage
226 * @return void
227 */
228 public function addAll($objects) {
229 if (is_array($objects) || $objects instanceof Iterator) {
230 $this->initializeStorage();
231 foreach ($objects as $object) {
232 $this->attach($object);
233 }
234 }
235 }
236
237 /**
238 * Detaches all objects from the storage
239 *
240 * @param array $objects The objects to be detached from the storage
241 * @return void
242 */
243 public function removeAll($objects) {
244 if (is_array($objects) || $objects instanceof Iterator) {
245 $this->initializeStorage();
246 foreach ($objects as $object) {
247 $this->detach($object);
248 }
249 }
250 }
251
252 /**
253 * Checks, if the given value is an object and throws an exception if not
254 *
255 * @param string $value The value to be tested
256 * @return bool TRUE, if the given value is an object
257 */
258 protected function isObject($value) {
259 if (!is_object($value)) {
260 throw new Tx_Extbase_MVC_Exception_InvalidArgumentType('Expected parameter to be an object, ' . gettype($offset) . ' given');
261 }
262 return TRUE;
263 }
264
265 /**
266 * Returns this object storage as an array
267 *
268 * @return array The object storage
269 */
270 public function toArray() {
271 $this->initializeStorage();
272 return $this->storage;
273 }
274
275 }
276
277 ?>