Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
whataboutpereira committed Jan 23, 2025
1 parent 5a59923 commit f1193bf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Mapping/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -2165,6 +2165,20 @@ public function setDiscriminatorColumn(DiscriminatorColumnMapping|array|null $co
throw MappingException::invalidDiscriminatorColumnType($this->name, $columnDef['type']);
}

if (isset($columnDef['enumType'])) {
if (! enum_exists($columnDef['enumType'])) {
throw MappingException::nonEnumTypeMapped($this->name, $columnDef['fieldName'], $columnDef['enumType']);
}

if (
defined('Doctrine\DBAL\Types\Types::ENUM')
&& $columnDef['type'] === Types::ENUM
&& ! isset($columnDef['options']['values'])
) {
$columnDef['options']['values'] = array_column($columnDef['enumType']::cases(), 'value');
}
}

$this->discriminatorColumn = DiscriminatorColumnMapping::fromMappingArray($columnDef);
}
}
Expand Down Expand Up @@ -2202,6 +2216,16 @@ public function setDiscriminatorMap(array $map): void
);
}

$values = $this->discriminatorColumn->options['values'] ?? null;

if ($values !== null) {
$diff = array_diff(array_keys($map), $values);

if ($diff !== []) {
throw MappingException::invalidEntriesInDiscriminatorMap(array_values($diff), $this->name, $this->discriminatorColumn->enumType);
}
}

foreach ($map as $value => $className) {
$this->addDiscriminatorMapClass($value, $className);
}
Expand Down
17 changes: 17 additions & 0 deletions src/Mapping/MappingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,23 @@ public static function fileMappingDriversRequireConfiguredDirectoryPath(string|n
);
}

/**
* Returns an exception that indicates that discriminator entries used in a discriminator map
* does not exist in the backed enum provided by enumType option.
*
* @param string[] $entries The discriminator entries that could not be found.
* @param string $owningClass The class that declares the discriminator map.
*/
public static function invalidEntriesInDiscriminatorMap(array $entries, string $owningClass, string $enumType): self
{
return new self(\sprintf(
"The entries %s in the discriminator map of class '%s' do not correspond to enum cases of '%s'.",
\implode(', ', \array_map(static fn ($entry): string => "'$entry'", $entries)),
$owningClass,
$enumType
));
}

/**
* Returns an exception that indicates that a class used in a discriminator map does not exist.
* An example would be an outdated (maybe renamed) classname.
Expand Down

0 comments on commit f1193bf

Please sign in to comment.