* prepared the Persistence Repository
[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 {
32
33 /**
34 * The array holding references to the stored objects.
35 *
36 * @var string
37 **/
38 private $storage = array();
39
40 /**
41 * An index for the Iterator interface
42 *
43 * @var string
44 **/
45 private $index = 0;
46
47 function rewind() {
48 rewind($this->storage);
49 }
50
51 function valid() {
52 return key($this->storage) !== false;
53 }
54
55 function key() {
56 return $this->index;
57 }
58
59 function current() {
60 return current($this->storage);
61 }
62
63 function next() {
64 next($this->storage);
65 $this->index++;
66 }
67
68 function count() {
69 return count($this->storage);
70 }
71
72 /**
73 * Does the Storage contains the given object
74 *
75 * @param Object $obj
76 * @return boolean TRUE|FALSE The result TRUE if the Storage contains the object; the result FALSE if not
77 */
78 function contains($obj) {
79 if (is_object($obj)) {
80 foreach($this->storage as $object) {
81 if ($object === $obj) return true;
82 }
83 }
84 return false;
85 }
86
87 /**
88 * Attaches an object to the storage
89 *
90 * @param Object $obj
91 * @return void
92 */
93 function attach($obj) {
94 if (is_object($obj) && !$this->contains($obj)) {
95 $this->storage[] = $obj;
96 }
97 }
98
99 /**
100 * Detaches an object to the storage
101 *
102 * @param Object $obj
103 * @return void
104 */
105 function detach($obj) {
106 if (is_object($obj)) {
107 foreach($this->storage as $idx => $object) {
108 if ($object === $obj) {
109 unset($this->storage[$idx]);
110 $this->rewind();
111 return;
112 }
113 }
114 }
115 }
116 }
117
118 ?>