ModelsToArrayTransformer.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 Symfony\Component\Form\ChoiceList\LegacyChoiceListAdapter;
  13. use Symfony\Component\Form\DataTransformerInterface;
  14. use Symfony\Component\Form\Exception\TransformationFailedException;
  15. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  16. /**
  17. * Class ModelsToArrayTransformer.
  18. *
  19. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  20. */
  21. class ModelsToArrayTransformer implements DataTransformerInterface
  22. {
  23. /**
  24. * @var ModelChoiceList
  25. */
  26. protected $choiceList;
  27. /**
  28. * @param ModelChoiceList|LegacyChoiceListAdapter $choiceList
  29. */
  30. public function __construct($choiceList)
  31. {
  32. if ($choiceList instanceof LegacyChoiceListAdapter && $choiceList->getAdaptedList() instanceof ModelChoiceList) {
  33. $this->choiceList = $choiceList->getAdaptedList();
  34. } elseif ($choiceList instanceof ModelChoiceList) {
  35. $this->choiceList = $choiceList;
  36. } else {
  37. new \InvalidArgumentException('Argument 1 passed to '.__CLASS__.'::'.__METHOD__.' must be an instance of Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList, instance of '.get_class($choiceList).' given');
  38. }
  39. }
  40. /**
  41. * {@inheritDoc}
  42. */
  43. public function transform($collection)
  44. {
  45. if (null === $collection) {
  46. return array();
  47. }
  48. $array = array();
  49. if (count($this->choiceList->getIdentifier()) > 1) {
  50. // load all choices
  51. $availableEntities = $this->choiceList->getEntities();
  52. foreach ($collection as $entity) {
  53. // identify choices by their collection key
  54. $key = array_search($entity, $availableEntities);
  55. $array[] = $key;
  56. }
  57. } else {
  58. foreach ($collection as $entity) {
  59. $array[] = current($this->choiceList->getIdentifierValues($entity));
  60. }
  61. }
  62. return $array;
  63. }
  64. /**
  65. * {@inheritDoc}
  66. */
  67. public function reverseTransform($keys)
  68. {
  69. $collection = $this->choiceList->getModelManager()->getModelCollectionInstance(
  70. $this->choiceList->getClass()
  71. );
  72. if (!$collection instanceof \ArrayAccess) {
  73. throw new UnexpectedTypeException($collection, '\ArrayAccess');
  74. }
  75. if ('' === $keys || null === $keys) {
  76. return $collection;
  77. }
  78. if (!is_array($keys)) {
  79. throw new UnexpectedTypeException($keys, 'array');
  80. }
  81. $notFound = array();
  82. // optimize this into a SELECT WHERE IN query
  83. foreach ($keys as $key) {
  84. if ($entity = $this->choiceList->getEntity($key)) {
  85. $collection[] = $entity;
  86. } else {
  87. $notFound[] = $key;
  88. }
  89. }
  90. if (count($notFound) > 0) {
  91. throw new TransformationFailedException(sprintf('The entities with keys "%s" could not be found', implode('", "', $notFound)));
  92. }
  93. return $collection;
  94. }
  95. }