* enabled $session->commit()
[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 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 /**
65 * Does the Storage contains the given object
66 *
67 * @param Object $obj
68 * @return boolean TRUE|FALSE The result TRUE if the Storage contains the object; the result FALSE if not
69 */
70 public function contains($obj) {
71 if (is_object($obj)) {
72 foreach($this->storage as $object) {
73 if ($object === $obj) return TRUE;
74 }
75 }
76 return FALSE;
77 }
78
79 /**
80 * Attaches an object to the storage
81 *
82 * @param Object $obj
83 * @return void
84 */
85 public function attach($obj) {
86 if (is_object($obj) && !$this->contains($obj)) {
87 $this->storage[] = $obj;
88 }
89 }
90
91 /**
92 * Detaches an object to the storage
93 *
94 * @param Object $obj
95 * @return void
96 */
97 public function detach($obj) {
98 if (is_object($obj)) {
99 foreach($this->storage as $key => $object) {
100 if ($object === $obj) {
101 unset($this->storage[$key]);
102 $this->rewind();
103 return;
104 }
105 }
106 }
107 }
108
109 /**
110 * Removes all object from the storage
111 *
112 * @return void
113 */
114 public function removeAll() {
115 $this->storage = array();
116 }
117
118 }
119
120 ?>