ModelsToArrayTransformer.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. * @throws RuntimeException
  45. */
  46. public function __construct($choiceList, ModelManagerInterface $modelManager, $class)
  47. {
  48. if (!$choiceList instanceof ModelChoiceList
  49. && !$choiceList instanceof ModelChoiceLoader
  50. && !$choiceList instanceof LazyChoiceList) {
  51. throw new RuntimeException('First param passed to ModelsToArrayTransformer should be instance of
  52. ModelChoiceLoader or ModelChoiceList or LazyChoiceList');
  53. }
  54. $this->choiceList = $choiceList;
  55. $this->modelManager = $modelManager;
  56. $this->class = $class;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public function transform($collection)
  62. {
  63. if (null === $collection) {
  64. return array();
  65. }
  66. $array = array();
  67. foreach ($collection as $key => $entity) {
  68. $id = implode('~', $this->getIdentifierValues($entity));
  69. $array[] = $id;
  70. }
  71. return $array;
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function reverseTransform($keys)
  77. {
  78. if (!is_array($keys)) {
  79. throw new UnexpectedTypeException($keys, 'array');
  80. }
  81. $collection = $this->modelManager->getModelCollectionInstance($this->class);
  82. $notFound = array();
  83. // optimize this into a SELECT WHERE IN query
  84. foreach ($keys as $key) {
  85. if ($entity = $this->modelManager->find($this->class, $key)) {
  86. $collection[] = $entity;
  87. } else {
  88. $notFound[] = $key;
  89. }
  90. }
  91. if (count($notFound) > 0) {
  92. throw new TransformationFailedException(sprintf('The entities with keys "%s" could not be found', implode('", "', $notFound)));
  93. }
  94. return $collection;
  95. }
  96. /**
  97. * @param object $entity
  98. *
  99. * @return array
  100. */
  101. private function getIdentifierValues($entity)
  102. {
  103. try {
  104. return $this->modelManager->getIdentifierValues($entity);
  105. } catch (\Exception $e) {
  106. throw new \InvalidArgumentException(sprintf('Unable to retrieve the identifier values for entity %s', ClassUtils::getClass($entity)), 0, $e);
  107. }
  108. }
  109. }