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