* added some fixtures for the unit tests
[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_TcaMapper.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 * An array of properties filled with database values of columns configured in $TCA.
41 *
42 * @var array
43 */
44 private $cleanProperties = NULL;
45
46 /**
47 * This is the magic wakeup() method. It's invoked by the unserialize statement in the reconstitution process
48 * of the object. If you want to implement your own __wakeup() method in your Domain Object you have to call
49 * parent::__wakeup() first!
50 *
51 * @return void
52 * @author Jochen Rau <jochen.rau@typoplanet.de>
53 */
54 public function __wakeup() {
55 foreach ($GLOBALS['EXTMVC']['reconstituteObject']['properties'] as $propertyName => $value) {
56 $this->_reconstituteProperty($propertyName, $value);
57 }
58 $this->initCleanProperties();
59 }
60
61 /**
62 * Getter for uid
63 *
64 * @return string
65 * @author Jochen Rau <jochen.rau@typoplanet.de>
66 */
67 public function getUid() {
68 return $this->uid;
69 }
70
71 /**
72 * Reconstitutes a property. This method should only be called at reconstitution time!
73 *
74 * @param string $propertyName
75 * @param string $value
76 * @return void
77 * @author Jochen Rau <jochen.rau@typoplanet.de>
78 */
79 public function _reconstituteProperty($propertyName, $value) {
80 if (property_exists($this, $propertyName)) {
81 $this->$propertyName = $value;
82 } else {
83 // throw new TX_EXTMVC_Persistence_Exception_UnknownProperty('The property "' . $propertyName . '" doesn\'t exist in this object.', 1233270476);
84 }
85 }
86
87 /**
88 * Register an object's clean state, e.g. after it has been reconstituted
89 * from the database
90 *
91 * @return void
92 * @author Jochen Rau <jochen.rau@typoplanet.de>
93 */
94 public function _memorizeCleanState() {
95 $this->initCleanProperties();
96 $cleanProperties = array();
97 foreach ($this->cleanProperties as $propertyName => $propertyValue) {
98 $cleanProperties[$propertyName] = $this->$propertyName;
99 }
100 $this->cleanProperties = $cleanProperties;
101 }
102
103 /**
104 * returns TRUE if the properties were modified after reconstitution
105 *
106 * @return boolean
107 * @author Jochen Rau <jochen.rau@typoplanet.de>
108 */
109 public function _isDirty() {
110 // 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);
111 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);
112 foreach ($this->cleanProperties as $propertyName => $propertyValue) {
113 if ($this->$propertyName !== $propertyValue) return TRUE;
114 }
115 return FALSE;
116 }
117
118 /**
119 * Returns a hash map of persitable properties and $values
120 *
121 * @return boolean
122 * @author Jochen Rau <jochen.rau@typoplanet.de>
123 */
124 public function _getProperties() {
125 $dataMapper = t3lib_div::makeInstance('TX_EXTMVC_Persistence_Mapper_TcaMapper');
126 $properties = get_object_vars($this);
127 $persistableProperties = array();
128 foreach ($properties as $propertyName => $propertyValue) {
129 if ($dataMapper->isPersistableProperty(get_class($this), $propertyName)) {
130 $persistableProperties[$propertyName] = $propertyValue;
131 }
132 }
133 return $persistableProperties;
134 }
135
136 /**
137 * Returns a hash map of dirty properties and $values
138 *
139 * @return boolean
140 * @author Jochen Rau <jochen.rau@typoplanet.de>
141 */
142 public function _getDirtyProperties() {
143 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);
144 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);
145 $dirtyProperties = array();
146 foreach ($this->cleanProperties as $propertyName => $propertyValue) {
147 if ($this->$propertyName !== $propertyValue) {
148 $dirtyProperties[$propertyName] = $this->$propertyName;
149 }
150 }
151 return $dirtyProperties;
152 }
153
154 private function initCleanProperties() {
155 $properties = get_object_vars($this);
156 $dataMapper = t3lib_div::makeInstance('TX_EXTMVC_Persistence_Mapper_TcaMapper');
157 foreach ($properties as $propertyName => $propertyValue) {
158 if ($dataMapper->isPersistableProperty(get_class($this), $propertyName)) {
159 $this->cleanProperties[$propertyName] = NULL;
160 }
161 }
162 $this->cleanProperties['uid'] = NULL;
163 }
164
165 }
166 ?>