2 /***************************************************************
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
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.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
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.
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
26 * A generic Domain Object
32 abstract class TX_EXTMVC_DomainObject_AbstractDomainObject
implements TX_EXTMVC_DomainObject_DomainObjectInterface
{
40 * @var An array holding the clean property values. Set right after reconstitution of the object
42 private $_cleanProperties = NULL;
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.
50 public function __construct() {
51 $this->initializeObject();
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!
61 public function __wakeup() {
62 foreach ($GLOBALS['EXTMVC']['reconstituteObject']['properties'] as $propertyName => $value) {
63 $this->_reconstituteProperty($propertyName, $value);
65 $this->initializeObject();
66 $this->initializeCleanProperties();
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.
75 protected function initializeObject() {
83 public function getUid() {
88 * Reconstitutes a property. This method should only be called at reconstitution time!
90 * @param string $propertyName
91 * @param string $value
95 public function _reconstituteProperty($propertyName, $value) {
96 if (property_exists($this, $propertyName)) {
97 $this->$propertyName = $value;
99 // TODO Should we throw new TX_EXTMVC_Persistence_Exception_UnknownProperty('The property "' . $propertyName . '" doesn\'t exist in this object.', 1233270476);
104 * Register an object's clean state, e.g. after it has been reconstituted
110 public function _memorizeCleanState() {
111 $this->initializeCleanProperties();
112 $cleanProperties = array();
113 foreach ($this->_cleanProperties
as $propertyName => $propertyValue) {
114 $cleanProperties[$propertyName] = $this->$propertyName;
116 $this->_cleanProperties
= $cleanProperties;
120 * Returns TRUE if the properties were modified after reconstitution
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;
135 * Returns a hash map of property names and property values
137 * @return array The properties
140 public function _getProperties() {
141 $properties = get_object_vars($this);
142 unset($properties['_cleanProperties']);
147 * Returns a hash map of dirty properties and $values
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;
161 return $dirtyProperties;
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.
169 * @author Jochen Rau <jochen.rau@typoplanet.de>
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;
179 $this->_cleanProperties
['uid'] = NULL;