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