* renamed TcaMapper to ObjectRelationalMapper
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / DomainObject / TX_EXTMVC_DomainObject_AbstractDomainObject.php
1 <?php
2 declare(ENCODING = 'utf-8');
3
4 /* *
5 * This script belongs to the FLOW3 framework. *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License as published by the *
9 * Free Software Foundation, either version 3 of the License, or (at your *
10 * option) any later version. *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23
24 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Persistence/Mapper/TX_EXTMVC_Persistence_Mapper_ObjectRelationalMapper.php');
25
26 /**
27 * A generic Domain Object
28 *
29 * @version $Id:$
30 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
31 */
32 abstract class TX_EXTMVC_DomainObject_AbstractDomainObject {
33
34 /**
35 * @var string The uid
36 */
37 protected $uid;
38
39 /**
40 * The generic constructor. If you want to implement your own __constructor() method in your Domain Object you have to call
41 * $this->initializeObject() in the first line of your constructor.
42 *
43 * @var array
44 */
45 private $cleanProperties = NULL;
46
47 public function __construct() {
48 $this->initializeObject();
49 }
50
51 /**
52 * This is the magic wakeup() method. It's invoked by the unserialize statement in the reconstitution process
53 * of the object. If you want to implement your own __wakeup() method in your Domain Object you have to call
54 * parent::__wakeup() first!
55 *
56 * @return void
57 * @author Jochen Rau <jochen.rau@typoplanet.de>
58 */
59 public function __wakeup() {
60 foreach ($GLOBALS['EXTMVC']['reconstituteObject']['properties'] as $propertyName => $value) {
61 $this->_reconstituteProperty($propertyName, $value);
62 }
63 $this->initializeObject();
64 $this->initializeCleanProperties();
65 }
66
67 /**
68 * A template function to initialize an object
69 *
70 * @return void
71 * @author Jochen Rau <jochen.rau@typoplanet.de>
72 */
73 protected function initializeObject() {
74 }
75
76 /**
77 * Getter for uid
78 *
79 * @return string
80 * @author Jochen Rau <jochen.rau@typoplanet.de>
81 */
82 public function getUid() {
83 return $this->uid;
84 }
85
86 /**
87 * Reconstitutes a property. This method should only be called at reconstitution time!
88 *
89 * @param string $propertyName
90 * @param string $value
91 * @return void
92 * @author Jochen Rau <jochen.rau@typoplanet.de>
93 */
94 public function _reconstituteProperty($propertyName, $value) {
95 if (property_exists($this, $propertyName)) {
96 $this->$propertyName = $value;
97 } else {
98 // throw new TX_EXTMVC_Persistence_Exception_UnknownProperty('The property "' . $propertyName . '" doesn\'t exist in this object.', 1233270476);
99 }
100 }
101
102 /**
103 * Register an object's clean state, e.g. after it has been reconstituted
104 * from the database
105 *
106 * @return void
107 * @author Jochen Rau <jochen.rau@typoplanet.de>
108 */
109 public function _memorizeCleanState() {
110 $this->initializeCleanProperties();
111 $cleanProperties = array();
112 foreach ($this->cleanProperties as $propertyName => $propertyValue) {
113 $cleanProperties[$propertyName] = $this->$propertyName;
114 }
115 $this->cleanProperties = $cleanProperties;
116 }
117
118 /**
119 * returns TRUE if the properties were modified after reconstitution
120 *
121 * @return boolean
122 * @author Jochen Rau <jochen.rau@typoplanet.de>
123 */
124 public function _isDirty() {
125 // if (!is_array($this->cleanProperties)) throw new TX_EXTMVC_Persistence_Exception_CleanStateNotMemorized('The clean state of the object "' . get_class($this) . '" has not been memorized before asking _isDirty().', 1233309106);
126 if ($this->uid !== NULL && $this->uid != $this->cleanProperties['uid']) throw new TX_EXTMVC_Persistence_Exception_TooDirty('The uid "' . $this->uid . '" has been modified, that is simply too much.', 1222871239);
127 foreach ($this->cleanProperties as $propertyName => $propertyValue) {
128 if ($this->$propertyName !== $propertyValue) return TRUE;
129 }
130 return FALSE;
131 }
132
133 /**
134 * Returns a hash map of property names and property values
135 *
136 * @return array The properties
137 * @author Jochen Rau <jochen.rau@typoplanet.de>
138 */
139 public function _getProperties() {
140 return get_object_vars($this);
141 }
142
143 /**
144 * Returns a hash map of dirty properties and $values
145 *
146 * @return boolean
147 * @author Jochen Rau <jochen.rau@typoplanet.de>
148 */
149 public function _getDirtyProperties() {
150 if (!is_array($this->cleanProperties)) throw new TX_EXTMVC_Persistence_Exception_CleanStateNotMemorized('The clean state of the object "' . get_class($this) . '" has not been memorized before asking _isDirty().', 1233309106);
151 if ($this->uid !== NULL && $this->uid != $this->cleanProperties['uid']) throw new TX_EXTMVC_Persistence_Exception_TooDirty('The uid "' . $this->uid . '" has been modified, that is simply too much.', 1222871239);
152 $dirtyProperties = array();
153 foreach ($this->cleanProperties as $propertyName => $propertyValue) {
154 if ($this->$propertyName !== $propertyValue) {
155 $dirtyProperties[$propertyName] = $this->$propertyName;
156 }
157 }
158 return $dirtyProperties;
159 }
160
161 private function initializeCleanProperties() {
162 $properties = get_object_vars($this);
163 $dataMapper = t3lib_div::makeInstance('TX_EXTMVC_Persistence_Mapper_ObjectRelationalMapper');
164 foreach ($properties as $propertyName => $propertyValue) {
165 if ($dataMapper->isPersistableProperty(get_class($this), $propertyName)) {
166 $this->cleanProperties[$propertyName] = NULL;
167 }
168 }
169 $this->cleanProperties['uid'] = NULL;
170 }
171
172 }
173 ?>