Skip to content

Commit

Permalink
fixes and better enum handling
Browse files Browse the repository at this point in the history
  • Loading branch information
addiks committed Apr 17, 2024
1 parent ada1adb commit 0bd4630
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
9 changes: 9 additions & 0 deletions DataLoader/BlackMagic/BlackMagicEntityCodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,21 @@ public function processMapping(

/** @var array<string, bool> $walkedFiles */
$walkedFiles = [$filePath => true];

/** @var string $safeFilePath */
$safeFilePath = $filePath;

do {
if (!file_exists($filePath)) {
$filePath = $safeFilePath;
break;
}

/** @var string $entityPHP */
$entityPHP = file_get_contents($filePath);

if (1 === preg_match("#\/\*\* \@addiks-original-file ([^\*]*) \*\/#is", $entityPHP, $matches)) {
$safeFilePath = $filePath;
$filePath = trim($matches[1]);

if (isset($walkedFiles[$filePath])) {
Expand Down
39 changes: 30 additions & 9 deletions Mapping/ObjectMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Connection;
use BackedEnum;
use UnitEnum;

final class ObjectMapping implements MappingInterface
{
Expand Down Expand Up @@ -163,6 +165,9 @@ public function resolveValue(
/** @var object|null $object */
$object = null;

/** @var string $columnName */
$columnName = $this->column?->getName() ?? '';

# During creation of an object, the class-name is on the top of that creation stack
$context->pushOnObjectHydrationStack($this->className);

Expand All @@ -179,9 +184,6 @@ public function resolveValue(
/** @var Type $type */
$type = $this->column->getType();

/** @var string $columnName */
$columnName = $this->column->getName();

if (array_key_exists($columnName, $dataFromAdditionalColumns)) {
/** @var Connection $connection */
$connection = $context->getEntityManager()->getConnection();
Expand All @@ -201,6 +203,13 @@ public function resolveValue(
$context,
$factoryData
);

} elseif (is_a($this->className, BackedEnum::class, true)) {
$object = call_user_func($this->className . '::from', $dataFromAdditionalColumns[$columnName]);
# $object = {$this->className}::from($dataFromAdditionalColumns[$columnName]);

} elseif (is_a($this->className, UnitEnum::class, true)) {
$object = constant(sprintf('%s::%s', $this->className, $dataFromAdditionalColumns[$columnName]));

} else {
if ($reflectionClass->isInstantiable()) {
Expand Down Expand Up @@ -312,14 +321,14 @@ public function revertValue(
$data = array_merge($data, $fieldData);
}

if ($this->serializer instanceof CallDefinitionInterface) {
/** @var string $columnName */
$columnName = '';
/** @var string $columnName */
$columnName = '';

if ($this->column instanceof Column) {
$columnName = $this->column->getName();
}
if ($this->column instanceof Column) {
$columnName = $this->column->getName();
}

if ($this->serializer instanceof CallDefinitionInterface) {
$data[$columnName] = $this->serializer->execute(
$context,
array_merge($data, ['' => $valueFromEntityField])
Expand All @@ -337,6 +346,12 @@ public function revertValue(
$connection->getDatabasePlatform()
);
}

} elseif (is_a($this->className, BackedEnum::class, true)) {
$data[$columnName] = $valueFromEntityField->value;

} elseif (is_a($this->className, UnitEnum::class, true)) {
$data[$columnName] = $valueFromEntityField->name;
}

$context->popFromObjectHydrationStack();
Expand Down Expand Up @@ -367,6 +382,12 @@ public function wakeUpMapping(ContainerInterface $container): void
if ($this->serializer instanceof CallDefinitionInterface) {
$this->serializer->wakeUpCall($container);
}

foreach ($this->fieldMappings as $fieldName => $fieldMapping) {
/** @var MappingInterface $fieldMapping */

$fieldMapping->wakeUpMapping($container);
}
}

}

0 comments on commit 0bd4630

Please sign in to comment.