[CLEANUP] Improve the @param/@return/@var PHPDoc
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / Generic / LazyLoadingProxy.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Persistence\Generic;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface;
18
19 /**
20 * A proxy that can replace any object and replaces itself in it's parent on
21 * first access (call, get, set, isset, unset).
22 */
23 class LazyLoadingProxy implements \Iterator, \TYPO3\CMS\Extbase\Persistence\Generic\LoadingStrategyInterface {
24
25 /**
26 * @var \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper
27 * @inject
28 */
29 protected $dataMapper;
30
31 /**
32 * The object this property is contained in.
33 *
34 * @var DomainObjectInterface
35 */
36 private $parentObject;
37
38 /**
39 * The name of the property represented by this proxy.
40 *
41 * @var string
42 */
43 private $propertyName;
44
45 /**
46 * The raw field value.
47 *
48 * @var mixed
49 */
50 private $fieldValue;
51
52 /**
53 * Constructs this proxy instance.
54 *
55 * @param DomainObjectInterface $parentObject The object instance this proxy is part of
56 * @param string $propertyName The name of the proxied property in it's parent
57 * @param mixed $fieldValue The raw field value.
58 */
59 public function __construct($parentObject, $propertyName, $fieldValue) {
60 $this->parentObject = $parentObject;
61 $this->propertyName = $propertyName;
62 $this->fieldValue = $fieldValue;
63 }
64
65 /**
66 * Populate this proxy by asking the $population closure.
67 *
68 * @return object The instance (hopefully) returned
69 */
70 public function _loadRealInstance() {
71 // this check safeguards against a proxy being activated multiple times
72 // usually that does not happen, but if the proxy is held from outside
73 // its parent ... the result would be weird.
74 if ($this->parentObject->_getProperty($this->propertyName) instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy) {
75 $objects = $this->dataMapper->fetchRelated($this->parentObject, $this->propertyName, $this->fieldValue, FALSE, FALSE);
76 $propertyValue = $this->dataMapper->mapResultToPropertyValue($this->parentObject, $this->propertyName, $objects);
77 $this->parentObject->_setProperty($this->propertyName, $propertyValue);
78 $this->parentObject->_memorizeCleanState($this->propertyName);
79 return $propertyValue;
80 } else {
81 return $this->parentObject->_getProperty($this->propertyName);
82 }
83 }
84
85 /**
86 * Magic method call implementation.
87 *
88 * @param string $methodName The name of the property to get
89 * @param array $arguments The arguments given to the call
90 * @return mixed
91 */
92 public function __call($methodName, $arguments) {
93 $realInstance = $this->_loadRealInstance();
94 if (!is_object($realInstance)) {
95 return NULL;
96 }
97 return call_user_func_array(array($realInstance, $methodName), $arguments);
98 }
99
100 /**
101 * Magic get call implementation.
102 *
103 * @param string $propertyName The name of the property to get
104 * @return mixed
105 */
106 public function __get($propertyName) {
107 $realInstance = $this->_loadRealInstance();
108 return $realInstance->{$propertyName};
109 }
110
111 /**
112 * Magic set call implementation.
113 *
114 * @param string $propertyName The name of the property to set
115 * @param mixed $value The value for the property to set
116 * @return void
117 */
118 public function __set($propertyName, $value) {
119 $realInstance = $this->_loadRealInstance();
120 $realInstance->{$propertyName} = $value;
121 }
122
123 /**
124 * Magic isset call implementation.
125 *
126 * @param string $propertyName The name of the property to check
127 * @return bool
128 */
129 public function __isset($propertyName) {
130 $realInstance = $this->_loadRealInstance();
131 return isset($realInstance->{$propertyName});
132 }
133
134 /**
135 * Magic unset call implementation.
136 *
137 * @param string $propertyName The name of the property to unset
138 * @return void
139 */
140 public function __unset($propertyName) {
141 $realInstance = $this->_loadRealInstance();
142 unset($realInstance->{$propertyName});
143 }
144
145 /**
146 * Magic toString call implementation.
147 *
148 * @return string
149 */
150 public function __toString() {
151 $realInstance = $this->_loadRealInstance();
152 return $realInstance->__toString();
153 }
154
155 /**
156 * Returns the current value of the storage array
157 *
158 * @return mixed
159 */
160 public function current() {
161 $realInstance = $this->_loadRealInstance();
162 return current($realInstance);
163 }
164
165 /**
166 * Returns the current key storage array
167 *
168 * @return int
169 */
170 public function key() {
171 $realInstance = $this->_loadRealInstance();
172 return key($realInstance);
173 }
174
175 /**
176 * Returns the next position of the storage array
177 *
178 * @return void
179 */
180 public function next() {
181 $realInstance = $this->_loadRealInstance();
182 next($realInstance);
183 }
184
185 /**
186 * Resets the array pointer of the storage
187 *
188 * @return void
189 */
190 public function rewind() {
191 $realInstance = $this->_loadRealInstance();
192 reset($realInstance);
193 }
194
195 /**
196 * Checks if the array pointer of the storage points to a valid position
197 *
198 * @return bool
199 */
200 public function valid() {
201 return $this->current() !== FALSE;
202 }
203
204 }