|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @copyright 2013 - 2017 Cross Solution <http://cross-solution.de> |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** */ |
|
11
|
|
|
namespace Core\Entity; |
|
12
|
|
|
|
|
13
|
|
|
use Core\Entity\Collection\ArrayCollection; |
|
14
|
|
|
use Core\Exception\ImmutablePropertyException; |
|
15
|
|
|
use Doctrine\Common\Collections\Collection; |
|
16
|
|
|
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* ${CARET} |
|
20
|
|
|
* |
|
21
|
|
|
* @author Mathias Gelhausen <[email protected]> |
|
22
|
|
|
* @todo write test |
|
23
|
|
|
*/ |
|
24
|
|
|
trait SnapshotTrait |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* |
|
28
|
|
|
* @ODM\EmbedOne(targetDocument="Core\Entity\SnapshotMeta") |
|
29
|
|
|
* @var SnapshotMeta |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $snapshotMeta; |
|
32
|
|
|
|
|
33
|
|
|
public function __construct(EntityInterface $source) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->snapshotMeta = new SnapshotMeta($source); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getSnapshotMeta() |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->snapshotMeta; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function getSnapshotAttributes() |
|
44
|
|
|
{ |
|
45
|
|
|
/** @noinspection PhpUndefinedFieldInspection */ |
|
46
|
|
|
return property_exists($this, 'snapshotAttributes') ? $this->snapshotAttributes : []; |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected function proxy() |
|
50
|
|
|
{ |
|
51
|
|
|
/* @var SnapshotMeta $meta */ |
|
52
|
|
|
$args = func_get_args(); |
|
53
|
|
|
$method = array_shift($args); |
|
54
|
|
|
$meta = $this->getSnapshotMeta(); |
|
55
|
|
|
$entity = $meta->getEntity(); |
|
56
|
|
|
$callback = [$entity, $method]; |
|
57
|
|
|
|
|
58
|
|
|
if (!is_callable($callback)) { |
|
59
|
|
|
throw new \BadMethodCallException(sprintf( |
|
60
|
|
|
'Proxy error: Method "%s" does not exist in proxied "%s"', |
|
61
|
|
|
$method, get_class($entity) |
|
62
|
|
|
)); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$return = call_user_func_array($callback, $args); |
|
66
|
|
|
|
|
67
|
|
|
return $return === $entity ? $this : $return; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected function inaccessible($property) |
|
71
|
|
|
{ |
|
72
|
|
|
throw new \DomainException(sprintf( |
|
73
|
|
|
'Property "%s" of "%s" must not be accessed directly. Please retrieve from original entity via getSnaphotMeta()->getEntity()', |
|
74
|
|
|
$property, get_class($this) |
|
75
|
|
|
)); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
protected function immutable($property) |
|
79
|
|
|
{ |
|
80
|
|
|
throw new ImmutablePropertyException($property, $this); |
|
81
|
|
|
} |
|
82
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: