|
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(); |
|
36
|
|
|
$this->snapshotEntity = $source; |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getOriginalEntity() |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->snapshotEntity; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getSnapshotMeta() |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->snapshotMeta; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function getSnapshotAttributes() |
|
50
|
|
|
{ |
|
51
|
|
|
/** @noinspection PhpUndefinedFieldInspection */ |
|
52
|
|
|
return property_exists($this, 'snapshotAttributes') ? $this->snapshotAttributes : []; |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $method |
|
59
|
|
|
* @param mixed[] ...$args Arguments to be passed to proxied method. |
|
60
|
|
|
* |
|
61
|
|
|
* @return SnapshotTrait |
|
|
|
|
|
|
62
|
|
|
*/ |
|
63
|
|
|
protected function proxy($method, ...$args) |
|
64
|
|
|
{ |
|
65
|
|
|
$entity = $this->getOriginalEntity(); |
|
66
|
|
|
$callback = [$entity, $method]; |
|
67
|
|
|
|
|
68
|
|
|
if (!is_callable($callback)) { |
|
69
|
|
|
throw new \BadMethodCallException(sprintf( |
|
70
|
|
|
'Proxy error: Method "%s" does not exist in proxied "%s"', |
|
71
|
|
|
$method, get_class($entity) |
|
72
|
|
|
)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$return = $callback(...$args); |
|
76
|
|
|
|
|
77
|
|
|
return $return === $entity ? $this : $return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
protected function proxyClone($method, ...$args) |
|
81
|
|
|
{ |
|
82
|
|
|
$value = $this->proxy($method, ...$args); |
|
83
|
|
|
$return = is_object($value) ? clone $value : $value; |
|
84
|
|
|
|
|
85
|
|
|
return $return; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected function inaccessible($property) |
|
89
|
|
|
{ |
|
90
|
|
|
throw new \DomainException(sprintf( |
|
91
|
|
|
'Property "%s" of "%s" must not be accessed directly. Please retrieve from original entity via getSnaphotMeta()->getEntity()', |
|
92
|
|
|
$property, get_class($this) |
|
93
|
|
|
)); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
protected function immutable($property) |
|
97
|
|
|
{ |
|
98
|
|
|
throw new ImmutablePropertyException($property, $this); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
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: