Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
idbentley committed Aug 12, 2024
1 parent b5cc1ce commit 34439ca
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 43 deletions.
69 changes: 42 additions & 27 deletions src/Handler/UnionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,42 +67,57 @@ public function deserializeUnion(DeserializationVisitorInterface $visitor, mixed
throw new RuntimeException('XML deserialisation into union types is not supported yet.');
}

foreach ($type['params'] as $possibleType) {
$finalType = null;
$finalType = null;
if (1 === count($type['params'])) {
if ($data[$type['params'][0]]) {
$lookupField = $type['params'][0];

if (!$context->getMetadataStack()->isEmpty()) {
$propertyMetadata = $context->getMetadataStack()->top();
if (null !== $propertyMetadata->unionDiscriminatorField) {
if (!array_key_exists($propertyMetadata->unionDiscriminatorField, $data)) {
throw new NonVisitableTypeException('Union Discriminator Field \'' . $propertyMetadata->unionDiscriminatorField . '\' not found in data');
}
if (!array_key_exists($lookupField, $data)) {
throw new NonVisitableTypeException('Union Discriminator Field \'' . $lookupField . '\' not found in data');
}

$lkup = $data[$propertyMetadata->unionDiscriminatorField];
if (!empty($propertyMetadata->unionDiscriminatorMap)) {
if (array_key_exists($lkup, $propertyMetadata->unionDiscriminatorMap)) {
$finalType = [
'name' => $propertyMetadata->unionDiscriminatorMap[$lkup],
'params' => [],
];
} else {
throw new NonVisitableTypeException('Union Discriminator Map does not contain key \'' . $lkup . '\'');
}
} else {
$lkup = $data[$lookupField];
$finalType = [
'name' => $lkup,
'params' => [],
];
}
} elseif (2 === count($type['params'])) {
if (is_array($type['params'][1]) && !array_key_exists('name', $type['params'][1])) {
$lookupField = $type['params'][0];
$unionMap = $type['params'][1];

if (!array_key_exists($lookupField, $data)) {
throw new NonVisitableTypeException('Union Discriminator Field \'' . $lookupField . '\' not found in data');
}

$lkup = $data[$lookupField];
if (!empty($unionMap)) {
if (array_key_exists($lkup, $unionMap)) {
$finalType = [
'name' => $lkup,
'name' => $unionMap[$lkup],
'params' => [],
];
} else {
throw new NonVisitableTypeException('Union Discriminator Map does not contain key \'' . $lkup . '\'');
}
} else {
$finalType = [
'name' => $lkup,
'params' => [],
];
}
}
}

if (null !== $finalType && null !== $finalType['name']) {
return $context->getNavigator()->accept($data, $finalType);
} else {
foreach ($type['params'] as $possibleType) {
if ($this->isPrimitiveType($possibleType['name']) && $this->testPrimitive($data, $possibleType['name'], $context->getFormat())) {
return $context->getNavigator()->accept($data, $possibleType);
}
if (null !== $finalType && null !== $finalType['name']) {
return $context->getNavigator()->accept($data, $finalType);
} else {
foreach ($type['params'] as $possibleType) {
$finalType = null;

if ($this->isPrimitiveType($possibleType['name']) && $this->testPrimitive($data, $possibleType['name'], $context->getFormat())) {
return $context->getNavigator()->accept($data, $possibleType);
}
}
}
Expand Down
45 changes: 29 additions & 16 deletions src/Metadata/Driver/TypedPropertiesDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,40 @@ public function loadMetadataForClass(ReflectionClass $class): ?ClassMetadata
// If the inner driver provides a type, don't guess anymore.
if ($propertyMetadata->type) {
if ('union' === $propertyMetadata->type['name']) {
// If the property has a unionDiscriminator annotation, overwrite the types array with the discriminator.
$propertyMetadata->setType($this->reorderTypes($propertyMetadata->type));
}

continue;
} else {
try {
$reflectionType = $this->getReflectionType($propertyMetadata);

if ($this->shouldTypeHint($reflectionType)) {
$type = $reflectionType->getName();

$propertyMetadata->setType($this->typeParser->parse($type));
} elseif ($this->shouldTypeHintUnion($reflectionType)) {
$propertyMetadata->setType($this->reorderTypes([
'name' => 'union',
'params' => array_map(fn (string $type) => $this->typeParser->parse($type), $reflectionType->getTypes()),
]));
}
} catch (ReflectionException $e) {
continue;
}
}

try {
$reflectionType = $this->getReflectionType($propertyMetadata);

if ($this->shouldTypeHint($reflectionType)) {
$type = $reflectionType->getName();

$propertyMetadata->setType($this->typeParser->parse($type));
} elseif ($this->shouldTypeHintUnion($reflectionType)) {
$propertyMetadata->setType($this->reorderTypes([
'name' => 'union',
'params' => array_map(fn (string $type) => $this->typeParser->parse($type), $reflectionType->getTypes()),
]));
// Update the type if a unionDiscriminator annotation is present.
$params = [];
if ($propertyMetadata->unionDiscriminatorField) {
$params[] = $propertyMetadata->unionDiscriminatorField;
if ($propertyMetadata->unionDiscriminatorMap) {
$params[] = $propertyMetadata->unionDiscriminatorMap;
}
} catch (ReflectionException $e) {
continue;

$propertyMetadata->setType([
'name' => 'union',
'params' => $params,
]);
}
}

Expand Down

0 comments on commit 34439ca

Please sign in to comment.