create generic data transformer with get an object by id, and add test

for CalendarType
This commit is contained in:
2022-05-20 19:23:47 +02:00
parent b6e0379583
commit 67fad5d764
11 changed files with 422 additions and 159 deletions

View File

@@ -14,6 +14,7 @@ namespace Chill\MainBundle\Form\DataTransformer;
use Closure;
use Doctrine\Persistence\ObjectRepository;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use function call_user_func;
/**
@@ -59,7 +60,13 @@ class IdToEntityDataTransformer implements DataTransformerInterface
return null;
}
return $this->repository->findOneBy(['id' => (int) $value]);
$object = $this->repository->findOneBy(['id' => (int) $value]);
if (null === $object) {
throw new TransformationFailedException('could not find any object by object id');
}
return $object;
}
/**
@@ -71,12 +78,22 @@ class IdToEntityDataTransformer implements DataTransformerInterface
$ids = [];
foreach ($value as $v) {
$ids[] = call_user_func($this->getId, $v);
$ids[] = $id = call_user_func($this->getId, $v);
if (null === $id) {
throw new TransformationFailedException('id is null');
}
}
return implode(',', $ids);
}
return call_user_func($this->getId, $value);
$id = call_user_func($this->getId, $value);
if (null === $id) {
throw new TransformationFailedException('id is null');
}
return (string) $id;
}
}