Splitted logic between entities and ValueObjects
[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 * @package TYPO3
29 * @subpackage extbase
30 * @version $ID:$
31 */
32 abstract class Tx_ExtBase_DomainObject_AbstractDomainObject implements Tx_ExtBase_DomainObject_DomainObjectInterface {
33
34 /**
35 * @var string The uid
36 */
37 protected $uid;
38
39 /**
40 * @var An array holding the clean property values. Set right after reconstitution of the object
41 */
42 private $_cleanProperties = NULL;
43
44 /**
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.
47 *
48 * @var array
49 */
50 public function __construct() {
51 $this->initializeObject();
52 }
53
54 /**
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!
58 *
59 * @return void
60 */
61 public function __wakeup() {
62 foreach ($GLOBALS['ExtBase']['reconstituteObject']['properties'] as $propertyName => $value) {
63 $this->_reconstituteProperty($propertyName, $value);
64 }
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 string
81 */
82 public function getUid() {
83 return $this->uid;
84 }
85
86 /**
87 * Reconstitutes a property. This method should only be called at reconstitution time!
88 *
89 * @param string $propertyName
90 * @param string $value
91 * @return void
92 * @internal
93 */
94 public function _reconstituteProperty($propertyName, $value) {
95 if (property_exists($this, $propertyName)) {
96 $this->$propertyName = $value;
97 }
98 }
99
100 /**
101 * Register an object's clean state, e.g. after it has been reconstituted
102 * from the database
103 *
104 * @return void
105 * @internal
106 */
107 public function _memorizeCleanState() {
108 $this->initializeCleanProperties();
109 $cleanProperties = array();
110 foreach ($this->_cleanProperties as $propertyName => $propertyValue) {
111 $cleanProperties[$propertyName] = $this->$propertyName;
112 }
113 $this->_cleanProperties = $cleanProperties;
114 }
115
116 /**
117 * Returns a hash map of property names and property values
118 *
119 * @return array The properties
120 * @internal
121 */
122 public function _getProperties() {
123 $properties = get_object_vars($this);
124 unset($properties['_cleanProperties']);
125 return $properties;
126 }
127
128 /**
129 * Returns a hash map of dirty properties and $values
130 *
131 * @return boolean
132 * @internal
133 */
134 public function _getDirtyProperties() {
135 if (!is_array($this->_cleanProperties)) throw new Tx_ExtBase_Persistence_Exception_CleanStateNotMemorized('The clean state of the object "' . get_class($this) . '" has not been memorized before asking _isDirty().', 1233309106);
136 if ($this->uid !== NULL && $this->uid != $this->_cleanProperties['uid']) throw new Tx_ExtBase_Persistence_Exception_TooDirty('The uid "' . $this->uid . '" has been modified, that is simply too much.', 1222871239);
137 $dirtyProperties = array();
138 foreach ($this->_cleanProperties as $propertyName => $propertyValue) {
139 if ($this->$propertyName !== $propertyValue) {
140 $dirtyProperties[$propertyName] = $this->$propertyName;
141 }
142 }
143 return $dirtyProperties;
144 }
145
146 /**
147 * Saves a copy of values of the persitable properties inside the object itself. This method is normally
148 * called right after it's reconstitution from a storage.
149 *
150 * @return void
151 * @author Jochen Rau <jochen.rau@typoplanet.de>
152 */
153 private function initializeCleanProperties() {
154 $properties = get_object_vars($this);
155 $dataMapper = t3lib_div::makeInstance('Tx_ExtBase_Persistence_Mapper_ObjectRelationalMapper'); // singleton
156 foreach ($properties as $propertyName => $propertyValue) {
157 if ($dataMapper->isPersistableProperty(get_class($this), $propertyName)) {
158 $this->_cleanProperties[$propertyName] = NULL;
159 }
160 }
161 $this->_cleanProperties['uid'] = NULL;
162 }
163
164 }
165 ?>