ModelsToArrayTransformer.php 3.2 KB

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