1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File was created 28.04.2016 08:28 |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace PeekAndPoke\Component\Slumber\Data\Addon\Journal\DomainModel; |
7
|
|
|
|
8
|
|
|
use PeekAndPoke\Component\Slumber\Annotation\Slumber; |
9
|
|
|
use PeekAndPoke\Component\Slumber\Data\Addon\SlumberId; |
10
|
|
|
use PeekAndPoke\Component\Slumber\Data\Addon\SlumberTimestamped; |
11
|
|
|
use PeekAndPoke\Component\Slumber\Data\Addon\UserRecord\SlumberRecordUser; |
12
|
|
|
use PeekAndPoke\Component\Slumber\Data\Addon\UserRecord\UserRecord; |
13
|
|
|
use PeekAndPoke\Component\Toolbox\ArrayUtil; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Karsten J. Gerber <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class JournalEntry implements Record |
19
|
|
|
{ |
20
|
|
|
use SlumberId; |
21
|
|
|
use SlumberTimestamped; |
22
|
|
|
use SlumberRecordUser; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param string $externalReference |
26
|
|
|
* @param array $recordData |
27
|
|
|
* |
28
|
|
|
* @return JournalEntry |
29
|
|
|
*/ |
30
|
4 |
|
public static function create($externalReference, $recordData) |
31
|
|
|
{ |
32
|
4 |
|
$ret = new static; |
33
|
4 |
|
$ret->externalReference = $externalReference; |
34
|
4 |
|
$ret->recordData = $recordData; |
35
|
|
|
|
36
|
4 |
|
return $ret; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
* |
42
|
|
|
* @Slumber\AsIs() |
43
|
|
|
*/ |
44
|
|
|
private $recordData = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
* |
49
|
|
|
* @Slumber\AsString() |
50
|
|
|
* @Slumber\Store\Indexed(background = true) |
51
|
|
|
*/ |
52
|
|
|
private $externalReference; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var bool |
56
|
|
|
* |
57
|
|
|
* @Slumber\AsBool() |
58
|
|
|
* @Slumber\Store\Indexed(background = true) |
59
|
|
|
*/ |
60
|
|
|
private $isCompacted = false; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var array |
64
|
|
|
* |
65
|
|
|
* @Slumber\AsIs() |
66
|
|
|
*/ |
67
|
|
|
private $compactedData = []; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* JournalEntry constructor. |
71
|
|
|
*/ |
72
|
4 |
|
public function __construct() |
73
|
|
|
{ |
74
|
4 |
|
$this->createdBy = new UserRecord(); |
75
|
4 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
3 |
|
public function getChangedBy() |
81
|
|
|
{ |
82
|
3 |
|
return $this->getCreatedBy()->getRole() |
83
|
3 |
|
. ' ' . $this->getCreatedBy()->getName() |
84
|
3 |
|
. '@' . $this->getCreatedBy()->getIp(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get the date of record creation |
89
|
|
|
* |
90
|
|
|
* @return \DateTime |
91
|
|
|
*/ |
92
|
3 |
|
public function getChangeDate() |
93
|
|
|
{ |
94
|
3 |
|
return $this->getCreatedAt(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param \DateTime $date |
99
|
|
|
* |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
2 |
|
public function setChangeDate(\DateTime $date) |
103
|
|
|
{ |
104
|
2 |
|
$this->createdAt = $date; |
105
|
|
|
|
106
|
2 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
3 |
|
public function getRecordData() |
113
|
|
|
{ |
114
|
3 |
|
return $this->recordData; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param array $recordData |
119
|
|
|
* |
120
|
|
|
* @return $this |
121
|
|
|
*/ |
122
|
2 |
|
public function setRecordData($recordData) |
123
|
|
|
{ |
124
|
2 |
|
$this->recordData = $recordData; |
125
|
|
|
|
126
|
2 |
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
1 |
|
public function getExternalReference() |
133
|
|
|
{ |
134
|
1 |
|
return $this->externalReference; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $externalReference |
139
|
|
|
* |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
2 |
|
public function setExternalReference($externalReference) |
143
|
|
|
{ |
144
|
2 |
|
$this->externalReference = $externalReference; |
145
|
|
|
|
146
|
2 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return boolean |
151
|
|
|
*/ |
152
|
3 |
|
public function getIsCompacted() |
153
|
|
|
{ |
154
|
3 |
|
return $this->isCompacted; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param boolean $isCompacted |
159
|
|
|
* |
160
|
|
|
* @return $this |
161
|
|
|
*/ |
162
|
2 |
|
public function setIsCompacted($isCompacted) |
163
|
|
|
{ |
164
|
2 |
|
$this->isCompacted = $isCompacted; |
165
|
|
|
|
166
|
2 |
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
2 |
|
public function getCompactedData() |
173
|
|
|
{ |
174
|
2 |
|
return $this->compactedData; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @param array $compactedData |
179
|
|
|
* |
180
|
|
|
* @return $this |
181
|
|
|
*/ |
182
|
2 |
|
public function setCompactedData($compactedData) |
183
|
|
|
{ |
184
|
2 |
|
$this->compactedData = $compactedData; |
185
|
|
|
|
186
|
2 |
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @return RecordableHistory |
191
|
|
|
*/ |
192
|
2 |
|
public function getCompactedHistory() |
193
|
|
|
{ |
194
|
2 |
|
$data = $this->getCompactedData(); |
195
|
|
|
|
196
|
2 |
|
$initialRecord = new JournalEntry(); |
197
|
2 |
|
$initialRecord->setChangeDate( |
198
|
2 |
|
new \DateTime(ArrayUtil::getNested($data, 'initialRecord.changeDate', 'now')) |
199
|
|
|
); |
200
|
2 |
|
$initialRecord->setRecordData(ArrayUtil::getNested($data, 'initialRecord.recordData', [])); |
201
|
|
|
|
202
|
2 |
|
$finalRecord = new JournalEntry(); |
203
|
2 |
|
$finalRecord->setChangeDate( |
204
|
2 |
|
new \DateTime(ArrayUtil::getNested($data, 'finalRecord.changeDate', 'now')) |
205
|
|
|
); |
206
|
2 |
|
$finalRecord->setRecordData(ArrayUtil::getNested($data, 'finalRecord.recordData', [])); |
207
|
|
|
|
208
|
2 |
|
$diffs = []; |
209
|
|
|
|
210
|
|
|
/** @noinspection ForeachSourceInspection */ |
211
|
2 |
|
foreach (ArrayUtil::getNested($data, 'diffs', []) as $diffData) { |
212
|
|
|
|
213
|
2 |
|
$diff = new RecordDiff( |
214
|
2 |
|
new \DateTime(ArrayUtil::getNested($diffData, 'changeDate')), |
215
|
2 |
|
ArrayUtil::getNested($diffData, 'changedBy') |
216
|
|
|
); |
217
|
|
|
|
218
|
|
|
/** @noinspection ForeachSourceInspection */ |
219
|
2 |
|
foreach (ArrayUtil::getNested($diffData, 'changes', []) as $changeData) { |
220
|
|
|
|
221
|
2 |
|
$key = ArrayUtil::getNested($changeData, 'key'); |
222
|
2 |
|
$before = ArrayUtil::getNested($changeData, 'before'); |
223
|
2 |
|
$after = ArrayUtil::getNested($changeData, 'after'); |
224
|
|
|
|
225
|
2 |
|
$diff->addChange(new RecordDiffEntry($key, $before, $after)); |
226
|
|
|
} |
227
|
|
|
|
228
|
2 |
|
$diffs[] = $diff; |
229
|
|
|
} |
230
|
|
|
|
231
|
2 |
|
$history = RecordableHistory::fromInitialAndFinalAndDiffs($initialRecord, $finalRecord, $diffs); |
232
|
|
|
|
233
|
2 |
|
return $history; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @param RecordableHistory $history |
238
|
|
|
* |
239
|
|
|
* @return $this |
240
|
|
|
*/ |
241
|
2 |
|
public function setCompactedHistory(RecordableHistory $history) |
242
|
|
|
{ |
243
|
2 |
|
$this->setIsCompacted(true); |
244
|
|
|
|
245
|
2 |
|
$initial = $history->getInitialRecord(); |
246
|
2 |
|
$final = $history->getFinalRecord(); |
247
|
|
|
|
248
|
|
|
$data = [ |
249
|
2 |
|
'initialRecord' => [ |
250
|
2 |
|
'changeDate' => $initial->getChangeDate()->format('c'), |
251
|
2 |
|
'changedBy' => $initial->getChangedBy(), |
252
|
2 |
|
'recordData' => $initial->getRecordData(), |
253
|
|
|
], |
254
|
|
|
'finalRecord' => [ |
255
|
2 |
|
'changeDate' => $final->getChangeDate()->format('c'), |
256
|
2 |
|
'changedBy' => $final->getChangedBy(), |
257
|
2 |
|
'recordData' => $final->getRecordData(), |
258
|
|
|
], |
259
|
|
|
'diffs' => [], |
260
|
|
|
]; |
261
|
|
|
|
262
|
2 |
|
foreach ($history->getDiffs() as $diff) { |
263
|
|
|
$diffData = [ |
264
|
2 |
|
'changeDate' => $diff->getChangeDate()->format('c'), |
265
|
2 |
|
'changedBy' => $diff->getChangedBy(), |
266
|
|
|
'changes' => [], |
267
|
|
|
]; |
268
|
|
|
|
269
|
2 |
|
foreach ($diff->getChanges() as $change) { |
270
|
|
|
|
271
|
2 |
|
$diffData['changes'][] = [ |
272
|
2 |
|
'key' => $change->getKey(), |
273
|
2 |
|
'before' => $change->getBefore(), |
274
|
2 |
|
'after' => $change->getAfter(), |
275
|
|
|
]; |
276
|
|
|
} |
277
|
|
|
|
278
|
2 |
|
$data['diffs'][] = $diffData; |
279
|
|
|
} |
280
|
|
|
|
281
|
2 |
|
$this->setCompactedData($data); |
282
|
|
|
|
283
|
2 |
|
return $this; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
} |
287
|
|
|
|