EXTMVC:
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / DomainObject / AbstractDomainObject.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 * A generic Domain Object
27 *
28 * @package TYPO3
29 * @subpackage extmvc
30 * @version $ID:$
31 */
32 abstract class TX_EXTMVC_DomainObject_AbstractDomainObject implements TX_EXTMVC_DomainObject_DomainObjectInterface {
33
34 /**
35 * @var string The uid
36 */
37 protected $uid;
38
39 /**
40 * @var An array holding the clean property values. Set right after reconstitution of the object
41 */
42 private $_cleanProperties = NULL;
43
44 /**
45 * The generic constructor. If you want to implement your own __constructor() method in your Domain Object you have to call
46 * $this->initializeObject() in the first line of your constructor.
47 *
48 * @var array
49 */
50 public function __construct() {
51 $this->initializeObject();
52 }
53
54 /**
55 * This is the magic __wakeup() method. It's invoked by the unserialize statement in the reconstitution process
56 * of the object. If you want to implement your own __wakeup() method in your Domain Object you have to call
57 * parent::__wakeup() first!
58 *
59 * @return void
60 */
61 public function __wakeup() {
62 foreach ($GLOBALS['EXTMVC']['reconstituteObject']['properties'] as $propertyName => $value) {
63 $this->_reconstituteProperty($propertyName, $value);
64 }
65 $this->initializeObject();
66 $this->initializeCleanProperties();
67 }
68
69 /**
70 * A template method to initialize an object. This can be used to manipulate the object after
71 * reconstitution and before the clean state of it's properties is stored.
72 *
73 * @return void
74 */
75 protected function initializeObject() {
76 }
77
78 /**
79 * Getter for uid
80 *
81 * @return string
82 */
83 public function getUid() {
84 return $this->uid;
85 }
86
87 /**
88 * Reconstitutes a property. This method should only be called at reconstitution time!
89 *
90 * @param string $propertyName
91 * @param string $value
92 * @return void
93 * @internal
94 */
95 public function _reconstituteProperty($propertyName, $value) {
96 if (property_exists($this, $propertyName)) {
97 $this->$propertyName = $value;
98 } else {
99 // TODO Should we throw new TX_EXTMVC_Persistence_Exception_UnknownProperty('The property "' . $propertyName . '" doesn\'t exist in this object.', 1233270476);
100 }
101 }
102
103 /**
104 * Register an object's clean state, e.g. after it has been reconstituted
105 * from the database
106 *
107 * @return void
108 * @internal
109 */
110 public function _memorizeCleanState() {
111 $this->initializeCleanProperties();
112 $cleanProperties = array();
113 foreach ($this->_cleanProperties as $propertyName => $propertyValue) {
114 $cleanProperties[$propertyName] = $this->$propertyName;
115 }
116 $this->_cleanProperties = $cleanProperties;
117 }
118
119 /**
120 * Returns TRUE if the properties were modified after reconstitution
121 *
122 * @return boolean
123 * @internal
124 */
125 public function _isDirty() {
126 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);
127 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);
128 foreach ($this->_cleanProperties as $propertyName => $propertyValue) {
129 if ($this->$propertyName !== $propertyValue) return TRUE;
130 }
131 return FALSE;
132 }
133
134 /**
135 * Returns a hash map of property names and property values
136 *
137 * @return array The properties
138 * @internal
139 */
140 public function _getProperties() {
141 $properties = get_object_vars($this);
142 unset($properties['_cleanProperties']);
143 return $properties;
144 }
145
146 /**
147 * Returns a hash map of dirty properties and $values
148 *
149 * @return boolean
150 * @internal
151 */
152 public function _getDirtyProperties() {
153 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);
154 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);
155 $dirtyProperties = array();
156 foreach ($this->_cleanProperties as $propertyName => $propertyValue) {
157 if ($this->$propertyName !== $propertyValue) {
158 $dirtyProperties[$propertyName] = $this->$propertyName;
159 }
160 }
161 return $dirtyProperties;
162 }
163
164 /**
165 * Saves a copy of values of the persitable properties inside the object itself. This method is normally
166 * called right after it's reconstitution from a storage.
167 *
168 * @return void
169 * @author Jochen Rau <jochen.rau@typoplanet.de>
170 */
171 private function initializeCleanProperties() {
172 $properties = get_object_vars($this);
173 $dataMapper = t3lib_div::makeInstance('TX_EXTMVC_Persistence_Mapper_ObjectRelationalMapper');
174 foreach ($properties as $propertyName => $propertyValue) {
175 if ($dataMapper->isPersistableProperty(get_class($this), $propertyName)) {
176 $this->_cleanProperties[$propertyName] = NULL;
177 }
178 }
179 $this->_cleanProperties['uid'] = NULL;
180 }
181
182 }
183 ?>