ModelsToArrayTransformer.php 3.6 KB

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