1 | <?php |
||
10 | final class WithFieldSerializers implements Event |
||
11 | { |
||
12 | /** |
||
13 | * @var AggregateRootId |
||
14 | */ |
||
15 | private $aggregateRootId; |
||
16 | |||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | private $items; |
||
21 | |||
22 | /** |
||
23 | * @var PointInTime |
||
24 | */ |
||
25 | private $timeOfRecording; |
||
26 | |||
27 | public function __construct( |
||
28 | AggregateRootId $aggregateRootId, |
||
29 | PointInTime $timeOfRecording, |
||
30 | array $items |
||
31 | ) { |
||
32 | $this->aggregateRootId = $aggregateRootId; |
||
33 | $this->timeOfRecording = $timeOfRecording; |
||
34 | $this->items = $items; |
||
35 | } |
||
36 | |||
37 | public function aggregateRootId(): AggregateRootId |
||
38 | { |
||
39 | return $this->aggregateRootId; |
||
40 | } |
||
41 | |||
42 | public function items(): array |
||
43 | { |
||
44 | return $this->items; |
||
45 | } |
||
46 | |||
47 | public function eventVersion(): int |
||
48 | { |
||
49 | return 1; |
||
50 | } |
||
51 | |||
52 | public function timeOfRecording(): PointInTime |
||
53 | { |
||
54 | return $this->timeOfRecording; |
||
55 | } |
||
56 | |||
57 | public static function fromPayload( |
||
58 | array $payload, |
||
59 | AggregateRootId $aggregateRootId, |
||
60 | PointInTime $timeOfRecording): Event |
||
61 | { |
||
62 | return new WithFieldSerializers( |
||
63 | $aggregateRootId, |
||
64 | $timeOfRecording, |
||
65 | array_map(function ($property) { |
||
66 | return ['property' => $property]; |
||
67 | }, $payload['items']) |
||
68 | ); |
||
69 | } |
||
70 | |||
71 | public function toPayload(): array |
||
72 | { |
||
73 | return [ |
||
74 | 'items' => array_map(function ($item) { |
||
75 | return $item['property']; |
||
76 | }, $this->items) |
||
77 | ]; |
||
78 | } |
||
79 | |||
80 | public static function withItems(AggregateRootId $aggregateRootId, PointInTime $timeOfRecording, array $items): WithFieldSerializers |
||
81 | { |
||
82 | return new WithFieldSerializers( |
||
83 | $aggregateRootId, |
||
84 | $timeOfRecording, |
||
85 | $items |
||
86 | ); |
||
87 | } |
||
88 | |||
89 | } |
||
90 | |||
92 |