‪TYPO3CMS  10.4
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->{$propertyName};
132  }
133 
140  public function ‪_getProperties(): array
141  {
142  $properties = get_object_vars($this);
143  foreach ($properties as $propertyName => $propertyValue) {
144  if ($propertyName[0] === '_') {
145  unset($properties[$propertyName]);
146  }
147  }
148  return $properties;
149  }
150 
158  public function ‪_hasProperty($propertyName)
159  {
160  return property_exists($this, $propertyName);
161  }
162 
169  public function ‪_isNew(): bool
170  {
171  return $this->uid === null;
172  }
173 
180  public function ‪_memorizeCleanState($propertyName = null)
181  {
182  if ($propertyName !== null) {
183  $this->‪_memorizePropertyCleanState($propertyName);
184  } else {
185  $this->_cleanProperties = [];
186  $properties = get_object_vars($this);
187  foreach ($properties as $propertyName => $propertyValue) {
188  if ($propertyName[0] === '_') {
189  continue;
190  }
191  // Do not memorize "internal" properties
192  $this->‪_memorizePropertyCleanState($propertyName);
193  }
194  }
195  }
196 
203  public function ‪_memorizePropertyCleanState($propertyName)
204  {
205  $propertyValue = $this->{$propertyName};
206  if (is_object($propertyValue)) {
207  $this->_cleanProperties[$propertyName] = clone $propertyValue;
208  // We need to make sure the clone and the original object
209  // are identical when compared with == (see _isDirty()).
210  // After the cloning, the Domain Object will have the property
211  // "isClone" set to TRUE, so we manually have to set it to FALSE
212  // again. Possible fix: Somehow get rid of the "isClone" property,
213  // which is currently needed in Fluid.
214  if ($propertyValue instanceof \‪TYPO3\CMS\‪Extbase\DomainObject\‪AbstractDomainObject) {
215  $this->_cleanProperties[$propertyName]->_setClone(false);
216  }
217  } else {
218  $this->_cleanProperties[$propertyName] = $propertyValue;
219  }
220  }
221 
227  public function ‪_getCleanProperties()
228  {
230  }
231 
240  public function ‪_getCleanProperty(string $propertyName)
241  {
242  return $this->_cleanProperties[$propertyName] ?? null;
243  }
244 
252  public function ‪_isDirty($propertyName = null)
253  {
254  if ($this->uid !== null && $this->‪_getCleanProperty('uid') !== null && $this->uid != $this->‪_getCleanProperty('uid')) {
255  throw new TooDirtyException('The uid "' . $this->uid . '" has been modified, that is simply too much.', 1222871239);
256  }
257 
258  if ($propertyName === null) {
259  foreach ($this->‪_getCleanProperties() as $propertyName => $cleanPropertyValue) {
260  if ($this->‪isPropertyDirty($cleanPropertyValue, $this->{$propertyName}) === true) {
261  return true;
262  }
263  }
264  } else {
265  if ($this->‪isPropertyDirty($this->‪_getCleanProperty($propertyName), $this->{$propertyName}) === true) {
266  return true;
267  }
268  }
269  return false;
270  }
271 
279  protected function ‪isPropertyDirty($previousValue, $currentValue)
280  {
281  // In case it is an object and it implements the ObjectMonitoringInterface, we call _isDirty() instead of a simple comparison of objects.
282  // 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
283  if (is_object($currentValue)) {
284  $currentTypeString = null;
285  if ($currentValue instanceof LazyLoadingProxy) {
286  $currentTypeString = $currentValue->_getTypeAndUidString();
287  } elseif ($currentValue instanceof DomainObjectInterface) {
288  $currentTypeString = get_class($currentValue) . ':' . $currentValue->getUid();
289  }
290 
291  if ($currentTypeString !== null) {
292  $previousTypeString = null;
293  if ($previousValue instanceof LazyLoadingProxy) {
294  $previousTypeString = $previousValue->_getTypeAndUidString();
295  } elseif ($previousValue instanceof DomainObjectInterface) {
296  $previousTypeString = get_class($previousValue) . ':' . $previousValue->getUid();
297  }
298 
299  $result = $currentTypeString !== $previousTypeString;
300  } elseif ($currentValue instanceof ObjectMonitoringInterface) {
301  $result = !is_object($previousValue) || $currentValue->_isDirty() || get_class($previousValue) !== get_class($currentValue);
302  } else {
303  // For all other objects we do only a simple comparison (!=) as we want cloned objects to return the same values.
304  $result = $previousValue != $currentValue;
305  }
306  } else {
307  $result = $previousValue !== $currentValue;
308  }
309  return $result;
310  }
311 
317  public function ‪_isClone()
318  {
319  return ‪$this->_isClone;
320  }
321 
330  public function ‪_setClone($clone)
331  {
332  $this->‪_isClone = (bool)$clone;
333  }
334 
338  public function ‪__clone()
339  {
340  $this->‪_isClone = true;
341  }
342 
348  public function ‪__toString()
349  {
350  return static::class . ':' . (string)$this->uid;
351  }
352 }
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\$_isClone
‪bool $_isClone
Definition: AbstractDomainObject.php:56
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_memorizeCleanState
‪_memorizeCleanState($propertyName=null)
Definition: AbstractDomainObject.php:173
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_isDirty
‪bool _isDirty($propertyName=null)
Definition: AbstractDomainObject.php:245
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_getCleanProperties
‪array _getCleanProperties()
Definition: AbstractDomainObject.php:220
‪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:151
‪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:341
‪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:133
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_memorizePropertyCleanState
‪_memorizePropertyCleanState($propertyName)
Definition: AbstractDomainObject.php:196
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_getCleanProperty
‪mixed _getCleanProperty(string $propertyName)
Definition: AbstractDomainObject.php:233
‪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:162
‪TYPO3\CMS\Extbase\Persistence\Generic\Exception\TooDirtyException
Definition: TooDirtyException.php:26
‪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:323
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\isPropertyDirty
‪bool isPropertyDirty($previousValue, $currentValue)
Definition: AbstractDomainObject.php:272
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\_isClone
‪bool _isClone()
Definition: AbstractDomainObject.php:310
‪TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject\__clone
‪__clone()
Definition: AbstractDomainObject.php:331
‪TYPO3\CMS\Extbase\Persistence\ObjectMonitoringInterface
Definition: ObjectMonitoringInterface.php:25