1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace JMS\Serializer\Metadata\Driver; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
8
|
|
|
use Doctrine\Persistence\Mapping\ClassMetadata as DoctrineClassMetadata; |
9
|
|
|
use JMS\Serializer\Metadata\ClassMetadata; |
10
|
|
|
use JMS\Serializer\Metadata\PropertyMetadata; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* This class decorates any other driver. If the inner driver does not provide a |
14
|
|
|
* a property type, the decorator will guess based on Doctrine 2 metadata. |
15
|
|
|
*/ |
16
|
|
|
class DoctrineTypeDriver extends AbstractDoctrineTypeDriver |
17
|
10 |
|
{ |
18
|
|
|
protected function setDiscriminator(DoctrineClassMetadata $doctrineMetadata, ClassMetadata $classMetadata): void |
19
|
10 |
|
{ |
20
|
10 |
|
if ( |
21
|
|
|
empty($classMetadata->discriminatorMap) && !$classMetadata->discriminatorDisabled |
22
|
3 |
|
&& !empty($doctrineMetadata->discriminatorMap) && $doctrineMetadata->isRootEntity() |
|
|
|
|
23
|
3 |
|
) { |
24
|
3 |
|
$classMetadata->setDiscriminator( |
25
|
|
|
$doctrineMetadata->discriminatorColumn['name'], |
|
|
|
|
26
|
|
|
$doctrineMetadata->discriminatorMap |
27
|
10 |
|
); |
28
|
|
|
} |
29
|
10 |
|
} |
30
|
|
|
|
31
|
10 |
|
/** |
32
|
10 |
|
* @param ClassMetadataInfo $doctrineMetadata |
33
|
10 |
|
* @param PropertyMetadata $propertyMetadata |
34
|
8 |
|
* |
35
|
8 |
|
* @return void |
36
|
|
|
*/ |
37
|
8 |
|
protected function setPropertyType(DoctrineClassMetadata $doctrineMetadata, PropertyMetadata $propertyMetadata): void |
38
|
|
|
{ |
39
|
|
|
$propertyName = $propertyMetadata->name; |
40
|
|
|
if ( |
41
|
|
|
$doctrineMetadata->hasField($propertyName) |
42
|
|
|
&& ($typeOfFiled = $doctrineMetadata->getTypeOfField($propertyName)) |
43
|
|
|
&& ($fieldType = $this->normalizeFieldType($typeOfFiled)) |
44
|
8 |
|
) { |
45
|
1 |
|
if (PHP_VERSION_ID >= 80100 && ($fieldMapping = $doctrineMetadata->getFieldMapping($propertyName)) && isset($fieldMapping['enumType'])) { |
|
|
|
|
46
|
|
|
$fieldType = $fieldMapping['enumType']; |
47
|
|
|
} |
48
|
7 |
|
|
49
|
7 |
|
$propertyMetadata->setType($this->typeParser->parse($fieldType)); |
50
|
|
|
|
51
|
|
|
return; |
52
|
7 |
|
} |
53
|
|
|
|
54
|
10 |
|
if ($doctrineMetadata->hasAssociation($propertyName)) { |
55
|
|
|
$targetEntity = $doctrineMetadata->getAssociationTargetClass($propertyName); |
56
|
|
|
|
57
|
|
|
if (null === $targetMetadata = $this->tryLoadingDoctrineMetadata($targetEntity)) { |
|
|
|
|
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// For inheritance schemes, we cannot add any type as we would only add the super-type of the hierarchy. |
62
|
|
|
// On serialization, this would lead to only the supertype being serialized, and properties of subtypes |
63
|
|
|
// being ignored. |
64
|
|
|
if ($targetMetadata instanceof DoctrineClassMetadata && !$targetMetadata->isInheritanceTypeNone()) { |
|
|
|
|
65
|
|
|
return; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (!$doctrineMetadata->isSingleValuedAssociation($propertyName)) { |
69
|
|
|
$targetEntity = sprintf('ArrayCollection<%s>', $targetEntity); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$propertyMetadata->setType($this->typeParser->parse($targetEntity)); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|