‪TYPO3CMS  9.5
AbstractDomainObject.php
Go to the documentation of this file.
1 <?php
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 
18 
26 {
30  protected ‪$uid;
31 
35  protected ‪$_localizedUid;
36 
40  protected ‪$_languageUid;
41 
45  protected ‪$_versionedUid;
46 
50  protected ‪$pid;
51 
57  private ‪$_isClone = false;
58 
62  private ‪$_cleanProperties = [];
63 
69  public function ‪getUid()
70  {
71  if ($this->uid !== null) {
72  return (int)‪$this->uid;
73  }
74  return null;
75  }
76 
82  public function ‪setPid(‪$pid)
83  {
84  if (‪$pid === null) {
85  $this->pid = null;
86  } else {
87  $this->pid = (int)‪$pid;
88  }
89  }
90 
96  public function ‪getPid()
97  {
98  if ($this->pid === null) {
99  return null;
100  }
101  return (int)‪$this->pid;
102  }
103 
111  public function ‪_setProperty($propertyName, $propertyValue)
112  {
113  if ($this->‪_hasProperty($propertyName)) {
114  $this->{$propertyName} = $propertyValue;
115  return true;
116  }
117  return false;
118  }
119 
126  public function ‪_getProperty($propertyName)
127  {
128  return $this->{$propertyName};
129  }
130 
136  public function ‪_getProperties()
137  {
138  $properties = get_object_vars($this);
139  foreach ($properties as $propertyName => $propertyValue) {
140  if ($propertyName[0] === '_') {
141  unset($properties[$propertyName]);
142  }
143  }
144  return $properties;
145  }
146 
153  public function ‪_hasProperty($propertyName)
154  {
155  return property_exists($this, $propertyName);
156  }
157 
163  public function ‪_isNew()
164  {
165  return $this->uid === null;
166  }
167 
174  public function ‪_memorizeCleanState($propertyName = null)
175  {
176  if ($propertyName !== null) {
177  $this->‪_memorizePropertyCleanState($propertyName);
178  } else {
179  $this->_cleanProperties = [];
180  $properties = get_object_vars($this);
181  foreach ($properties as $propertyName => $propertyValue) {
182  if ($propertyName[0] === '_') {
183  continue;
184  }
185  // Do not memorize "internal" properties
186  $this->‪_memorizePropertyCleanState($propertyName);
187  }
188  }
189  }
190 
197  public function ‪_memorizePropertyCleanState($propertyName)
198  {
199  $propertyValue = $this->{$propertyName};
200  if (is_object($propertyValue)) {
201  $this->_cleanProperties[$propertyName] = clone $propertyValue;
202  // We need to make sure the clone and the original object
203  // are identical when compared with == (see _isDirty()).
204  // After the cloning, the Domain Object will have the property
205  // "isClone" set to TRUE, so we manually have to set it to FALSE
206  // again. Possible fix: Somehow get rid of the "isClone" property,
207  // which is currently needed in Fluid.
208  if ($propertyValue instanceof \‪TYPO3\CMS\‪Extbase\DomainObject\‪AbstractDomainObject) {
209  $this->_cleanProperties[$propertyName]->_setClone(false);
210  }
211  } else {
212  $this->_cleanProperties[$propertyName] = $propertyValue;
213  }
214  }
215 
221  public function ‪_getCleanProperties()
222  {
224  }
225 
233  public function ‪_getCleanProperty($propertyName)
234  {
235  return $this->_cleanProperties[$propertyName] ?? null;
236  }
237 
245  public function ‪_isDirty($propertyName = null)
246  {
247  if ($this->uid !== null && $this->‪_getCleanProperty('uid') !== null && $this->uid != $this->‪_getCleanProperty('uid')) {
248  throw new \TYPO3\CMS\Extbase\Persistence\Generic\Exception\TooDirtyException('The uid "' . $this->uid . '" has been modified, that is simply too much.', 1222871239);
249  }
250 
251  if ($propertyName === null) {
252  foreach ($this->‪_getCleanProperties() as $propertyName => $cleanPropertyValue) {
253  if ($this->‪isPropertyDirty($cleanPropertyValue, $this->{$propertyName}) === true) {
254  return true;
255  }
256  }
257  } else {
258  if ($this->‪isPropertyDirty($this->‪_getCleanProperty($propertyName), $this->{$propertyName}) === true) {
259  return true;
260  }
261  }
262  return false;
263  }
264 
272  protected function ‪isPropertyDirty($previousValue, $currentValue)
273  {
274  // In case it is an object and it implements the ObjectMonitoringInterface, we call _isDirty() instead of a simple comparison of objects.
275  // We do this, because if the object itself contains a lazy loaded property, the comparison of the objects might fail even if the object didn't change
276  if (is_object($currentValue)) {
277  $currentTypeString = null;
278  if ($currentValue instanceof LazyLoadingProxy) {
279  $currentTypeString = $currentValue->_getTypeAndUidString();
280  } elseif ($currentValue instanceof DomainObjectInterface) {
281  $currentTypeString = get_class($currentValue) . ':' . $currentValue->getUid();
282  }
283 
284  if ($currentTypeString !== null) {
285  $previousTypeString = null;
286  if ($previousValue instanceof LazyLoadingProxy) {
287  $previousTypeString = $previousValue->_getTypeAndUidString();
288  } elseif ($previousValue instanceof DomainObjectInterface) {
289  $previousTypeString = get_class($previousValue) . ':' . $previousValue->getUid();
290  }
291 
292  $result = $currentTypeString !== $previousTypeString;
293  } elseif ($currentValue instanceof \‪TYPO3\CMS\‪Extbase\Persistence\ObjectMonitoringInterface) {
294  $result = !is_object($previousValue) || $currentValue->_isDirty() || get_class($previousValue) !== get_class($currentValue);
295  } else {
296  // For all other objects we do only a simple comparison (!=) as we want cloned objects to return the same values.
297  $result = $previousValue != $currentValue;
298  }
299  } else {
300  $result = $previousValue !== $currentValue;
301  }
302  return $result;
303  }
304 
310  public function ‪_isClone()
311  {
312  return ‪$this->_isClone;
313  }
314 
323  public function ‪_setClone($clone)
324  {
325  $this->‪_isClone = (bool)$clone;
326  }
327 
331  public function ‪__clone()
332  {
333  $this->‪_isClone = true;
334  }
335 
341  public function ‪__toString()
342  {
343  return static::class . ':' . (string)$this->uid;
344  }
345 }
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\$_isClone
‪bool $_isClone
Definition: AbstractDomainObject.php:51
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_memorizeCleanState
‪_memorizeCleanState($propertyName=null)
Definition: AbstractDomainObject.php:167
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_isDirty
‪bool _isDirty($propertyName=null)
Definition: AbstractDomainObject.php:238
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_setProperty
‪bool _setProperty($propertyName, $propertyValue)
Definition: AbstractDomainObject.php:104
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_getCleanProperties
‪array _getCleanProperties()
Definition: AbstractDomainObject.php:214
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\getPid
‪int getPid()
Definition: AbstractDomainObject.php:89
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\$_versionedUid
‪int $_versionedUid
Definition: AbstractDomainObject.php:41
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_hasProperty
‪bool _hasProperty($propertyName)
Definition: AbstractDomainObject.php:146
‪TYPO3
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\__toString
‪string __toString()
Definition: AbstractDomainObject.php:334
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_getCleanProperty
‪mixed _getCleanProperty($propertyName)
Definition: AbstractDomainObject.php:226
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\getUid
‪int getUid()
Definition: AbstractDomainObject.php:62
‪TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
Definition: DomainObjectInterface.php:26
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject
Definition: AbstractDomainObject.php:26
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\setPid
‪setPid($pid)
Definition: AbstractDomainObject.php:75
‪TYPO3\CMS\Extbase\DomainObject
Definition: AbstractDomainObject.php:2
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_getProperty
‪mixed _getProperty($propertyName)
Definition: AbstractDomainObject.php:119
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\$_cleanProperties
‪array $_cleanProperties
Definition: AbstractDomainObject.php:55
‪TYPO3\CMS\Extbase\Persistence\ObjectMonitoringInterface\_isDirty
‪bool _isDirty()
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\$pid
‪int $pid
Definition: AbstractDomainObject.php:45
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_getProperties
‪array _getProperties()
Definition: AbstractDomainObject.php:129
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_memorizePropertyCleanState
‪_memorizePropertyCleanState($propertyName)
Definition: AbstractDomainObject.php:190
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy
Definition: LazyLoadingProxy.php:28
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\$_languageUid
‪int $_languageUid
Definition: AbstractDomainObject.php:37
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\$uid
‪int $uid
Definition: AbstractDomainObject.php:29
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_isNew
‪bool _isNew()
Definition: AbstractDomainObject.php:156
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\$_localizedUid
‪int $_localizedUid
Definition: AbstractDomainObject.php:33
‪TYPO3\CMS\Extbase\Persistence\ObjectMonitoringInterface\_memorizeCleanState
‪_memorizeCleanState()
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_setClone
‪_setClone($clone)
Definition: AbstractDomainObject.php:316
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\isPropertyDirty
‪bool isPropertyDirty($previousValue, $currentValue)
Definition: AbstractDomainObject.php:265
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_isClone
‪bool _isClone()
Definition: AbstractDomainObject.php:303
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\__clone
‪__clone()
Definition: AbstractDomainObject.php:324
‪TYPO3\CMS\Extbase\Persistence\ObjectMonitoringInterface
Definition: ObjectMonitoringInterface.php:24