1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Kévin Dunglas <[email protected]> |
5
|
|
|
* |
6
|
|
|
* This source file is subject to the MIT license that is bundled |
7
|
|
|
* with this source code in the file LICENSE. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Dunglas\DoctrineJsonOdm\Type; |
11
|
|
|
|
12
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
13
|
|
|
use Doctrine\DBAL\Types\JsonArrayType; |
14
|
|
|
use Doctrine\DBAL\Types\JsonType; |
15
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
16
|
|
|
|
17
|
|
|
if (class_exists(JsonType::class)) { |
18
|
|
|
/** |
19
|
|
|
* @internal |
20
|
|
|
*/ |
21
|
|
|
class InternalParentClass extends JsonType |
22
|
|
|
{ |
23
|
|
|
} |
24
|
|
|
} else { |
25
|
|
|
/** |
26
|
|
|
* @internal |
27
|
|
|
*/ |
28
|
|
|
class InternalParentClass extends JsonArrayType |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The JSON document type. |
35
|
|
|
* |
36
|
|
|
* @author Kévin Dunglas <[email protected]> |
37
|
|
|
*/ |
38
|
|
|
final class JsonDocumentType extends InternalParentClass |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* @var SerializerInterface |
42
|
|
|
*/ |
43
|
|
|
private $serializer; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $format = 'json'; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
private $serializationContext = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
private $deserializationContext = []; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Sets the serializer to use. |
62
|
|
|
* |
63
|
|
|
* @param SerializerInterface $serializer |
64
|
|
|
*/ |
65
|
|
|
public function setSerializer(SerializerInterface $serializer) |
66
|
|
|
{ |
67
|
|
|
$this->serializer = $serializer; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Gets the serializer or throw an exception if it isn't available. |
72
|
|
|
* |
73
|
|
|
* @throws \RuntimeException |
74
|
|
|
* |
75
|
|
|
* @return SerializerInterface |
76
|
|
|
*/ |
77
|
|
|
private function getSerializer() |
78
|
|
|
{ |
79
|
|
|
if (null === $this->serializer) { |
80
|
|
|
throw new \RuntimeException(sprintf('An instance of "%s" must be available. Call the "setSerializer" method.', SerializerInterface::class)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->serializer; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Sets the serialization format (default to "json"). |
88
|
|
|
* |
89
|
|
|
* @param string $format |
90
|
|
|
*/ |
91
|
|
|
public function setFormat($format) |
92
|
|
|
{ |
93
|
|
|
$this->format = $format; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Sets the serialization context (default to an empty array). |
98
|
|
|
* |
99
|
|
|
* @param array $serializationContext |
100
|
|
|
*/ |
101
|
|
|
public function setSerializationContext(array $serializationContext) |
102
|
|
|
{ |
103
|
|
|
$this->serializationContext = $serializationContext; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Sets the deserialization context (default to an empty array). |
108
|
|
|
* |
109
|
|
|
* @param array $deserializationContext |
110
|
|
|
*/ |
111
|
|
|
public function setDeserializationContext(array $deserializationContext) |
112
|
|
|
{ |
113
|
|
|
$this->deserializationContext = $deserializationContext; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
|
|
public function convertToDatabaseValue($value, AbstractPlatform $platform) |
120
|
|
|
{ |
121
|
|
|
if (null === $value) { |
122
|
|
|
return; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $this->getSerializer()->serialize($value, $this->format, $this->serializationContext); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
|
|
public function convertToPHPValue($value, AbstractPlatform $platform) |
132
|
|
|
{ |
133
|
|
|
if (null === $value || $value === '') { |
134
|
|
|
return; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $this->getSerializer()->deserialize($value, '', $this->format, $this->deserializationContext); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* {@inheritdoc} |
142
|
|
|
*/ |
143
|
|
|
public function requiresSQLCommentHint(AbstractPlatform $platform) |
144
|
|
|
{ |
145
|
|
|
return true; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritdoc} |
150
|
|
|
*/ |
151
|
|
|
public function getName() |
152
|
|
|
{ |
153
|
|
|
return 'json_document'; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
This check looks for classes that have been defined more than once in the same file.
If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.
This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.