ModelsToArrayTransformer.php 3.3 KB

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