-
Notifications
You must be signed in to change notification settings - Fork 1
/
MySQL8DateFormatting.php
44 lines (34 loc) · 1.23 KB
/
MySQL8DateFormatting.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
namespace EventSauce\MessageRepository\DoctrineMessageRepository;
use EventSauce\EventSourcing\Header;
use EventSauce\EventSourcing\Message;
use EventSauce\EventSourcing\Serialization\MessageSerializer;
use function substr;
/**
* @internal
*/
class MySQL8DateFormatting implements MessageSerializer
{
public function __construct(
private MessageSerializer $messageSerializer,
private string $field = Header::TIME_OF_RECORDING,
) {
}
public function serializeMessage(Message $message): array
{
$payload = $this->messageSerializer->serializeMessage($message);
$dateTimeString = $payload['headers'][$this->field] ?? null;
if ($dateTimeString !== null) {
$payload['headers'][$this->field] = substr($dateTimeString, 0, -2) . ':' . substr($dateTimeString, -2);
}
return $payload;
}
public function unserializePayload(array $payload): Message
{
$dateTimeString = $payload['headers'][$this->field] ?? null;
if ($dateTimeString !== null) {
$payload['headers'][$this->field] = substr($dateTimeString, 0, -3) . substr($dateTimeString, -2);
}
return $this->messageSerializer->unserializePayload($payload);
}
}