PersistentObjectProxy.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 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::reset()
180  */
181  public function reset() {
182  return $this->__call(__FUNCTION__, []);
183  }
184 
185  /**
186  * @see PersistentObject::afterCreate()
187  */
188  public function afterCreate() {
189  return $this->__call(__FUNCTION__, []);
190  }
191 
192  /**
193  * @see PersistentObject::beforeInsert()
194  */
195  public function beforeInsert() {
196  return $this->__call(__FUNCTION__, []);
197  }
198 
199  /**
200  * @see PersistentObject::afterInsert()
201  */
202  public function afterInsert() {
203  return $this->__call(__FUNCTION__, []);
204  }
205 
206  /**
207  * @see PersistentObject::afterLoad()
208  */
209  public function afterLoad() {
210  return $this->__call(__FUNCTION__, []);
211  }
212 
213  /**
214  * @see PersistentObject::beforeUpdate()
215  */
216  public function beforeUpdate() {
217  return $this->__call(__FUNCTION__, []);
218  }
219 
220  /**
221  * @see PersistentObject::afterUpdate()
222  */
223  public function afterUpdate() {
224  return $this->__call(__FUNCTION__, []);
225  }
226 
227  /**
228  * @see PersistentObject::beforeDelete()
229  */
230  public function beforeDelete() {
231  return $this->__call(__FUNCTION__, []);
232  }
233 
234  /**
235  * @see PersistentObject::afterDelete()
236  */
237  public function afterDelete() {
238  return $this->__call(__FUNCTION__, []);
239  }
240 
241  /**
242  * Get the value of a named item.
243  * @param $name The name of the item to query.
244  * @return The value of the item / null if it doesn't exits.
245  */
246  public function getValue($name) {
247  // return pk values as they are parts of the oid
248  $mapper = ObjectFactory::getInstance('persistenceFacade')->getMapper($this->getType());
249  $pkNames = $mapper->getPkNames();
250  for ($i=0, $count=sizeof($pkNames); $i<$count; $i++) {
251  if ($name == $pkNames[$i]) {
252  $ids = $this->oid->getId();
253  return $ids[$i];
254  }
255  }
256  return $this->__call(__FUNCTION__, [$name]);
257  }
258 
259  /**
260  * @see PersistentObject::setValue()
261  */
262  public function setValue($name, $value, $forceSet=false, $trackChange=true) {
263  return $this->__call(__FUNCTION__, [$name, $value, $forceSet, $trackChange]);
264  }
265 
266  /**
267  * @see PersistentObject::hasValue()
268  */
269  public function hasValue($name) {
270  return $this->__call(__FUNCTION__, [$name]);
271  }
272 
273  /**
274  * @see PersistentObject::removeValue()
275  */
276  public function removeValue($name) {
277  return $this->__call(__FUNCTION__, [$name]);
278  }
279 
280  /**
281  * @see PersistentObject::validateValues()
282  */
283  public function validateValues() {
284  return $this->__call(__FUNCTION__, []);
285  }
286 
287  /**
288  * @see PersistentObject::validateValue()
289  */
290  public function validateValue($name, $value) {
291  return $this->__call(__FUNCTION__, [$name, $value]);
292  }
293 
294  /**
295  * @see PersistentObject::getChangedValues()
296  */
297  public function getChangedValues() {
298  return $this->__call(__FUNCTION__, []);
299  }
300 
301  /**
302  * @see PersistentObject::getOriginalValue()
303  */
304  public function getOriginalValue($name) {
305  return $this->__call(__FUNCTION__, [$name]);
306  }
307 
308  /**
309  * @see PersistentObject::getIndispensableObjects()
310  */
311  public function getIndispensableObjects() {
312  return $this->__call(__FUNCTION__, []);
313  }
314 
315  /**
316  * @see PersistentObject::getProperty()
317  */
318  public function getProperty($name) {
319  return $this->__call(__FUNCTION__, [$name]);
320  }
321 
322  /**
323  * @see PersistentObject::setProperty()
324  */
325  public function setProperty($name, $value) {
326  return $this->__call(__FUNCTION__, [$name, $value]);
327  }
328 
329  /**
330  * @see PersistentObject::getPropertyNames()
331  */
332  public function getPropertyNames() {
333  return $this->__call(__FUNCTION__, []);
334  }
335 
336  /**
337  * @see PersistentObject::getValueProperty()
338  */
339  public function getValueProperty($name, $property) {
340  return $this->__call(__FUNCTION__, [$name, $property]);
341  }
342 
343  /**
344  * @see PersistentObject::setValueProperty()
345  */
346  public function setValueProperty($name, $property, $value) {
347  return $this->__call(__FUNCTION__, [$name, $property, $value]);
348  }
349 
350  /**
351  * @see PersistentObject::getValuePropertyNames()
352  */
353  public function getValuePropertyNames($name) {
354  return $this->__call(__FUNCTION__, [$name]);
355  }
356 
357  /**
358  * @see PersistentObject::getValueNames()
359  */
360  public function getValueNames($excludeTransient=false) {
361  return $this->__call(__FUNCTION__, [$excludeTransient]);
362  }
363 
364  /**
365  * @see PersistentObject::getDisplayValue()
366  * @note Subclasses will override this for special application requirements
367  */
368  public function getDisplayValue() {
369  return $this->getOID()->__toString();
370  }
371 
372  /**
373  * @see PersistentObject::dump()
374  */
375  public function dump() {
376  return $this->__call(__FUNCTION__, []);
377  }
378 
379  /**
380  * Get a string representation of the instance.
381  * @return String
382  */
383  public function __toString() {
384  return 'Proxy_'.$this->oid->__toString();
385  }
386 }
387 ?>
copyValues(PersistentObject $object, $copyPkValues=true)
PersistenceException signals an exception in the persistence service.
Persistence layer related interfaces and classes.
Definition: namespaces.php:42
getOID()
Get the object id of the PersistentObject.
IllegalArgumentException signals an exception in method arguments.
getValue($name)
Get the value of a named item.
ObjectId is the unique identifier of an object.
Definition: ObjectId.php:28
__call($name, array $arguments)
Delegate method call to the instance.
__toString()
Get a string representation of the instance.
setValue($name, $value, $forceSet=false, $trackChange=true)
getType()
Get the type of the PersistentObject.
static getInstance($name, $dynamicConfiguration=[])
PersistentObjectProxy is proxy for an PersistentObject instance.
PersistentObject defines the interface of all persistent objects.
getRealSubject()
Get the PersistentObject instance.
static fromObject($object)
Create a PersistenceProxy instance from a PersistentObject.
resolve($buildDepth=BuildDepth::SINGLE)
Load the PersistentObject instance.
ObjectFactory implements the service locator pattern by wrapping a Factory instance and providing sta...