PersistentObject.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\lib\persistence;
12 
15 
16 /**
17  * PersistentObject defines the interface of all persistent objects.
18  * It mainly requires an unique identifier for each instance (ObjectId),
19  * tracking of the persistent state, methods for setting and getting values
20  * as well as callback methods for lifecycle events.
21  *
22  * @author ingo herwig <ingo@wemove.com>
23  */
24 interface PersistentObject {
25 
26  const STATE_CLEAN = 0;
27  const STATE_DIRTY = 1;
28  const STATE_NEW = 2;
29  const STATE_DELETED = 3;
30 
31  /**
32  * Initialize the object with a set of data. This method does not validate, does not
33  * change the object's state and does not call any listeners. Any existing data will
34  * be overwritten. The data will also be used as base line for tracking changes.
35  * @param $data An associative array with the data to set.
36  */
37  public function initialize(array $data);
38 
39  /**
40  * Get the type of the object.
41  * @return The objects type.
42  */
43  public function getType();
44 
45  /**
46  * Get the PersistenceMapper of the object.
47  * @return PersistenceMapper
48  */
49  public function getMapper();
50 
51  /**
52  * Get the object id of the PersistentObject.
53  * @return ObjectId
54  */
55  public function getOID();
56 
57  /**
58  * Set the object id of the PersistentObject.
59  * @param $oid The PersistentObject's oid.
60  */
61  public function setOID(ObjectId $oid);
62 
63  /**
64  * Get the object's state:
65  * @return One of the STATE constant values:
66  */
67  public function getState();
68 
69  /**
70  * Set the state of the object to one of the STATE constants.
71  */
72  public function setState($state);
73 
74  /**
75  * Delete the object
76  */
77  public function delete();
78 
79  /**
80  * Get a copy of the object (ChangeListeners and Lock are not copied)
81  * @return PersistentObject
82  */
83  public function __clone();
84 
85  /**
86  * Copy all non-empty values to a given instance (ChangeListeners are triggered)
87  * @param $object PersistentObject instance to copy the values to.
88  * @param $copyPkValues Boolean whether primary key values should be copied
89  */
90  public function copyValues(PersistentObject $object, $copyPkValues=true);
91 
92  /**
93  * Copy all values, that don't exist yet from a given instance
94  * (ChangeListeners are not triggered)
95  * @param $object PersistentObject instance to copy the values from.
96  */
97  public function mergeValues(PersistentObject $object);
98 
99  /**
100  * Clear all values. Set each value to null except
101  * for the primary key values
102  */
103  public function clearValues();
104 
105  /**
106  * <!--
107  * Persistence hook methods.
108  * -->
109  */
110 
111  /**
112  * This method is called once after creation of this object. At this time it
113  * is not known in the store.
114  */
115  public function afterCreate();
116 
117  /**
118  * This method is called once before inserting the newly created object into the store.
119  */
120  public function beforeInsert();
121 
122  /**
123  * This method is called once after inserting the newly created object into the store.
124  */
125  public function afterInsert();
126 
127  /**
128  * This method is called always after loading the object from the store.
129  */
130  public function afterLoad();
131 
132  /**
133  * This method is called always before updating the modified object in the store.
134  */
135  public function beforeUpdate();
136 
137  /**
138  * This method is called always after updating the modified object in the store.
139  */
140  public function afterUpdate();
141 
142  /**
143  * This method is called once before deleting the object from the store.
144  */
145  public function beforeDelete();
146 
147  /**
148  * This method is called once after deleting the object from the store.
149  */
150  public function afterDelete();
151 
152  /**
153  * <!--
154  * Values and Properties
155  * -->
156  */
157 
158  /**
159  * Get the value of a named item.
160  * @param $name The name of the item to query.
161  * @return The value of the item / null if it doesn't exits.
162  */
163  public function getValue($name);
164 
165  /**
166  * Set the value of a named item if it exists.
167  * @param $name The name of the item to set.
168  * @param $value The value of the item.
169  * @param $forceSet Boolean whether to set the value even if it is already set
170  * and to bypass validation (used to notify listeners) (default: _false_)
171  * @param $trackChange Boolean whether to track the change (change state, notify listeners) or not (default: _true_)
172  * Only set this false, if you know, what you are doing
173  * @return Boolean whether the operation succeeds or not
174  */
175  public function setValue($name, $value, $forceSet=false, $trackChange=true);
176 
177  /**
178  * Check if the node has a given item.
179  * @param $name The name of the item to query.
180  * @return Boolean whether the item exists or not.
181  */
182  public function hasValue($name);
183 
184  /**
185  * Remove a named item.
186  * @param $name The name of the item to remove.
187  */
188  public function removeValue($name);
189 
190  /**
191  * Get the names of all items.
192  * @param $excludeTransient Boolean whether to exclude transient values (default: _false_)
193  * @return An array of item names.
194  */
195  public function getValueNames($excludeTransient=false);
196 
197  /**
198  * Validate all values by calling PersistentObject::validateValue()
199  * Throws a ValidationException in case of invalid data.
200  * @param $message The Message instance used to provide translations.
201  */
202  public function validateValues(Message $message);
203 
204  /**
205  * Check if data may be set. The method is also called, when setting a value.
206  * Controller may call this method before setting data and saving the object.
207  * Throws a ValidationException in case of invalid data.
208  * @param $name The name of the item to set.
209  * @param $value The value of the item.
210  * @param $message The Message instance used to provide translations.
211  * The default implementation calls PersistentObject::validateValueAgainstValidateType().
212  */
213  public function validateValue($name, $value, Message $message);
214 
215  /**
216  * Get the list of changed attributes since creation, loading.
217  * @return Array of value names
218  */
219  public function getChangedValues();
220 
221  /**
222  * Get the original data provided to the initialize method.
223  * @return Associative array with value names as keys and values as values
224  */
225  public function getOriginalValues();
226 
227  /**
228  * Get the list of objects that must exist in the store, before
229  * this object may be persisted. Implementing classes may use this method to
230  * manage dependencies.
231  * @return Array of PersistentObject instances
232  */
233  public function getIndispensableObjects();
234 
235  /**
236  * Get the value of a named property in the object.
237  * @param $name The name of the property to query.
238  * @return The value of the property / null if it doesn't exits.
239  */
240  public function getProperty($name);
241 
242  /**
243  * Set the value of a named property in the object.
244  * @param $name The name of the property to set.
245  * @param $value The value of the property to set.
246  */
247  public function setProperty($name, $value);
248 
249  /**
250  * Get the names of all properties in the object. Properties are
251  * either defined by using the PersistentObject::setProperty() method
252  * or by the PersistentMapper.
253  * @return An array consisting of the names.
254  */
255  public function getPropertyNames();
256 
257  /**
258  * Get the value of one property of a named item.
259  * @param $name The name of the item to get its properties.
260  * @param $property The name of the property to get.
261  * @return The value property/null if not found.
262  */
263  public function getValueProperty($name, $property);
264 
265  /**
266  * Set the value of one property of a named item.
267  * @param $name The name of the item to set its properties.
268  * @param $property The name of the property to set.
269  * @param $value The value to set on the property.
270  */
271  public function setValueProperty($name, $property, $value);
272 
273  /**
274  * Get the names of all properties of a value in the object.
275  * @return An array consisting of the names.
276  */
277  public function getValuePropertyNames($name);
278 
279  /**
280  * <!--
281  * Output
282  * -->
283  */
284 
285  /**
286  * Get the value of the object used for display.
287  * @return The value.
288  */
289  public function getDisplayValue();
290 
291  /**
292  * Get a string representation of the values of the PersistentObject.
293  * @return String
294  */
295  public function dump();
296 }
297 ?>
setProperty($name, $value)
Set the value of a named property in the object.
getType()
Get the type of the object.
setState($state)
Set the state of the object to one of the STATE constants.
getOID()
Get the object id of the PersistentObject.
getDisplayValue()
Get the value of the object used for display.
beforeUpdate()
This method is called always before updating the modified object in the store.
getOriginalValues()
Get the original data provided to the initialize method.
afterDelete()
This method is called once after deleting the object from the store.
validateValue($name, $value, Message $message)
Check if data may be set.
getMapper()
Get the PersistenceMapper of the object.
getValueNames($excludeTransient=false)
Get the names of all items.
getProperty($name)
Get the value of a named property in the object.
afterUpdate()
This method is called always after updating the modified object in the store.
Persistence layer related interfaces and classes.
Definition: namespaces.php:42
ObjectId is the unique identifier of an object.
Definition: ObjectId.php:27
getPropertyNames()
Get the names of all properties in the object.
validateValues(Message $message)
Validate all values by calling PersistentObject::validateValue() Throws a ValidationException in case...
Message is used to get localized messages to be used in the user interface.
Definition: Message.php:23
__clone()
Get a copy of the object (ChangeListeners and Lock are not copied)
afterCreate()
This method is called once after creation of this object.
getValuePropertyNames($name)
Get the names of all properties of a value in the object.
getValue($name)
Get the value of a named item.
setOID(ObjectId $oid)
Set the object id of the PersistentObject.
initialize(array $data)
Initialize the object with a set of data.
hasValue($name)
Check if the node has a given item.
dump()
Get a string representation of the values of the PersistentObject.
removeValue($name)
Remove a named item.
setValue($name, $value, $forceSet=false, $trackChange=true)
Set the value of a named item if it exists.
copyValues(PersistentObject $object, $copyPkValues=true)
Copy all non-empty values to a given instance (ChangeListeners are triggered)
getIndispensableObjects()
Get the list of objects that must exist in the store, before this object may be persisted.
afterInsert()
This method is called once after inserting the newly created object into the store.
afterLoad()
This method is called always after loading the object from the store.
beforeDelete()
This method is called once before deleting the object from the store.
getValueProperty($name, $property)
Get the value of one property of a named item.
mergeValues(PersistentObject $object)
Copy all values, that don't exist yet from a given instance (ChangeListeners are not triggered) ...
beforeInsert()
This method is called once before inserting the newly created object into the store.
getState()
Get the object's state:
getChangedValues()
Get the list of changed attributes since creation, loading.
setValueProperty($name, $property, $value)
Set the value of one property of a named item.
PersistentObject defines the interface of all persistent objects.