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