‪TYPO3CMS  11.5
AbstractDomainObject.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
23 
31 {
35  protected ‪$uid;
36 
40  protected ‪$_localizedUid;
41 
45  protected ‪$_languageUid;
46 
50  protected ‪$_versionedUid;
51 
55  protected ‪$pid;
56 
62  private ‪$_isClone = false;
63 
67  private ‪$_cleanProperties = [];
68 
74  public function ‪getUid(): ?int
75  {
76  if ($this->uid !== null) {
77  return (int)‪$this->uid;
78  }
79  return null;
80  }
81 
87  public function ‪setPid(int ‪$pid): void
88  {
89  $this->pid = ‪$pid;
90  }
91 
97  public function ‪getPid(): ?int
98  {
99  if ($this->pid === null) {
100  return null;
101  }
102  return (int)‪$this->pid;
103  }
104 
113  public function ‪_setProperty(string $propertyName, $propertyValue)
114  {
115  if ($this->‪_hasProperty($propertyName)) {
116  $this->{$propertyName} = $propertyValue;
117  return true;
118  }
119  return false;
120  }
121 
129  public function ‪_getProperty(string $propertyName)
130  {
131  return $this->‪_hasProperty($propertyName) && isset($this->{$propertyName})
132  ? $this->{$propertyName}
133  : null;
134  }
135 
142  public function ‪_getProperties(): array
143  {
144  $properties = get_object_vars($this);
145  foreach ($properties as $propertyName => $propertyValue) {
146  if ($propertyName[0] === '_') {
147  unset($properties[$propertyName]);
148  }
149  }
150  return $properties;
151  }
152 
160  public function ‪_hasProperty($propertyName)
161  {
162  return property_exists($this, $propertyName);
163  }
164 
171  public function ‪_isNew(): bool
172  {
173  return $this->uid === null;
174  }
175 
182  public function ‪_memorizeCleanState($propertyName = null)
183  {
184  if ($propertyName !== null) {
185  $this->‪_memorizePropertyCleanState($propertyName);
186  } else {
187  $this->_cleanProperties = [];
188  $properties = get_object_vars($this);
189  foreach ($properties as $propertyName => $propertyValue) {
190  if ($propertyName[0] === '_') {
191  continue;
192  }
193  // Do not memorize "internal" properties
194  $this->‪_memorizePropertyCleanState($propertyName);
195  }
196  }
197  }
198 
205  public function ‪_memorizePropertyCleanState($propertyName)
206  {
207  $propertyValue = $this->{$propertyName};
208  if (is_object($propertyValue)) {
209  $this->_cleanProperties[$propertyName] = clone $propertyValue;
210  // We need to make sure the clone and the original object
211  // are identical when compared with == (see _isDirty()).
212  // After the cloning, the Domain Object will have the property
213  // "isClone" set to TRUE, so we manually have to set it to FALSE
214  // again. Possible fix: Somehow get rid of the "isClone" property,
215  // which is currently needed in Fluid.
216  if ($propertyValue instanceof \‪TYPO3\CMS\‪Extbase\DomainObject\‪AbstractDomainObject) {
217  $this->_cleanProperties[$propertyName]->_setClone(false);
218  }
219  } else {
220  $this->_cleanProperties[$propertyName] = $propertyValue;
221  }
222  }
223 
229  public function ‪_getCleanProperties()
230  {
232  }
233 
242  public function ‪_getCleanProperty(string $propertyName)
243  {
244  return $this->_cleanProperties[$propertyName] ?? null;
245  }
246 
254  public function ‪_isDirty($propertyName = null)
255  {
256  if ($this->uid !== null && $this->‪_getCleanProperty('uid') !== null && $this->uid != $this->‪_getCleanProperty('uid')) {
257  throw new TooDirtyException('The uid "' . $this->uid . '" has been modified, that is simply too much.', 1222871239);
258  }
259 
260  if ($propertyName === null) {
261  foreach ($this->‪_getCleanProperties() as $propertyName => $cleanPropertyValue) {
262  if ($this->‪isPropertyDirty($cleanPropertyValue, $this->{$propertyName}) === true) {
263  return true;
264  }
265  }
266  } else {
267  if ($this->‪isPropertyDirty($this->‪_getCleanProperty($propertyName), $this->‪_getProperty($propertyName)) === true) {
268  return true;
269  }
270  }
271  return false;
272  }
273 
281  protected function ‪isPropertyDirty($previousValue, $currentValue)
282  {
283  // In case it is an object and it implements the ObjectMonitoringInterface, we call _isDirty() instead of a simple comparison of objects.
284  // 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
285  if (is_object($currentValue)) {
286  $currentTypeString = null;
287  if ($currentValue instanceof LazyLoadingProxy) {
288  $currentTypeString = $currentValue->_getTypeAndUidString();
289  } elseif ($currentValue instanceof DomainObjectInterface) {
290  $currentTypeString = get_class($currentValue) . ':' . $currentValue->getUid();
291  }
292 
293  if ($currentTypeString !== null) {
294  $previousTypeString = null;
295  if ($previousValue instanceof LazyLoadingProxy) {
296  $previousTypeString = $previousValue->_getTypeAndUidString();
297  } elseif ($previousValue instanceof DomainObjectInterface) {
298  $previousTypeString = get_class($previousValue) . ':' . $previousValue->getUid();
299  }
300 
301  $result = $currentTypeString !== $previousTypeString;
302  } elseif ($currentValue instanceof ObjectMonitoringInterface) {
303  $result = !is_object($previousValue) || $currentValue->_isDirty() || get_class($previousValue) !== get_class($currentValue);
304  } else {
305  // For all other objects we do only a simple comparison (!=) as we want cloned objects to return the same values.
306  $result = $previousValue != $currentValue;
307  }
308  } else {
309  $result = $previousValue !== $currentValue;
310  }
311  return $result;
312  }
313 
319  public function ‪_isClone()
320  {
321  return ‪$this->_isClone;
322  }
323 
332  public function ‪_setClone($clone)
333  {
334  $this->‪_isClone = (bool)$clone;
335  }
336 
340  public function ‪__clone()
341  {
342  $this->‪_isClone = true;
343  }
344 
350  public function ‪__toString()
351  {
352  return static::class . ':' . (string)$this->uid;
353  }
354 }
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\$_isClone
‪bool $_isClone
Definition: AbstractDomainObject.php:56
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_memorizeCleanState
‪_memorizeCleanState($propertyName=null)
Definition: AbstractDomainObject.php:175
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_isDirty
‪bool _isDirty($propertyName=null)
Definition: AbstractDomainObject.php:247
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_getCleanProperties
‪array _getCleanProperties()
Definition: AbstractDomainObject.php:222
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:18
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\getUid
‪int null getUid()
Definition: AbstractDomainObject.php:67
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\$_versionedUid
‪int $_versionedUid
Definition: AbstractDomainObject.php:46
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_hasProperty
‪bool _hasProperty($propertyName)
Definition: AbstractDomainObject.php:153
‪TYPO3
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_setProperty
‪bool _setProperty(string $propertyName, $propertyValue)
Definition: AbstractDomainObject.php:106
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\__toString
‪string __toString()
Definition: AbstractDomainObject.php:343
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\getPid
‪int null getPid()
Definition: AbstractDomainObject.php:90
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\setPid
‪setPid(int $pid)
Definition: AbstractDomainObject.php:80
‪TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface
Definition: DomainObjectInterface.php:29
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject
Definition: AbstractDomainObject.php:31
‪TYPO3\CMS\Extbase\DomainObject
Definition: AbstractDomainObject.php:18
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\$_cleanProperties
‪array $_cleanProperties
Definition: AbstractDomainObject.php:60
‪TYPO3\CMS\Extbase\Persistence\ObjectMonitoringInterface\_isDirty
‪bool _isDirty()
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\$pid
‪int $pid
Definition: AbstractDomainObject.php:50
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_getProperties
‪array _getProperties()
Definition: AbstractDomainObject.php:135
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_memorizePropertyCleanState
‪_memorizePropertyCleanState($propertyName)
Definition: AbstractDomainObject.php:198
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_getCleanProperty
‪mixed _getCleanProperty(string $propertyName)
Definition: AbstractDomainObject.php:235
‪TYPO3\CMS\Extbase\Persistence\Generic\LazyLoadingProxy
Definition: LazyLoadingProxy.php:29
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\$_languageUid
‪int $_languageUid
Definition: AbstractDomainObject.php:42
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\$uid
‪int $uid
Definition: AbstractDomainObject.php:34
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_getProperty
‪mixed _getProperty(string $propertyName)
Definition: AbstractDomainObject.php:122
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_isNew
‪bool _isNew()
Definition: AbstractDomainObject.php:164
‪TYPO3\CMS\Extbase\Persistence\Generic\Exception\TooDirtyException
Definition: TooDirtyException.php:25
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\$_localizedUid
‪int $_localizedUid
Definition: AbstractDomainObject.php:38
‪TYPO3\CMS\Extbase\Persistence\ObjectMonitoringInterface\_memorizeCleanState
‪_memorizeCleanState()
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_setClone
‪_setClone($clone)
Definition: AbstractDomainObject.php:325
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\isPropertyDirty
‪bool isPropertyDirty($previousValue, $currentValue)
Definition: AbstractDomainObject.php:274
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_isClone
‪bool _isClone()
Definition: AbstractDomainObject.php:312
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\__clone
‪__clone()
Definition: AbstractDomainObject.php:333
‪TYPO3\CMS\Extbase\Persistence\ObjectMonitoringInterface
Definition: ObjectMonitoringInterface.php:25