Extbase:
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / DomainObject / AbstractEntity.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * An abstract Entity. An Entity is an object fundamentally defined not by its attributes,
27 * but by a thread of continuity and identity (e.g. a person).
28 *
29 * @package Extbase
30 * @subpackage DomainObject
31 * @version $ID:$
32 */
33 abstract class Tx_Extbase_DomainObject_AbstractEntity extends Tx_Extbase_DomainObject_AbstractDomainObject {
34
35 /**
36 * @var An array holding the clean property values. Set right after reconstitution of the object
37 */
38 private $_cleanProperties = NULL;
39
40 /**
41 * Register an object's clean state, e.g. after it has been reconstituted
42 * from the database
43 *
44 * @return void
45 * @internal
46 */
47 public function _memorizeCleanState() {
48 // TODO Remove dependency to $dataMapper
49 $dataMapper = t3lib_div::makeInstance('Tx_Extbase_Persistence_Mapper_ObjectRelationalMapper'); // singleton
50 $this->_cleanProperties = array();
51 $properties = get_object_vars($this);
52 foreach ($properties as $propertyName => $propertyValue) {
53 if ($dataMapper->isPersistableProperty(get_class($this), $propertyName)) {
54 $this->_cleanProperties[$propertyName] = $this->$propertyName;
55 }
56 }
57 }
58
59 /**
60 * Returns a hash map of dirty properties and $values
61 *
62 * @return array
63 * @internal
64 */
65 public function _getDirtyProperties() {
66 if (!is_array($this->_cleanProperties)) throw new Tx_Extbase_Persistence_Exception_CleanStateNotMemorized('The clean state of the object "' . get_class($this) . '" has not been memorized before asking _isDirty().', 1233309106);
67 if ($this->uid !== NULL && $this->uid != $this->_cleanProperties['uid']) throw new Tx_Extbase_Persistence_Exception_TooDirty('The uid "' . $this->uid . '" has been modified, that is simply too much.', 1222871239);
68 $dirtyProperties = array();
69 foreach ($this->_cleanProperties as $propertyName => $propertyValue) {
70 if ($this->$propertyName !== $propertyValue) {
71 $dirtyProperties[$propertyName] = $this->$propertyName;
72 }
73 }
74 return $dirtyProperties;
75 }
76
77 /**
78 * Returns TRUE if the properties were modified after reconstitution
79 *
80 * @return boolean
81 * @internal
82 */
83 public function _isDirty($propertyName = NULL) {
84 if (!is_array($this->_cleanProperties)) throw new Tx_Extbase_Persistence_Exception_CleanStateNotMemorized('The clean state of the object "' . get_class($this) . '" has not been memorized before asking _isDirty().', 1233309106);
85 if ($this->uid !== NULL && $this->uid != $this->_cleanProperties['uid']) throw new Tx_Extbase_Persistence_Exception_TooDirty('The uid "' . $this->uid . '" has been modified, that is simply too much.', 1222871239);
86 $result = FALSE;
87 if ($propertyName !== NULL) {
88 $result = $this->_cleanProperties[$propertyName] !== $this->$propertyName;
89 } else {
90 foreach ($this->_cleanProperties as $propertyName => $propertyValue) {
91 if ($this->$propertyName !== $propertyValue) {
92 $result = TRUE;
93 break;
94 }
95 }
96 }
97 return $result;
98 }
99
100 }
101 ?>