80b20f1dcfbd3206694b513f2176ba4c391bb0a4
[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 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, $value) {
104 if (!is_object($offset)) throw new Tx_Extbase_MVC_Exception_InvalidArgumentType('Expected parameter 1 to be object, ' . gettype($offset) . ' given');
105 // if (!is_object($obj)) throw new Tx_Extbase_MVC_Exception_InvalidArgumentType('Expected parameter 2 to be object, ' . gettype($offset) . ' given');
106 // if (!($offset === $obj)) throw new Tx_Extbase_MVC_Exception_InvalidArgumentType('Parameter 1 and parameter 2 must be a reference to the same object.');
107 if (!$this->contains($offset)) {
108 $this->storage[spl_object_hash($offset)] = $value;
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_MVC_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_MVC_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_MVC_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_MVC_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, $value = NULL) {
163 if (!is_object($object)) throw new Tx_Extbase_MVC_Exception_InvalidArgumentType('Expected parameter to be an object, ' . gettype($object) . ' given');
164 if (!$this->contains($object)) {
165 if ($value === NULL) {
166 $value = $object;
167 }
168 // TODO Revise this with Karsten
169 $this->storage[spl_object_hash($object)] = $value;
170 }
171 }
172
173 /**
174 * Detaches an object from the storage
175 *
176 * @param Object $object The object to be removed from the storage
177 * @return void
178 */
179 public function detach($object) {
180 if (!is_object($object)) throw new Tx_Extbase_MVC_Exception_InvalidArgumentType('Expected parameter to be an object, ' . gettype($object) . ' given');
181 unset($this->storage[spl_object_hash($object)]);
182 }
183
184 /**
185 * Attach all objects to the storage
186 *
187 * @param array $objects The objects to be attached to the storage
188 * @return void
189 */
190 public function addAll($objects) {
191 if (is_array($objects) || ($objects instanceof Tx_Extbase_Persistence_ObjectStorage)) {
192 foreach ($objects as $object) {
193 $this->attach($object);
194 }
195 } else {
196 throw new Tx_Extbase_MVC_Exception_InvalidArgumentType('Expected parameter to be an array, ' . gettype($object) . ' given');
197 }
198 }
199
200 /**
201 * Detaches all objects from the storage
202 *
203 * @param array $objects The objects to be detached from the storage
204 * @return void
205 */
206 public function removeAll($objects) {
207 if (!is_array($object)) throw new Tx_Extbase_MVC_Exception_InvalidArgumentType('Expected parameter to be an array, ' . gettype($object) . ' given');
208 foreach ($objects as $object) {
209 $this->detach($object);
210 }
211 }
212
213 /**
214 * Returns this object storage as an array
215 *
216 * @return array The object storage
217 */
218 public function toArray() {
219 return $this->storage;
220 }
221
222 }
223
224 ?>