4b7c18bc05c40f461e4683a4544cf184d92f5c6a
[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 require_once(t3lib_extMgm::extPath('extmvc') . 'Classes/Persistence/Mapper/TX_EXTMVC_Persistence_Mapper_ObjectRelationalMapper.php');
24
25 /**
26 * A generic Domain Object
27 *
28 * @version $Id:$
29 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
30 */
31 abstract class TX_EXTMVC_DomainObject_AbstractDomainObject {
32
33 /**
34 * @var string The uid
35 */
36 protected $uid;
37
38 /**
39 * The generic constructor. If you want to implement your own __constructor() method in your Domain Object you have to call
40 * $this->initializeObject() in the first line of your constructor.
41 *
42 * @var array
43 */
44 private $cleanProperties = NULL;
45
46 public function __construct() {
47 $this->initializeObject();
48 }
49
50 /**
51 * This is the magic __wakeup() method. It's invoked by the unserialize statement in the reconstitution process
52 * of the object. If you want to implement your own __wakeup() method in your Domain Object you have to call
53 * parent::__wakeup() first!
54 *
55 * @return void
56 */
57 public function __wakeup() {
58 foreach ($GLOBALS['EXTMVC']['reconstituteObject']['properties'] as $propertyName => $value) {
59 $this->_reconstituteProperty($propertyName, $value);
60 }
61 $this->initializeObject();
62 $this->initializeCleanProperties();
63 }
64
65 /**
66 * A template function to initialize an object
67 *
68 * @return void
69 */
70 // SK: Add a short comment how this method can be used
71 protected function initializeObject() {
72 }
73
74 /**
75 * Getter for uid
76 *
77 * @return string
78 */
79 public function getUid() {
80 return $this->uid;
81 }
82
83 /**
84 * Reconstitutes a property. This method should only be called at reconstitution time!
85 *
86 * @param string $propertyName
87 * @param string $value
88 * @return void
89 */
90 // SK: add @internal annotation, and a big fat warning that this should not be used!!
91 public function _reconstituteProperty($propertyName, $value) {
92 if (property_exists($this, $propertyName)) {
93 $this->$propertyName = $value;
94 } else {
95 // TODO throw new TX_EXTMVC_Persistence_Exception_UnknownProperty('The property "' . $propertyName . '" doesn\'t exist in this object.', 1233270476);
96 }
97 }
98
99 /**
100 * Register an object's clean state, e.g. after it has been reconstituted
101 * from the database
102 *
103 * @return void
104 */
105 // SK: add @internal annotation, and a big fat warning that this should not be used!!
106 public function _memorizeCleanState() {
107 $this->initializeCleanProperties();
108 $cleanProperties = array();
109 foreach ($this->cleanProperties as $propertyName => $propertyValue) {
110 $cleanProperties[$propertyName] = $this->$propertyName;
111 }
112 $this->cleanProperties = $cleanProperties;
113 }
114
115 /**
116 * returns TRUE if the properties were modified after reconstitution
117 *
118 * @return boolean
119 */
120 // SK: add @internal annotation, and a big fat warning that this should not be used!!
121 public function _isDirty() {
122 // 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);
123 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);
124 foreach ($this->cleanProperties as $propertyName => $propertyValue) {
125 if ($this->$propertyName !== $propertyValue) return TRUE;
126 }
127 return FALSE;
128 }
129
130 /**
131 * Returns a hash map of property names and property values
132 *
133 * @return array The properties
134 */
135 // SK: add @internal annotation, and a big fat warning that this should not be used!!
136 public function _getProperties() {
137 return get_object_vars($this);
138 }
139
140 /**
141 * Returns a hash map of dirty properties and $values
142 *
143 * @return boolean
144 */
145 // SK: add @internal annotation, and a big fat warning that this should not be used!!
146 public function _getDirtyProperties() {
147 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);
148 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);
149 $dirtyProperties = array();
150 foreach ($this->cleanProperties as $propertyName => $propertyValue) {
151 if ($this->$propertyName !== $propertyValue) {
152 $dirtyProperties[$propertyName] = $this->$propertyName;
153 }
154 }
155 return $dirtyProperties;
156 }
157
158 // SK: PHPDoc
159 private function initializeCleanProperties() {
160 $properties = get_object_vars($this);
161 $dataMapper = t3lib_div::makeInstance('TX_EXTMVC_Persistence_Mapper_ObjectRelationalMapper');
162 foreach ($properties as $propertyName => $propertyValue) {
163 if ($dataMapper->isPersistableProperty(get_class($this), $propertyName)) {
164 $this->cleanProperties[$propertyName] = NULL;
165 }
166 }
167 $this->cleanProperties['uid'] = NULL;
168 }
169
170 }
171 ?>