|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DDDominio\EventSourcing\Snapshotting; |
|
4
|
|
|
|
|
5
|
|
|
use DDDominio\EventSourcing\Common\EventSourcedAggregateRootInterface; |
|
6
|
|
|
|
|
7
|
|
|
abstract class ReflectionSnapshotTranslator implements SnapshotTranslatorInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @return string |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract protected function aggregateClass(); |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @return string |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract protected function snapshotClass(); |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @return array |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract protected function aggregateToSnapshotPropertyDictionary(); |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param EventSourcedAggregateRootInterface $aggregate |
|
26
|
|
|
* @return object |
|
27
|
|
|
*/ |
|
28
|
1 |
|
public function buildSnapshotFromAggregate($aggregate) |
|
29
|
|
|
{ |
|
30
|
1 |
|
$snapshotClass = $this->snapshotClass(); |
|
31
|
1 |
|
$snapshot = (new \ReflectionClass($snapshotClass))->newInstanceWithoutConstructor(); |
|
32
|
1 |
|
$reflectedClass = new \ReflectionClass($snapshotClass); |
|
33
|
|
|
|
|
34
|
1 |
|
$dictionary = $this->aggregateToSnapshotPropertyDictionary(); |
|
35
|
|
|
|
|
36
|
1 |
|
foreach ($dictionary as $aggregateProperty => $snapshotProperty) { |
|
37
|
1 |
|
$nameProperty = $reflectedClass->getProperty($snapshotProperty); |
|
38
|
1 |
|
$nameProperty->setAccessible(true); |
|
39
|
1 |
|
$nameProperty->setValue($snapshot, $aggregate->$aggregateProperty()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
return $snapshot; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param SnapshotInterface $snapshot |
|
47
|
|
|
* @return object |
|
48
|
|
|
*/ |
|
49
|
2 |
|
public function buildAggregateFromSnapshot($snapshot) |
|
50
|
|
|
{ |
|
51
|
2 |
|
$aggregateClass = $this->aggregateClass(); |
|
52
|
2 |
|
$aggregate = (new \ReflectionClass($aggregateClass))->newInstanceWithoutConstructor(); |
|
53
|
2 |
|
$reflectedClass = new \ReflectionClass($aggregateClass); |
|
54
|
|
|
|
|
55
|
2 |
|
$dictionary = $this->aggregateToSnapshotPropertyDictionary(); |
|
56
|
2 |
|
unset($dictionary['version']); |
|
57
|
2 |
|
$this->setPropertyValue('version', $snapshot->version(), $aggregate, $reflectedClass->getParentClass()); |
|
58
|
|
|
|
|
59
|
2 |
|
foreach ($dictionary as $aggregateProperty => $snapshotProperty) { |
|
60
|
2 |
|
$this->setPropertyValue( |
|
61
|
|
|
$aggregateProperty, |
|
62
|
2 |
|
$snapshot->$snapshotProperty(), |
|
63
|
|
|
$aggregate, |
|
64
|
|
|
$reflectedClass |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
return $aggregate; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $propertyName |
|
73
|
|
|
* @param string $propertyValue |
|
74
|
|
|
* @param object $aggregate |
|
75
|
|
|
* @param \ReflectionClass $reflectedClass |
|
76
|
|
|
*/ |
|
77
|
2 |
|
private function setPropertyValue($propertyName, $propertyValue, $aggregate, $reflectedClass) |
|
78
|
|
|
{ |
|
79
|
2 |
|
$nameProperty = $reflectedClass->getProperty($propertyName); |
|
80
|
2 |
|
$nameProperty->setAccessible(true); |
|
81
|
2 |
|
$nameProperty->setValue($aggregate, $propertyValue); |
|
82
|
2 |
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|