|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Kreta package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Beñat Espiña <[email protected]> |
|
7
|
|
|
* (c) Gorka Laucirica <[email protected]> |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
declare(strict_types=1); |
|
14
|
|
|
|
|
15
|
|
|
namespace Kreta\Notifier\Infrastructure\Serialization\JMS; |
|
16
|
|
|
|
|
17
|
|
|
use JMS\Serializer\SerializerBuilder; |
|
18
|
|
|
use JMS\Serializer\SerializerInterface; |
|
19
|
|
|
use Kreta\SharedKernel\Serialization\Serializer; |
|
20
|
|
|
|
|
21
|
|
|
final class JMSSerializer implements Serializer |
|
22
|
|
|
{ |
|
23
|
|
|
private $serializer; |
|
24
|
|
|
private $format; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(string $format = 'json') |
|
27
|
|
|
{ |
|
28
|
|
|
$this->serializer = $this->serializer(); |
|
29
|
|
|
$this->format = $format; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function serialize($object) : string |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->serializer->serialize($object, $this->format); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function deserialize(string $serializedObject, string $type) |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
return $this->serializer->deserialize($serializedObject, $type, $this->format); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
private function serializer() : SerializerInterface |
|
43
|
|
|
{ |
|
44
|
|
|
return SerializerBuilder::create() |
|
45
|
|
|
->addMetadataDir( |
|
46
|
|
|
__DIR__ . '/../../../../../../vendor/kreta/shared-kernel/src/Kreta/SharedKernel' . |
|
47
|
|
|
'/Infrastructure/Serialization/JMS/Config/Identity', |
|
48
|
|
|
'Kreta\\SharedKernel\\Domain\\Model\\Identity' |
|
49
|
|
|
) |
|
50
|
|
|
->addMetadataDir(__DIR__ . '/Config/Inbox', 'Kreta\\Notifier\\Domain\\Model\\Inbox') |
|
51
|
|
|
->addMetadataDir( |
|
52
|
|
|
__DIR__ . '/Config/Inbox/Notification', |
|
53
|
|
|
'Kreta\\Notifier\\Domain\\Model\\Inbox\\Notification' |
|
54
|
|
|
) |
|
55
|
|
|
->setCacheDir(__DIR__ . '/../../../../../../var/cache/jms-serializer') |
|
56
|
|
|
->build(); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.