3b2b45b6ab715eb8814806dc1adb4e32c0be1955
[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 * All Model domain objects need to inherit from either AbstractEntity or AbstractValueObject, as this provides important framework information.
29 *
30 * @package Extbase
31 * @subpackage DomainObject
32 * @version $ID:$
33 */
34 abstract class Tx_Extbase_DomainObject_AbstractDomainObject implements Tx_Extbase_DomainObject_DomainObjectInterface, Tx_Extbase_Persistence_ObjectMonitoringInterface {
35
36 /**
37 * @var int The uid
38 */
39 protected $uid;
40
41 /**
42 * @var int The uid of the localized record. In TYPO3 v4.x the property "uid" holds the uid of the record in default language (the translationOrigin).
43 */
44 protected $_localizedUid;
45
46 /**
47 * @var int The uid of the language of the object. In TYPO3 v4.x this is the uid of the language record in the table sys_language.
48 */
49 protected $_languageUid;
50
51 /**
52 * TRUE if the object is a clone
53 * @var boolean
54 */
55 private $_isClone = FALSE;
56
57 /**
58 * The generic constructor. If you want to implement your own __constructor() method in your Domain Object you have to call
59 * $this->initializeObject() in the first line of your constructor.
60 *
61 * @var array
62 */
63 public function __construct() {
64 $this->initializeObject();
65 }
66
67 /**
68 * This is the magic __wakeup() method. It's invoked by the unserialize statement in the reconstitution process
69 * of the object. If you want to implement your own __wakeup() method in your Domain Object you have to call
70 * parent::__wakeup() first!
71 *
72 * @return void
73 */
74 public function __wakeup() {
75 $this->initializeObject();
76 }
77
78 /**
79 * A template method to initialize an object. This can be used to manipulate the object after
80 * reconstitution and before the clean state of it's properties is stored.
81 *
82 * @return void
83 */
84 protected function initializeObject() {
85 }
86
87 /**
88 * Getter for uid.
89 *
90 * @return int the uid or NULL if none set yet.
91 */
92 final public function getUid() {
93 if ($this->uid !== NULL) {
94 return (int)$this->uid;
95 } else {
96 return NULL;
97 }
98 }
99
100 /**
101 * Reconstitutes a property. Only for internal use.
102 *
103 * @param string $propertyName
104 * @param string $value
105 * @return void
106 */
107 public function _setProperty($propertyName, $propertyValue) {
108 if ($this->_hasProperty($propertyName)) {
109 $this->$propertyName = $propertyValue;
110 return TRUE;
111 }
112 return FALSE;
113 }
114
115 /**
116 * Returns the property value of the given property name. Only for internal use.
117 *
118 * @return mixed The propertyValue
119 */
120 public function _getProperty($propertyName) {
121 return $this->$propertyName;
122 }
123
124 /**
125 * Returns a hash map of property names and property values. Only for internal use.
126 *
127 * @return array The properties
128 */
129 public function _getProperties() {
130 $properties = get_object_vars($this);
131 foreach ($properties as $propertyName => $propertyValue) {
132 if ($propertyName{0} === '_') {
133 unset($properties[$propertyName]);
134 }
135 }
136 return $properties;
137 }
138
139 /**
140 * Returns the property value of the given property name. Only for internal use.
141 *
142 * @return boolean TRUE bool true if the property exists, FALSE if it doesn't exist or
143 * NULL in case of an error.
144 */
145 public function _hasProperty($propertyName) {
146 return property_exists($this, $propertyName);
147 }
148
149 /**
150 * Returns TRUE if the object is new (the uid was not set, yet). Only for internal use
151 *
152 * @return boolean
153 */
154 public function _isNew() {
155 return $this->uid === NULL;
156 }
157
158 /**
159 * Register an object's clean state, e.g. after it has been reconstituted
160 * from the database
161 *
162 * @return void
163 */
164 public function _memorizeCleanState() {
165 }
166
167 /**
168 * Returns TRUE if the properties were modified after reconstitution. However, value objects can be never updated.
169 *
170 * @return boolean
171 */
172 public function _isDirty($propertyName = NULL) {
173 return FALSE;
174 }
175
176 /**
177 * Returns TRUE if the object has been clonesd, cloned, FALSE otherwise.
178 *
179 * @return boolean TRUE if the object has been cloned
180 */
181 public function _isClone() {
182 return $this->_isClone;
183 }
184
185 /**
186 * Setter whether this Domain Object is a clone of another one.
187 * NEVER SET THIS PROPERTY DIRECTLY. We currently need it to make the
188 * _isDirty check inside AbstractEntity work, but it is just a work-
189 * around right now.
190 *
191 * @param boolean $clone
192 */
193 public function _setClone($clone) {
194 $this->_isClone = (boolean)$clone;
195 }
196
197 /**
198 * Clone method. Sets the _isClone property.
199 *
200 * @return void
201 */
202 public function __clone() {
203 $this->_isClone = TRUE;
204 }
205
206 /**
207 * Returns the class name and the uid of the object as string
208 *
209 * @return string
210 */
211 public function __toString() {
212 return get_class($this) . ':' . (string)$this->uid;
213 }
214
215 }
216 ?>