ExtBase:
[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 extbase
30 * @version $ID:$
31 */
32 abstract class Tx_ExtBase_DomainObject_AbstractDomainObject implements Tx_ExtBase_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['ExtBase']['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 }
99 }
100
101 /**
102 * Register an object's clean state, e.g. after it has been reconstituted
103 * from the database
104 *
105 * @return void
106 * @internal
107 */
108 public function _memorizeCleanState() {
109 $this->initializeCleanProperties();
110 $cleanProperties = array();
111 foreach ($this->_cleanProperties as $propertyName => $propertyValue) {
112 $cleanProperties[$propertyName] = $this->$propertyName;
113 }
114 $this->_cleanProperties = $cleanProperties;
115 }
116
117 /**
118 * Returns TRUE if the properties were modified after reconstitution
119 *
120 * @return boolean
121 * @internal
122 */
123 public function _isDirty() {
124 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);
125 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);
126 foreach ($this->_cleanProperties as $propertyName => $propertyValue) {
127 if ($this->$propertyName !== $propertyValue) return TRUE;
128 }
129 return FALSE;
130 }
131
132 /**
133 * Returns a hash map of property names and property values
134 *
135 * @return array The properties
136 * @internal
137 */
138 public function _getProperties() {
139 $properties = get_object_vars($this);
140 unset($properties['_cleanProperties']);
141 return $properties;
142 }
143
144 /**
145 * Returns a hash map of dirty properties and $values
146 *
147 * @return boolean
148 * @internal
149 */
150 public function _getDirtyProperties() {
151 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);
152 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);
153 $dirtyProperties = array();
154 foreach ($this->_cleanProperties as $propertyName => $propertyValue) {
155 if ($this->$propertyName !== $propertyValue) {
156 $dirtyProperties[$propertyName] = $this->$propertyName;
157 }
158 }
159 return $dirtyProperties;
160 }
161
162 /**
163 * Saves a copy of values of the persitable properties inside the object itself. This method is normally
164 * called right after it's reconstitution from a storage.
165 *
166 * @return void
167 * @author Jochen Rau <jochen.rau@typoplanet.de>
168 */
169 private function initializeCleanProperties() {
170 $properties = get_object_vars($this);
171 $dataMapper = t3lib_div::makeInstance('Tx_ExtBase_Persistence_Mapper_ObjectRelationalMapper');
172 foreach ($properties as $propertyName => $propertyValue) {
173 if ($dataMapper->isPersistableProperty(get_class($this), $propertyName)) {
174 $this->_cleanProperties[$propertyName] = NULL;
175 }
176 }
177 $this->_cleanProperties['uid'] = NULL;
178 }
179
180 }
181 ?>