|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of Goodwix Doctrine JSON ODM. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Goodwix\DoctrineJsonOdm\Type; |
|
10
|
|
|
|
|
11
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
|
12
|
|
|
use Doctrine\DBAL\Types\JsonType; |
|
13
|
|
|
use Goodwix\DoctrineJsonOdm\Exception\JsonOdmException; |
|
14
|
|
|
use Symfony\Component\Serializer\Exception\ExceptionInterface; |
|
15
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
16
|
|
|
|
|
17
|
|
|
class ODMType extends JsonType |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var array */ |
|
20
|
|
|
public $deserializationContext = []; |
|
21
|
|
|
/** @var SerializerInterface */ |
|
22
|
|
|
private $serializer; |
|
23
|
|
|
|
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
private $format = 'json'; |
|
26
|
|
|
|
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
private $entityClass; |
|
29
|
|
|
|
|
30
|
|
|
/** @var array */ |
|
31
|
|
|
private $serializationContext = []; |
|
32
|
|
|
|
|
33
|
18 |
|
public function getSerializer(): SerializerInterface |
|
34
|
|
|
{ |
|
35
|
18 |
|
if (null === $this->serializer) { |
|
36
|
1 |
|
throw new \RuntimeException( |
|
37
|
1 |
|
sprintf( |
|
38
|
1 |
|
'An instance of "%s" must be available. Call the "setSerializer" method.', |
|
39
|
1 |
|
SerializerInterface::class |
|
40
|
|
|
)) |
|
41
|
|
|
; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
17 |
|
return $this->serializer; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
10 |
|
public function setSerializer(SerializerInterface $serializer): void |
|
48
|
|
|
{ |
|
49
|
10 |
|
$this->serializer = $serializer; |
|
50
|
10 |
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
public function getFormat(): string |
|
53
|
|
|
{ |
|
54
|
2 |
|
return $this->format; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
public function setFormat(string $format): void |
|
58
|
|
|
{ |
|
59
|
1 |
|
$this->format = $format; |
|
60
|
1 |
|
} |
|
61
|
|
|
|
|
62
|
19 |
|
public function getEntityClass(): string |
|
63
|
|
|
{ |
|
64
|
19 |
|
if (null === $this->entityClass) { |
|
65
|
1 |
|
throw new \RuntimeException( |
|
66
|
1 |
|
'ODM entity class name must be available. Call the "setEntityClass" method.' |
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
18 |
|
return $this->entityClass; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
9 |
|
public function setEntityClass(string $entityClass): void |
|
74
|
|
|
{ |
|
75
|
9 |
|
$this->entityClass = $entityClass; |
|
76
|
9 |
|
} |
|
77
|
|
|
|
|
78
|
14 |
|
public function getSerializationContext(): array |
|
79
|
|
|
{ |
|
80
|
14 |
|
return $this->serializationContext; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
4 |
|
public function setSerializationContext(array $serializationContext): void |
|
84
|
|
|
{ |
|
85
|
4 |
|
$this->serializationContext = $serializationContext; |
|
86
|
4 |
|
} |
|
87
|
|
|
|
|
88
|
9 |
|
public function getDeserializationContext(): array |
|
89
|
|
|
{ |
|
90
|
9 |
|
return $this->deserializationContext; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
4 |
|
public function setDeserializationContext(array $deserializationContext): void |
|
94
|
|
|
{ |
|
95
|
4 |
|
$this->deserializationContext = $deserializationContext; |
|
96
|
4 |
|
} |
|
97
|
|
|
|
|
98
|
12 |
|
public function getName(): string |
|
99
|
|
|
{ |
|
100
|
12 |
|
return $this->getEntityClass(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* {@inheritdoc} |
|
105
|
|
|
* |
|
106
|
|
|
* @throws JsonOdmException |
|
107
|
|
|
*/ |
|
108
|
14 |
|
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string |
|
109
|
|
|
{ |
|
110
|
14 |
|
if (null === $value) { |
|
111
|
2 |
|
return null; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
try { |
|
115
|
12 |
|
$context = $this->getSerializationContext(); |
|
116
|
12 |
|
$value = $this->getSerializer()->serialize($value, $this->format, $context); |
|
117
|
1 |
|
} catch (ExceptionInterface $exception) { |
|
118
|
1 |
|
$message = sprintf('Serialization exception occurred for class "%s".', $this->getEntityClass()); |
|
119
|
|
|
|
|
120
|
1 |
|
throw new JsonOdmException($message, 0, $exception); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
11 |
|
return $value; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* {@inheritdoc} |
|
128
|
|
|
* |
|
129
|
|
|
* @throws JsonOdmException |
|
130
|
|
|
*/ |
|
131
|
10 |
|
public function convertToPHPValue($value, AbstractPlatform $platform): ?object |
|
132
|
|
|
{ |
|
133
|
10 |
|
if (null === $value || '' === $value) { |
|
134
|
3 |
|
return null; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
try { |
|
138
|
7 |
|
$context = $this->getDeserializationContext(); |
|
139
|
7 |
|
$value = $this->getSerializer()->deserialize($value, $this->getEntityClass(), $this->format, $context); |
|
140
|
1 |
|
} catch (ExceptionInterface $exception) { |
|
141
|
1 |
|
$message = sprintf('Deserialization exception occurred for class "%s".', $this->getEntityClass()); |
|
142
|
|
|
|
|
143
|
1 |
|
throw new JsonOdmException($message, 0, $exception); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
6 |
|
return $value; |
|
|
|
|
|
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
5 |
|
public static function registerODMType(string $entityClass, SerializerInterface $serializer): void |
|
150
|
|
|
{ |
|
151
|
5 |
|
if (!class_exists($entityClass) && !interface_exists($entityClass)) { |
|
152
|
1 |
|
throw new \DomainException(sprintf('Class or interface "%s" does not exist.', $entityClass)); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
4 |
|
self::addType($entityClass, static::class); |
|
156
|
|
|
|
|
157
|
|
|
/** @var ODMType $type */ |
|
158
|
4 |
|
$type = self::getType($entityClass); |
|
159
|
4 |
|
$type->setEntityClass($entityClass); |
|
160
|
4 |
|
$type->setSerializer($serializer); |
|
161
|
4 |
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|