|
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- <?php
-
- namespace Lc\CaracoleBundle\Form\Product;
-
- use Doctrine\ORM\EntityManagerInterface;
- use Lc\CaracoleBundle\Model\Product\ProductInterface;
- use Symfony\Component\Form\DataTransformerInterface;
- use Symfony\Component\Form\Exception\TransformationFailedException;
-
- class ProductToIdTransformer implements DataTransformerInterface
- {
- private EntityManagerInterface $entityManager;
-
- public function __construct(EntityManagerInterface $entityManager)
- {
- $this->entityManager = $entityManager;
- }
-
- public function transform($product)
- {
- if (null === $product) {
- return '';
- }
-
- return $product->getId();
- }
-
- public function reverseTransform($productId)
- {
- if (!$productId) {
- return;
- }
-
- $product = $this->entityManager->getRepository(
- $this->entityManager->getEntityName(ProductInterface::class)
- )->find($productId);
-
- if (null === $product) {
- throw new TransformationFailedException(
- sprintf(
- 'An issue with number "%s" does not exist!',
- $productId
- )
- );
- }
-
- return $product;
- }
- }
|