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