ModelsToArrayTransformer.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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 Symfony\Component\Form\Exception\UnexpectedTypeException;
  12. use Symfony\Component\Form\Exception\TransformationFailedException;
  13. use Symfony\Component\Form\DataTransformerInterface;
  14. use Sonata\AdminBundle\Model\ModelManagerInterface;
  15. use Sonata\AdminBundle\Form\ChoiceList\ModelChoiceList;
  16. class ModelsToArrayTransformer implements DataTransformerInterface
  17. {
  18. protected $choiceList;
  19. public function __construct(ModelChoiceList $choiceList)
  20. {
  21. $this->choiceList = $choiceList;
  22. }
  23. /**
  24. * Transforms entities into choice keys
  25. *
  26. * @param Collection|object $collection A collection of entities, a single entity or
  27. * NULL
  28. * @return mixed An array of choice keys, a single key or NULL
  29. */
  30. public function transform($collection)
  31. {
  32. if (null === $collection) {
  33. return array();
  34. }
  35. $array = array();
  36. if (count($this->choiceList->getIdentifier()) > 1) {
  37. // load all choices
  38. $availableEntities = $this->choiceList->getEntities();
  39. foreach ($collection as $entity) {
  40. // identify choices by their collection key
  41. $key = array_search($entity, $availableEntities);
  42. $array[] = $key;
  43. }
  44. } else {
  45. foreach ($collection as $entity) {
  46. $array[] = current($this->choiceList->getIdentifierValues($entity));
  47. }
  48. }
  49. return $array;
  50. }
  51. /**
  52. * Transforms choice keys into entities
  53. *
  54. * @param mixed $keys An array of keys, a single key or NULL
  55. * @return Collection|object A collection of entities, a single entity
  56. * or NULL
  57. */
  58. public function reverseTransform($keys)
  59. {
  60. $collection = $this->choiceList->getModelManager()->getModelCollectionInstance(
  61. $this->choiceList->getClass()
  62. );
  63. if (!$collection instanceof \ArrayAccess) {
  64. throw new UnexpectedTypeException($collection, '\ArrayAccess');
  65. }
  66. if ('' === $keys || null === $keys) {
  67. return $collection;
  68. }
  69. if (!is_array($keys)) {
  70. throw new UnexpectedTypeException($keys, 'array');
  71. }
  72. $notFound = array();
  73. // optimize this into a SELECT WHERE IN query
  74. foreach ($keys as $key) {
  75. if ($entity = $this->choiceList->getEntity($key)) {
  76. $collection[] = $entity;
  77. } else {
  78. $notFound[] = $key;
  79. }
  80. }
  81. if (count($notFound) > 0) {
  82. throw new TransformationFailedException(sprintf('The entities with keys "%s" could not be found', implode('", "', $notFound)));
  83. }
  84. return $collection;
  85. }
  86. }