[+BUGFIX] correct warning in ObjectStorage & LazyObjectStorage
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / LazyObjectStorage.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * A proxy that can replace any object and replaces itself in it's parent on
30 * first access (call, get, set, isset, unset).
31 *
32 * @package Extbase
33 * @subpackage Persistence
34 * @version $Id$
35 */
36 class Tx_Extbase_Persistence_LazyObjectStorage extends Tx_Extbase_Persistence_ObjectStorage implements Tx_Extbase_Persistence_LoadingStrategyInterface {
37
38 /**
39 * This field is only needed to make debugging easier:
40 * If you call current() on a class that implements Iterator, PHP will return the first field of the object
41 * instead of calling the current() method of the interface.
42 * We use this unusual behavior of PHP to return the warning below in this case.
43 *
44 * @var string
45 */
46 private $warning = 'You should never see this warning. If you do, you probably used PHP array functions like current() on the Tx_Extbase_Persistence_LazyObjectStorage. To retrieve the first result, you can use the rewind() and current() methods.';
47
48 /**
49 * @var Tx_Extbase_Persistence_Mapper_DataMapper
50 */
51 protected $dataMapper;
52
53 /**
54 * The object this property is contained in.
55 *
56 * @var object
57 */
58 protected $parentObject;
59
60 /**
61 * The name of the property represented by this proxy.
62 *
63 * @var string
64 */
65 protected $propertyName;
66
67 /**
68 * The raw field value.
69 *
70 * @var mixed
71 */
72 protected $fieldValue;
73
74 /**
75 *
76 * @var bool
77 */
78 protected $isInitialized = FALSE;
79
80 /**
81 * Returns the state of the initialization
82 *
83 * @return void
84 */
85 public function isInitialized() {
86 return $this->isInitialized;
87 }
88
89 /**
90 * Constructs this proxy instance.
91 *
92 * @param object $parentObject The object instance this proxy is part of
93 * @param string $propertyName The name of the proxied property in it's parent
94 * @param mixed $fieldValue The raw field value.
95 */
96 public function __construct($parentObject, $propertyName, $fieldValue) {
97 $this->parentObject = $parentObject;
98 $this->propertyName = $propertyName;
99 $this->fieldValue = $fieldValue;
100 }
101
102 /**
103 * Injects the DataMapper to map nodes to objects
104 *
105 * @param Tx_Extbase_Persistence_Mapper_DataMapper $dataMapper
106 * @return void
107 */
108 public function injectDataMapper(Tx_Extbase_Persistence_Mapper_DataMapper $dataMapper) {
109 $this->dataMapper = $dataMapper;
110 }
111
112 /**
113 * This is a function lazy load implementation.
114 *
115 * @return void
116 */
117 protected function initialize() {
118 if (!$this->isInitialized) {
119 $this->isInitialized = TRUE;
120
121 $objects = $this->dataMapper->fetchRelated($this->parentObject, $this->propertyName, $this->fieldValue, FALSE);
122 foreach ($objects as $object) {
123 parent::attach($object);
124 }
125 $this->_memorizeCleanState();
126 $this->parentObject->_memorizeCleanState($this->propertyName);
127 }
128 }
129
130 // Delegation to the ObjectStorage methods below
131
132 /**
133 * @see Tx_Extbase_Persistence_ObjectStorage::addAll
134 */
135 public function addAll($storage) {
136 $this->initialize();
137 parent::addAll($storage);
138 }
139
140 /**
141 * @see Tx_Extbase_Persistence_ObjectStorage::attach
142 */
143 public function attach($object, $data = NULL) {
144 $this->initialize();
145 parent::attach($object, $data);
146 }
147
148 /**
149 * @see Tx_Extbase_Persistence_ObjectStorage::contains
150 */
151 public function contains($object) {
152 $this->initialize();
153 return parent::contains($object);
154 }
155
156 /**
157 * Counts the elements in the storage array
158 *
159 * @return int The number of elements in the ObjectStorage
160 */
161 public function count() {
162 $columnMap = $this->dataMapper->getDataMap(get_class($this->parentObject))->getColumnMap($this->propertyName);
163 $numberOfElements = NULL;
164 if ($columnMap->getTypeOfRelation() === Tx_Extbase_Persistence_Mapper_ColumnMap::RELATION_HAS_MANY) {
165 $numberOfElements = $this->dataMapper->countRelated($this->parentObject, $this->propertyName, $this->fieldValue);
166 } else {
167 $this->initialize();
168 $numberOfElements = count($this->storage);
169 }
170 if (is_null($numberOfElements)) {
171 throw new Tx_Extbase_Persistence_Exception('The number of elements could not be determined.', 1252514486);
172 }
173 return $numberOfElements;
174 }
175
176 /**
177 * @see Tx_Extbase_Persistence_ObjectStorage::current
178 */
179 public function current() {
180 $this->initialize();
181 return parent::current();
182 }
183
184 /**
185 * @see Tx_Extbase_Persistence_ObjectStorage::detach
186 */
187 public function detach($object) {
188 $this->initialize();
189 parent::detach($object);
190 }
191
192 /**
193 * @see Tx_Extbase_Persistence_ObjectStorage::key
194 */
195 public function key() {
196 $this->initialize();
197 return parent::key();
198 }
199
200 /**
201 * @see Tx_Extbase_Persistence_ObjectStorage::next
202 */
203 public function next() {
204 $this->initialize();
205 parent::next();
206 }
207
208 /**
209 * @see Tx_Extbase_Persistence_ObjectStorage::offsetExists
210 */
211 public function offsetExists($object) {
212 $this->initialize();
213 return parent::offsetExists($object);
214 }
215
216 /**
217 * @see Tx_Extbase_Persistence_ObjectStorage::offsetGet
218 */
219 public function offsetGet($object) {
220 $this->initialize();
221 return parent::offsetGet($object);
222 }
223
224 /**
225 * @see Tx_Extbase_Persistence_ObjectStorage::offsetSet
226 */
227 public function offsetSet($object , $info) {
228 $this->initialize();
229 parent::offsetSet($object, $info);
230 }
231
232 /**
233 * @see Tx_Extbase_Persistence_ObjectStorage::offsetUnset
234 */
235 public function offsetUnset($object) {
236 $this->initialize();
237 parent::offsetUnset($object);
238 }
239
240 /**
241 * @see Tx_Extbase_Persistence_ObjectStorage::removeAll
242 */
243 public function removeAll($storage) {
244 $this->initialize();
245 parent::removeAll($storage);
246 }
247
248 /**
249 * @see Tx_Extbase_Persistence_ObjectStorage::rewind
250 */
251 public function rewind() {
252 $this->initialize();
253 parent::rewind();
254 }
255
256 /**
257 * @see Tx_Extbase_Persistence_ObjectStorage::valid
258 */
259 public function valid() {
260 $this->initialize();
261 return parent::valid();
262 }
263
264 /**
265 * @see Tx_Extbase_Persistence_ObjectStorage::toArray
266 */
267 public function toArray() {
268 $this->initialize();
269 return parent::toArray();
270 }
271
272 }
273 ?>