* renamed TcaMapper to ObjectRelationalMapper
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / TX_EXTMVC_Persistence_ObjectStorage.php
1 <?php
2 declare(ENCODING = 'utf-8');
3
4 /* *
5 * This script belongs to the FLOW3 framework. *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License as published by the *
9 * Free Software Foundation, either version 3 of the License, or (at your *
10 * option) any later version. *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23
24 /**
25 * The storage for objects. It ensures the uniqueness of an object in the storage. It's a remake of the
26 * SplObjectStorage introduced in a usable version in PHP 5.3.
27 *
28 * @version $Id:$
29 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
30 */
31 class TX_EXTMVC_Persistence_ObjectStorage implements Iterator, Countable, ArrayAccess {
32
33 /**
34 * The array holding references to the stored objects.
35 *
36 * @var string
37 **/
38 private $storage = array();
39
40 public function rewind() {
41 reset($this->storage);
42 }
43
44 public function valid() {
45 return $this->current() !== FALSE;
46 }
47
48 public function key() {
49 return key($this->storage);
50 }
51
52 public function current() {
53 return current($this->storage);
54 }
55
56 public function next() {
57 next($this->storage);
58 }
59
60 public function count() {
61 return count($this->storage);
62 }
63
64 public function offsetSet($offset, $obj) {
65 if (is_object($obj) && !$this->contains($obj)) {
66 $this->storage[$offset] = $obj;
67 }
68 }
69
70 public function offsetExists($offset) {
71 return isset($this->storage[$offset]);
72 }
73
74 public function offsetUnset($offset) {
75 unset($this->storage[$offset]);
76 }
77
78 public function offsetGet($offset) {
79 return isset($this->storage[$offset]) ? $this->storage[$offset] : NULL;
80 }
81
82 /**
83 * Does the Storage contains the given object
84 *
85 * @param Object $obj
86 * @return boolean TRUE|FALSE The result TRUE if the Storage contains the object; the result FALSE if not
87 */
88 public function contains($obj) {
89 if (is_object($obj)) {
90 foreach($this->storage as $object) {
91 if ($object === $obj) return TRUE;
92 }
93 }
94 return FALSE;
95 }
96
97 /**
98 * Attaches an object to the storage
99 *
100 * @param Object $obj
101 * @return void
102 */
103 public function attach($obj) {
104 if (is_object($obj) && !$this->contains($obj)) {
105 $this->storage[] = $obj;
106 }
107 }
108
109 /**
110 * Detaches an object to the storage
111 *
112 * @param Object $obj
113 * @return void
114 */
115 public function detach($obj) {
116 if (is_object($obj)) {
117 foreach($this->storage as $key => $object) {
118 if ($object === $obj) {
119 unset($this->storage[$key]);
120 $this->rewind();
121 return;
122 }
123 }
124 }
125 }
126
127 /**
128 * Removes all object from the storage
129 *
130 * @return void
131 */
132 public function removeAll() {
133 $this->storage = array();
134 }
135
136 /**
137 * Returns this object storage as an array
138 *
139 * @return array The object storage
140 * @author Jochen Rau <jochen.rau@typoplanet.de>
141 */
142 public function toArray() {
143 return $this->storage;
144 }
145
146 }
147
148 ?>