ModelsToArrayTransformer.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Form\DataTransformer;
  11. use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList;
  12. use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceLoader;
  13. use Sonata\AdminBundle\Model\ModelManagerInterface;
  14. use Symfony\Component\Form\ChoiceList\LazyChoiceList;
  15. use Symfony\Component\Form\DataTransformerInterface;
  16. use Symfony\Component\Form\Exception\RuntimeException;
  17. use Symfony\Component\Form\Exception\TransformationFailedException;
  18. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  19. /**
  20. * Class ModelsToArrayTransformer.
  21. *
  22. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  23. */
  24. class ModelsToArrayTransformer implements DataTransformerInterface
  25. {
  26. /**
  27. * @var ModelManagerInterface
  28. */
  29. protected $modelManager;
  30. /**
  31. * @var string
  32. */
  33. protected $class;
  34. /**
  35. * @var ModelChoiceList
  36. */
  37. protected $choiceList;
  38. /**
  39. * ModelsToArrayTransformer constructor.
  40. *
  41. * @param ModelChoiceList|LazyChoiceList|ModelChoiceLoader $choiceList
  42. * @param ModelManagerInterface $modelManager
  43. * @param $class
  44. *
  45. * @throws RuntimeException
  46. */
  47. public function __construct($choiceList, ModelManagerInterface $modelManager, $class)
  48. {
  49. if (!$choiceList instanceof ModelChoiceList
  50. && !$choiceList instanceof ModelChoiceLoader
  51. && !$choiceList instanceof LazyChoiceList) {
  52. throw new RuntimeException('First param passed to ModelsToArrayTransformer should be instance of
  53. ModelChoiceLoader or ModelChoiceList or LazyChoiceList');
  54. }
  55. $this->choiceList = $choiceList;
  56. $this->modelManager = $modelManager;
  57. $this->class = $class;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function transform($collection)
  63. {
  64. if (null === $collection) {
  65. return array();
  66. }
  67. $array = array();
  68. foreach ($collection as $key => $entity) {
  69. $id = implode('~', $this->getIdentifierValues($entity));
  70. $array[] = $id;
  71. }
  72. return $array;
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function reverseTransform($keys)
  78. {
  79. if (!is_array($keys)) {
  80. throw new UnexpectedTypeException($keys, 'array');
  81. }
  82. $collection = $this->modelManager->getModelCollectionInstance($this->class);
  83. $notFound = array();
  84. // optimize this into a SELECT WHERE IN query
  85. foreach ($keys as $key) {
  86. if ($entity = $this->modelManager->find($this->class, $key)) {
  87. $collection[] = $entity;
  88. } else {
  89. $notFound[] = $key;
  90. }
  91. }
  92. if (count($notFound) > 0) {
  93. throw new TransformationFailedException(sprintf('The entities with keys "%s" could not be found', implode('", "', $notFound)));
  94. }
  95. return $collection;
  96. }
  97. /**
  98. * @param object $entity
  99. *
  100. * @return array
  101. */
  102. private function getIdentifierValues($entity)
  103. {
  104. try {
  105. return $this->modelManager->getIdentifierValues($entity);
  106. } catch (\Exception $e) {
  107. throw new \InvalidArgumentException(sprintf('Unable to retrieve the identifier values for entity %s', ClassUtils::getClass($entity)), 0, $e);
  108. }
  109. }
  110. }