src/Eccube/Form/DataTransformer/EntityToIdTransformer.php line 51

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Eccube\Form\DataTransformer;
  13. use Doctrine\Persistence\ObjectManager;
  14. use Symfony\Component\Form\DataTransformerInterface;
  15. use Symfony\Component\Form\Exception\TransformationFailedException;
  16. class EntityToIdTransformer implements DataTransformerInterface
  17. {
  18.     /**
  19.      * @var ObjectManager
  20.      */
  21.     private $om;
  22.     /**
  23.      * @var string
  24.      */
  25.     private $className;
  26.     /**
  27.      * @param ObjectManager $om
  28.      * @param string $className
  29.      */
  30.     public function __construct(ObjectManager $om$className)
  31.     {
  32.         $this->om $om;
  33.         $this->className $className;
  34.     }
  35.     public function transform($entity)
  36.     {
  37.         if (null === $entity) {
  38.             return '';
  39.         }
  40.         return $entity->getId();
  41.     }
  42.     public function reverseTransform($id)
  43.     {
  44.         if ('' === $id || null === $id) {
  45.             return null;
  46.         }
  47.         $entity $this->om
  48.             ->getRepository($this->className)
  49.             ->find($id)
  50.         ;
  51.         if (null === $entity) {
  52.             throw new TransformationFailedException();
  53.         }
  54.         return $entity;
  55.     }
  56. }