src/Infrastructure/CommonBundle/Form/DataTransformer/ObjectToIdTransformer.php line 37

Open in your IDE?
  1. <?php
  2. namespace Whater\Infrastructure\CommonBundle\Form\DataTransformer;
  3. use Doctrine\Persistence\ObjectManager;
  4. use Doctrine\Persistence\ManagerRegistry;
  5. use Doctrine\Common\Persistence\ObjectRepository;
  6. use Doctrine\ORM\EntityManager;
  7. use Doctrine\ORM\EntityRepository;
  8. use Symfony\Component\Form\DataTransformerInterface;
  9. use Symfony\Component\Form\Exception\InvalidConfigurationException;
  10. use Symfony\Component\Form\Exception\TransformationFailedException;
  11. /**
  12. * Class ObjectToIdTransformer
  13. *
  14. * @package Whater\Infrastructure\CommonBundle\Form\DataTransformer
  15. */
  16. class ObjectToIdTransformer implements DataTransformerInterface
  17. {
  18. /** @var string */
  19. protected $class;
  20. /** @var string */
  21. protected $property;
  22. /** @var EntityManager */
  23. protected $em;
  24. /** @var EntityRepository */
  25. protected $repository;
  26. /**
  27. * @param ManagerRegistry $registry
  28. * @param string $class
  29. * @param string $property
  30. */
  31. public function __construct(ManagerRegistry $registry, $em, $class, $property)
  32. {
  33. $this->class = $class;
  34. $this->property = $property;
  35. $this->em = $this->getObjectManager($registry, $em);
  36. $this->repository = $this->getObjectRepository($this->em, $this->class);
  37. }
  38. /**
  39. * @param mixed $entity
  40. *
  41. * @return mixed|null
  42. */
  43. public function transform($entity)
  44. {
  45. if (null === $entity) {
  46. return null;
  47. }
  48. $className = $this->repository->getClassName();
  49. if (!$entity instanceof $className) {
  50. throw new TransformationFailedException(sprintf('Object must be instance of %s, instance of %s has given.', $className, get_class($entity)));
  51. }
  52. $methodName = $this->property;
  53. if (!method_exists($entity, $methodName)) {
  54. $methodName = 'get' . ucfirst($this->property);
  55. if (!method_exists($entity, $methodName)) {
  56. throw new InvalidConfigurationException(sprintf('There is no getter for property "%s" in class "%s".', $this->property, $this->class));
  57. }
  58. }
  59. return $entity->{$methodName}();
  60. }
  61. /**
  62. * @param mixed $id
  63. *
  64. * @return mixed|null|object
  65. */
  66. public function reverseTransform($id)
  67. {
  68. if (!$id) {
  69. return null;
  70. }
  71. $property = $this->property;
  72. if(strcmp($property, "id") == 0){
  73. $property = "uuid";
  74. }
  75. $entity = $this->repository->findOneBy([$property => $id]);
  76. if (null === $entity) {
  77. throw new TransformationFailedException(sprintf('Can\'t find entity of class "%s" with property "%s" = "%s".', $this->class, $this->property, $id));
  78. }
  79. return $entity;
  80. }
  81. /**
  82. * @param ManagerRegistry $registry
  83. * @param ObjectManager|string $omName
  84. *
  85. * @return ObjectManager
  86. */
  87. private function getObjectManager(ManagerRegistry $registry, $omName)
  88. {
  89. if ($omName instanceof ObjectManager) {
  90. return $omName;
  91. }
  92. $omName = (string)$omName;
  93. if ($om = $registry->getManager($omName)) {
  94. return $om;
  95. }
  96. throw new InvalidConfigurationException(sprintf('Doctrine Manager named "%s" does not exist.', $omName));
  97. }
  98. /**
  99. * @param ObjectManager $om
  100. * @param string $class
  101. *
  102. * @return ObjectRepository
  103. */
  104. private function getObjectRepository(ObjectManager $om, $class)
  105. {
  106. if ($repo = $om->getRepository($class)) {
  107. return $repo;
  108. }
  109. throw new InvalidConfigurationException(sprintf('Repository for class "%s" does not exist.', $class));
  110. }
  111. }