PersistentObjectProxy.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 
19 
20 /**
21  * PersistentObjectProxy is proxy for an PersistentObject instance.
22  *
23  * @author ingo herwig <ingo@wemove.com>
24  */
26 
27  protected $_oid = null; // object identifier
28  protected $_realSubject = null; // the PersistentObject instance
29 
30  /**
31  * Constructor.
32  * @param $oid The object id of the PersistentObject instance.
33  */
34  public function __construct(ObjectId $oid) {
35  $this->_oid = $oid;
36  }
37 
38  /**
39  * Create a PersistenceProxy instance from a PersistentObject. This is useful
40  * if you want to prevent automatic loading of the subject if it is already loaded.
41  * Returns the argument, if already an PersistentObjectProxy instance.
42  * @param $object The PersistentObject or PersistentObjectProxy
43  * @return PersistentObjectProxy
44  */
45  public static function fromObject($object) {
46  if ($object instanceof PersistentObjectProxy) {
47  return $object;
48  }
49  else if ($object instanceof PersistentObject) {
50  $proxy = new PersistentObjectProxy($object->getOID());
51  $proxy->_realSubject = $object;
52  return $proxy;
53  }
54  else {
55  throw new IllegalArgumentException("Cannot create proxy from unknown object");
56  }
57  }
58 
59  /**
60  * Get the PersistentObject instance.
61  * @return PersistentObject
62  */
63  public function getRealSubject() {
64  if ($this->_realSubject == null) {
65  $this->resolve();
66  }
67  return $this->_realSubject;
68  }
69 
70  /**
71  * Delegate method call to the instance.
72  */
73  public function __call($name, array $arguments) {
74  if ($this->_realSubject == null) {
75  $this->resolve();
76  }
77  return call_user_func_array(array($this->_realSubject, $name), $arguments);
78  }
79 
80  /**
81  * Load the PersistentObject instance. Use this method if the subject should be loaded
82  * with a depth greater than BuildDepth::SINGLE
83  * @param $buildDepth One of the BUILDDEPTH constants or a number describing the number of generations to build
84  * (default: _BuildDepth::SINGLE_)
85  */
86  public function resolve($buildDepth=BuildDepth::SINGLE) {
87  $this->_realSubject = ObjectFactory::getInstance('persistenceFacade')->load($this->_oid, $buildDepth);
88  if ($this->_realSubject == null) {
89  throw new PersistenceException("Could not resolve oid: ".$this->_oid);
90  }
91  }
92 
93  /**
94  * @see PersistentObject::initialize()
95  */
96  public function initialize(array $data) {
97  return $this->__call(__FUNCTION__, $data);
98  }
99 
100  /**
101  * Get the type of the PersistentObject.
102  * @return String
103  */
104  public function getType() {
105  return $this->_oid->getType();
106  }
107 
108  /**
109  * @see PersistentObject::getMapper()
110  */
111  public function getMapper() {
112  return $this->__call(__FUNCTION__, array());
113  }
114 
115  /**
116  * Get the object id of the PersistentObject.
117  * @return ObjectId
118  */
119  public function getOID() {
120  return $this->_oid;
121  }
122 
123  /**
124  * @see PersistentObject::setOID()
125  */
126  public function setOID(ObjectId $oid) {
127  return $this->__call(__FUNCTION__, array($oid));
128  }
129 
130  /**
131  * @see PersistentObject::getState()
132  */
133  public function getState() {
134  return $this->__call(__FUNCTION__, array());
135  }
136 
137  /**
138  * @see PersistentObject::setState()
139  */
140  public function setState($state) {
141  return $this->__call(__FUNCTION__, array($state));
142  }
143 
144  /**
145  * @see PersistentObject::delete()
146  */
147  public function delete() {
148  return $this->__call(__FUNCTION__, array());
149  }
150 
151  /**
152  * @see PersistentObject::__clone()
153  */
154  public function __clone() {
155  return $this->__call(__FUNCTION__, array());
156  }
157 
158  /**
159  * @see PersistentObject::copyValues()
160  */
161  public function copyValues(PersistentObject $object, $copyPkValues=true) {
162  return $this->__call(__FUNCTION__, array($object, $copyPkValues));
163  }
164 
165  /**
166  * @see PersistentObject::mergeValues()
167  */
168  public function mergeValues(PersistentObject $object) {
169  return $this->__call(__FUNCTION__, array($object));
170  }
171 
172  /**
173  * @see PersistentObject::clearValues()
174  */
175  public function clearValues() {
176  return $this->__call(__FUNCTION__, array());
177  }
178 
179  /**
180  * @see PersistentObject::afterCreate()
181  */
182  public function afterCreate() {
183  return $this->__call(__FUNCTION__, array());
184  }
185 
186  /**
187  * @see PersistentObject::beforeInsert()
188  */
189  public function beforeInsert() {
190  return $this->__call(__FUNCTION__, array());
191  }
192 
193  /**
194  * @see PersistentObject::afterInsert()
195  */
196  public function afterInsert() {
197  return $this->__call(__FUNCTION__, array());
198  }
199 
200  /**
201  * @see PersistentObject::afterLoad()
202  */
203  public function afterLoad() {
204  return $this->__call(__FUNCTION__, array());
205  }
206 
207  /**
208  * @see PersistentObject::beforeUpdate()
209  */
210  public function beforeUpdate() {
211  return $this->__call(__FUNCTION__, array());
212  }
213 
214  /**
215  * @see PersistentObject::afterUpdate()
216  */
217  public function afterUpdate() {
218  return $this->__call(__FUNCTION__, array());
219  }
220 
221  /**
222  * @see PersistentObject::beforeDelete()
223  */
224  public function beforeDelete() {
225  return $this->__call(__FUNCTION__, array());
226  }
227 
228  /**
229  * @see PersistentObject::afterDelete()
230  */
231  public function afterDelete() {
232  return $this->__call(__FUNCTION__, array());
233  }
234 
235  /**
236  * Get the value of a named item.
237  * @param $name The name of the item to query.
238  * @return The value of the item / null if it doesn't exits.
239  */
240  public function getValue($name) {
241  // return pk values as they are parts of the oid
242  $mapper = ObjectFactory::getInstance('persistenceFacade')->getMapper($this->getType());
243  $pkNames = $mapper->getPkNames();
244  for ($i=0, $count=sizeof($pkNames); $i<$count; $i++) {
245  if ($name == $pkNames[$i]) {
246  $ids = $this->_oid->getId();
247  return $ids[$i];
248  }
249  }
250  return $this->__call(__FUNCTION__, array($name));
251  }
252 
253  /**
254  * @see PersistentObject::setValue()
255  */
256  public function setValue($name, $value, $forceSet=false, $trackChange=true) {
257  return $this->__call(__FUNCTION__, array($name, $value, $forceSet, $trackChange));
258  }
259 
260  /**
261  * @see PersistentObject::hasValue()
262  */
263  public function hasValue($name) {
264  return $this->__call(__FUNCTION__, array($name));
265  }
266 
267  /**
268  * @see PersistentObject::removeValue()
269  */
270  public function removeValue($name) {
271  return $this->__call(__FUNCTION__, array($name));
272  }
273 
274  /**
275  * @see PersistentObject::validateValues()
276  */
277  public function validateValues(Message $message) {
278  return $this->__call(__FUNCTION__, array($message));
279  }
280 
281  /**
282  * @see PersistentObject::validateValue()
283  */
284  public function validateValue($name, $value, Message $message) {
285  return $this->__call(__FUNCTION__, array($name, $value, $message));
286  }
287 
288  /**
289  * @see PersistentObject::getChangedValues()
290  */
291  public function getChangedValues() {
292  return $this->__call(__FUNCTION__, array());
293  }
294 
295  /**
296  * @see PersistentObject::getOriginalValues()
297  */
298  public function getOriginalValues() {
299  return $this->__call(__FUNCTION__, array());
300  }
301 
302  /**
303  * @see PersistentObject::getIndispensableObjects()
304  */
305  public function getIndispensableObjects() {
306  return $this->__call(__FUNCTION__, array());
307  }
308 
309  /**
310  * @see PersistentObject::getProperty()
311  */
312  public function getProperty($name) {
313  return $this->__call(__FUNCTION__, array($name));
314  }
315 
316  /**
317  * @see PersistentObject::setProperty()
318  */
319  public function setProperty($name, $value) {
320  return $this->__call(__FUNCTION__, array($name, $value));
321  }
322 
323  /**
324  * @see PersistentObject::getPropertyNames()
325  */
326  public function getPropertyNames() {
327  return $this->__call(__FUNCTION__, array());
328  }
329 
330  /**
331  * @see PersistentObject::getValueProperty()
332  */
333  public function getValueProperty($name, $property) {
334  return $this->__call(__FUNCTION__, array($name, $property));
335  }
336 
337  /**
338  * @see PersistentObject::setValueProperty()
339  */
340  public function setValueProperty($name, $property, $value) {
341  return $this->__call(__FUNCTION__, array($name, $property, $value));
342  }
343 
344  /**
345  * @see PersistentObject::getValuePropertyNames()
346  */
347  public function getValuePropertyNames($name) {
348  return $this->__call(__FUNCTION__, array($name));
349  }
350 
351  /**
352  * @see PersistentObject::getValueNames()
353  */
354  public function getValueNames($excludeTransient=false) {
355  return $this->__call(__FUNCTION__, array($excludeTransient));
356  }
357 
358  /**
359  * @see PersistentObject::getDisplayValue()
360  * @note Subclasses will override this for special application requirements
361  */
362  public function getDisplayValue() {
363  return $this->getOID()->__toString();
364  }
365 
366  /**
367  * @see PersistentObject::dump()
368  */
369  public function dump() {
370  return $this->__call(__FUNCTION__, array());
371  }
372 
373  /**
374  * Get a string representation of the instance.
375  * @return String
376  */
377  public function __toString() {
378  return 'Proxy_'.$this->_oid->__toString();
379  }
380 }
381 ?>
setValue($name, $value, $forceSet=false, $trackChange=true)
static fromObject($object)
Create a PersistenceProxy instance from a PersistentObject.
validateValue($name, $value, Message $message)
getValue($name)
Get the value of a named item.
IllegalArgumentException signals an exception in method arguments.
__call($name, array $arguments)
Delegate method call to the instance.
getOID()
Get the object id of the PersistentObject.
Persistence layer related interfaces and classes.
Definition: namespaces.php:42
ObjectId is the unique identifier of an object.
Definition: ObjectId.php:27
getType()
Get the type of the PersistentObject.
getRealSubject()
Get the PersistentObject instance.
static getInstance($name, $dynamicConfiguration=array())
Message is used to get localized messages to be used in the user interface.
Definition: Message.php:23
copyValues(PersistentObject $object, $copyPkValues=true)
PersistentObjectProxy is proxy for an PersistentObject instance.
PersistenceException signals an exception in the persistence service.
resolve($buildDepth=BuildDepth::SINGLE)
Load the PersistentObject instance.
__toString()
Get a string representation of the instance.
PersistentObject defines the interface of all persistent objects.