16
16
use Doctrine\ORM\EntityManagerInterface;
17
17
use Doctrine\ORM\Mapping\AssociationMapping;
18
18
use Doctrine\ORM\Mapping\ClassMetadata;
19
+ use Doctrine\ORM\Mapping\EmbeddedClassMapping;
20
+ use Doctrine\ORM\Mapping\FieldMapping;
21
+ use Doctrine\ORM\Mapping\JoinColumnMapping;
19
22
use Doctrine\ORM\Mapping\MappingException as OrmMappingException;
20
23
use Doctrine\Persistence\Mapping\MappingException;
21
24
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
@@ -78,20 +81,20 @@ public function getTypes(string $class, string $property, array $context = []):
78
81
if ($metadata instanceof ClassMetadata) {
79
82
$associationMapping = $metadata->getAssociationMapping($property);
80
83
81
- if (isset ($associationMapping[ 'indexBy'] )) {
82
- $subMetadata = $this->entityManager->getClassMetadata($associationMapping[ 'targetEntity'] );
84
+ if (self::getMappingValue ($associationMapping, 'indexBy')) {
85
+ $subMetadata = $this->entityManager->getClassMetadata(self::getMappingValue( $associationMapping, 'targetEntity') );
83
86
84
87
// Check if indexBy value is a property
85
- $fieldName = $associationMapping[ 'indexBy'] ;
88
+ $fieldName = self::getMappingValue( $associationMapping, 'indexBy') ;
86
89
if (null === ($typeOfField = $subMetadata->getTypeOfField($fieldName))) {
87
- $fieldName = $subMetadata->getFieldForColumn($associationMapping[ 'indexBy'] );
90
+ $fieldName = $subMetadata->getFieldForColumn(self::getMappingValue( $associationMapping, 'indexBy') );
88
91
// Not a property, maybe a column name?
89
92
if (null === ($typeOfField = $subMetadata->getTypeOfField($fieldName))) {
90
93
// Maybe the column name is the association join column?
91
94
$associationMapping = $subMetadata->getAssociationMapping($fieldName);
92
95
93
96
$indexProperty = $subMetadata->getSingleAssociationReferencedJoinColumnName($fieldName);
94
- $subMetadata = $this->entityManager->getClassMetadata($associationMapping[ 'targetEntity'] );
97
+ $subMetadata = $this->entityManager->getClassMetadata(self::getMappingValue( $associationMapping, 'targetEntity') );
95
98
96
99
// Not a property, maybe a column name?
97
100
if (null === ($typeOfField = $subMetadata->getTypeOfField($indexProperty))) {
@@ -118,7 +121,7 @@ public function getTypes(string $class, string $property, array $context = []):
118
121
}
119
122
120
123
if ($metadata instanceof ClassMetadata && isset($metadata->embeddedClasses[$property])) {
121
- return [new Type(Type::BUILTIN_TYPE_OBJECT, false, $metadata->embeddedClasses[$property][ 'class'] )];
124
+ return [new Type(Type::BUILTIN_TYPE_OBJECT, false, self::getMappingValue( $metadata->embeddedClasses[$property], 'class') )];
122
125
}
123
126
124
127
if ($metadata->hasField($property)) {
@@ -130,7 +133,7 @@ public function getTypes(string $class, string $property, array $context = []):
130
133
131
134
$nullable = $metadata instanceof ClassMetadata && $metadata->isNullable($property);
132
135
$enumType = null;
133
- if (null !== $enumClass = $metadata->getFieldMapping($property)[ 'enumType'] ?? null) {
136
+ if (null !== $enumClass = self::getMappingValue( $metadata->getFieldMapping($property), 'enumType') ?? null) {
134
137
$enumType = new Type(Type::BUILTIN_TYPE_OBJECT, $nullable, $enumClass);
135
138
}
136
139
@@ -220,17 +223,17 @@ private function getMetadata(string $class): ?ClassMetadata
220
223
*/
221
224
private function isAssociationNullable(array|AssociationMapping $associationMapping): bool
222
225
{
223
- if (isset ($associationMapping['id']) && $associationMapping[ 'id'] ) {
226
+ if (self::getMappingValue ($associationMapping, 'id') ) {
224
227
return false;
225
228
}
226
229
227
- if (!isset ($associationMapping[ 'joinColumns'] )) {
230
+ if (!self::getMappingValue ($associationMapping, 'joinColumns')) {
228
231
return true;
229
232
}
230
233
231
- $joinColumns = $associationMapping[ 'joinColumns'] ;
234
+ $joinColumns = self::getMappingValue( $associationMapping, 'joinColumns') ;
232
235
foreach ($joinColumns as $joinColumn) {
233
- if (isset($joinColumn['nullable']) && ! $joinColumn[ 'nullable'] ) {
236
+ if (false === self::getMappingValue( $joinColumn, 'nullable') ) {
234
237
return false;
235
238
}
236
239
}
@@ -272,4 +275,13 @@ private function getPhpType(string $doctrineType): ?string
272
275
default => null,
273
276
};
274
277
}
278
+
279
+ private static function getMappingValue(array|AssociationMapping|EmbeddedClassMapping|FieldMapping|JoinColumnMapping $mapping, string $key): mixed
280
+ {
281
+ if ($mapping instanceof AssociationMapping || $mapping instanceof EmbeddedClassMapping || $mapping instanceof FieldMapping || $mapping instanceof JoinColumnMapping) {
282
+ return $mapping->$key;
283
+ }
284
+
285
+ return $mapping[$key] ?? null;
286
+ }
275
287
}
0 commit comments