[+BUGFIX] correct warning in ObjectStorage & LazyObjectStorage
[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 * Opposed to the SplObjectStorage the ObjectStorage does not implement the Serializable interface.
30 *
31 * @package Extbase
32 * @subpackage Persistence
33 * @version $ID:$
34 */
35 class Tx_Extbase_Persistence_ObjectStorage implements Countable, Iterator, ArrayAccess, Tx_Extbase_Persistence_ObjectMonitoringInterface {
36
37 /**
38 * This field is only needed to make debugging easier:
39 * If you call current() on a class that implements Iterator, PHP will return the first field of the object
40 * instead of calling the current() method of the interface.
41 * We use this unusual behavior of PHP to return the warning below in this case.
42 *
43 * @var string
44 */
45 private $warning = 'You should never see this warning. If you do, you probably used PHP array functions like current() on the Tx_Extbase_Persistence_ObjectStorage. To retrieve the first result, you can use the rewind() and current() methods.';
46
47 /**
48 * An array holding the objects and the stored information. The key of the array items ist the
49 * spl_object_hash of the given object.
50 *
51 * array(
52 * spl_object_hash =>
53 * array(
54 * 'obj' => $object,
55 * 'inf' => $information
56 * )
57 * )
58 *
59 * @var array
60 */
61 protected $storage = array();
62
63 /**
64 * A flag indication if the object storage was modified after reconstitution (eg. by adding a new object)
65 * @var bool
66 */
67 protected $isModified = FALSE;
68
69 /**
70 * Rewind the iterator to the first storage element.
71 *
72 * @return void
73 */
74 public function rewind() {
75 reset($this->storage);
76 }
77
78 /**
79 * Checks if the array pointer of the storage points to a valid position
80 *
81 * @return void
82 */
83 public function valid() {
84 return current($this->storage);
85 }
86
87 /**
88 * Returns the index at which the iterator currently is. This is different from the SplObjectStorage
89 * as the key in this implementation is the object hash.
90 *
91 * @return string The index corresponding to the position of the iterator.
92 */
93 public function key() {
94 return key($this->storage);
95 }
96
97 /**
98 * Returns the current storage entry.
99 *
100 * @return object The object at the current iterator position.
101 */
102 public function current() {
103 $item = current($this->storage);
104 return $item['obj'];
105 }
106
107 /**
108 * Moves the iterator to the next object in the storage.
109 *
110 * @return void
111 */
112 public function next() {
113 next($this->storage);
114 }
115
116 /**
117 * Counts the number of objects in the storage.
118 *
119 * @return int The number of objects in the storage.
120 */
121 public function count() {
122 return count($this->storage);
123 }
124
125 /**
126 * Associate data to an object in the storage. offsetSet() is an alias of attach().
127 *
128 * @param object $object The object to add.
129 * @param mixed $information The data to associate with the object.
130 * @return void
131 */
132 public function offsetSet($object, $information) {
133 $this->isModified = TRUE;
134 $this->storage[spl_object_hash($object)] = array('obj' => $object, 'inf' => $information);
135 }
136
137 /**
138 * Checks whether an object exists in the storage.
139 *
140 * @param string $object The object to look for.
141 * @return boolean Returns TRUE if the object exists in the storage, and FALSE otherwise.
142 */
143 public function offsetExists($object) {
144 return isset($this->storage[spl_object_hash($object)]);
145 }
146
147 /**
148 * Removes an object from the storage. offsetUnset() is an alias of detach().
149 *
150 * @param Object $object The object to remove.
151 * @return void
152 */
153 public function offsetUnset($object) {
154 $this->isModified = TRUE;
155 unset($this->storage[spl_object_hash($object)]);
156 }
157
158 /**
159 * Returns the data associated with an object in the storage.
160 *
161 * @param string $object The object to look for.
162 * @return mixed The data previously associated with the object in the storage.
163 */
164 public function offsetGet($object) {
165 return $this->storage[spl_object_hash($object)]['inf'];
166 }
167
168 /**
169 * Checks if the storage contains the object provided.
170 *
171 * @param Object $object The object to look for.
172 * @return boolean Returns TRUE if the object is in the storage, FALSE otherwise.
173 */
174 public function contains($object) {
175 return $this->offsetExists($object);
176 }
177
178 /**
179 * Adds an object inside the storage, and optionaly associate it to some data.
180 *
181 * @param object $object The object to add.
182 * @param mixed $information The data to associate with the object.
183 * @return void
184 */
185 public function attach($object, $information = NULL) {
186 $this->offsetSet($object, $information);
187 }
188
189 /**
190 * Removes the object from the storage.
191 *
192 * @param Object $object The object to remove.
193 * @return void
194 */
195 public function detach($object) {
196 $this->offsetUnset($object);
197 }
198
199 /**
200 * Returns the data, or info, associated with the object pointed by the current iterator position.
201 *
202 * @return mixed The data associated with the current iterator position.
203 */
204 public function getInfo() {
205 $item = current($this->storage);
206 return $item['inf'];
207 }
208
209 public function setInfo($data) {
210 $this->isModified = TRUE;
211 $key = key($this->storage);
212 $this->storage[$key]['inf'] = $data;
213 }
214
215 /**
216 * Adds all objects-data pairs from a different storage in the current storage.
217 *
218 * @param Tx_Extbase_Persistence_ObjectStorage $storage The storage you want to import.
219 * @return void
220 */
221 public function addAll(Tx_Extbase_Persistence_ObjectStorage $storage) {
222 foreach ($storage as $object) {
223 $this->attach($object, $storage->getInfo());
224 }
225 }
226
227 /**
228 * Removes objects contained in another storage from the current storage.
229 *
230 * @param Tx_Extbase_Persistence_ObjectStorage $storage The storage containing the elements to remove.
231 * @return void
232 */
233 public function removeAll(Tx_Extbase_Persistence_ObjectStorage $storage) {
234 foreach ($storage as $object) {
235 $this->detach($object);
236 }
237 }
238
239 /**
240 * Returns this object storage as an array
241 *
242 * @return array The object storage
243 */
244 public function toArray() {
245 $array = array();
246 foreach ($this->storage as $item) {
247 $array[] = $item['obj'];
248 }
249 return $array;
250 }
251
252 public function serialize() {
253 throw new RuntimeException('An ObjectStorage instance cannot be serialized.', 1267700868);
254 }
255
256 public function unserialize($serialized) {
257 throw new RuntimeException('A ObjectStorage instance cannot be unserialized.', 1267700870);
258 }
259
260 /**
261 * Register an object's clean state, e.g. after it has been reconstituted
262 * from the database
263 *
264 * @return void
265 */
266 public function _memorizeCleanState() {
267 $this->isModified = FALSE;
268 }
269
270 /**
271 * Returns TRUE if the properties were modified after reconstitution
272 *
273 * @return boolean
274 */
275 public function _isDirty() {
276 return $this->isModified;
277 }
278
279 }
280
281 ?>