2 /***************************************************************
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
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.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
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.
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
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.
30 * @subpackage Persistence
33 class Tx_Extbase_Persistence_ObjectStorage
implements Iterator
, Countable
, ArrayAccess
{
36 * The array holding references of the stored objects
40 protected $storage = array();
43 * This is a template function to be overwritten by a concrete implementation. It enables you to implement
44 * a lazy load implementation.
48 protected function initializeStorage() {
52 * Resets the array pointer of the storage
56 public function rewind() {
57 $this->initializeStorage();
58 reset($this->storage
);
62 * Checks if the array pointer of the storage points to a valid position
66 public function valid() {
67 $this->initializeStorage();
68 return $this->current() !== FALSE;
72 * Returns the current key storage array
76 public function key() {
77 $this->initializeStorage();
78 return key($this->storage
);
82 * Returns the current value of the storage array
86 public function current() {
87 $this->initializeStorage();
88 return current($this->storage
);
92 * Returns the next position of the storage array
96 public function next() {
97 $this->initializeStorage();
102 * Counts the elements in the storage array
106 public function count() {
107 $this->initializeStorage();
108 return count($this->storage
);
112 * Loads the array at a given offset. Nothing happens if the object already exists in the storage
114 * @param string $offset
115 * @param string $obj The object
118 public function offsetSet($offset, $value) {
119 if (!is_object($offset)) throw new Tx_Extbase_MVC_Exception_InvalidArgumentType('Expected parameter 1 to be object, ' . gettype($offset) . ' given');
120 // TODO Check implementation again
121 // if (!is_object($obj)) throw new Tx_Extbase_MVC_Exception_InvalidArgumentType('Expected parameter 2 to be object, ' . gettype($offset) . ' given');
122 // if (!($offset === $obj)) throw new Tx_Extbase_MVC_Exception_InvalidArgumentType('Parameter 1 and parameter 2 must be a reference to the same object.');
123 $this->initializeStorage();
124 if (!$this->contains($offset)) {
125 $this->storage
[spl_object_hash($offset)] = $value;
130 * Checks if a given offset exists in the storage
132 * @param string $offset
133 * @return boolean TRUE if the given offset exists; otherwise FALSE
135 public function offsetExists($offset) {
136 $this->isObject($offset);
137 $this->initializeStorage();
138 return isset($this->storage
[spl_object_hash($offset)]);
142 * Unsets the storage at the given offset
144 * @param string $offset The offset
147 public function offsetUnset($offset) {
148 $this->isObject($offset);
149 $this->initializeStorage();
150 unset($this->storage
[spl_object_hash($offset)]);
154 * Returns the object at the given offset
156 * @param string $offset The offset
157 * @return Object The object
159 public function offsetGet($offset) {
160 $this->isObject($offset);
161 $this->initializeStorage();
162 return isset($this->storage
[spl_object_hash($offset)]) ?
$this->storage
[spl_object_hash($offset)] : NULL;
166 * Checks if the storage contains the given object
168 * @param Object $object The object to be checked for
169 * @return boolean TRUE|FALSE Returns TRUE if the storage contains the object; otherwise FALSE
171 public function contains($object) {
172 $this->isObject($object);
173 $this->initializeStorage();
174 return array_key_exists(spl_object_hash($object), $this->storage
);
178 * Attaches an object to the storage
180 * @param Object $obj The Object to be attached
183 public function attach($object, $value = NULL) {
184 $this->isObject($object);
185 $this->initializeStorage();
186 if (!$this->contains($object)) {
187 if ($value === NULL) {
190 // TODO Revise this with Karsten
191 $this->storage
[spl_object_hash($object)] = $value;
196 * Detaches an object from the storage
198 * @param Object $object The object to be removed from the storage
201 public function detach($object) {
202 $this->isObject($object);
203 $this->initializeStorage();
204 unset($this->storage
[spl_object_hash($object)]);
208 * Attach all objects to the storage
210 * @param array $objects The objects to be attached to the storage
213 public function addAll($objects) {
214 if (is_array($objects) ||
$objects instanceof Iterator
) {
215 $this->initializeStorage();
216 foreach ($objects as $object) {
217 $this->attach($object);
223 * Detaches all objects from the storage
225 * @param array $objects The objects to be detached from the storage
228 public function removeAll($objects) {
229 if (is_array($objects) ||
$objects instanceof Iterator
) {
230 $this->initializeStorage();
231 foreach ($objects as $object) {
232 $this->detach($object);
238 * Checks, if the given value is an object and throws an exception if not
240 * @param string $value The value to be tested
241 * @return bool TRUE, if the given value is an object
243 protected function isObject($value) {
244 if (!is_object($value)) {
245 throw new Tx_Extbase_MVC_Exception_InvalidArgumentType('Expected parameter to be an object, ' . gettype($offset) . ' given');
251 * Returns this object storage as an array
253 * @return array The object storage
255 public function toArray() {
256 $this->initializeStorage();
257 return $this->storage
;