LegacyModelsToArrayTransformer.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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\DataTransformerInterface;
  13. use Symfony\Component\Form\Exception\TransformationFailedException;
  14. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  15. /**
  16. * NEXT_MAJOR: remove this class when dropping Symfony < 2.7 support.
  17. *
  18. * This class should be used with Symfony <2.7 only and will be deprecated with 3.0.
  19. *
  20. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  21. */
  22. class LegacyModelsToArrayTransformer implements DataTransformerInterface
  23. {
  24. /**
  25. * @var ModelChoiceList
  26. */
  27. protected $choiceList;
  28. /**
  29. * @param ModelChoiceList $choiceList
  30. */
  31. public function __construct(ModelChoiceList $choiceList)
  32. {
  33. if (interface_exists('Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface')) {
  34. @trigger_error(
  35. 'The '.__CLASS__.' class is deprecated since 3.11, to be removed in 4.0. '.
  36. 'Use Sonata\AdminBundle\Form\DataTransformer\ModelsToArrayTransformer instead.',
  37. E_USER_DEPRECATED
  38. );
  39. }
  40. $this->choiceList = $choiceList;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function transform($collection)
  46. {
  47. if (null === $collection) {
  48. return array();
  49. }
  50. $array = array();
  51. if (count($this->choiceList->getIdentifier()) > 1) {
  52. // load all choices
  53. $availableEntities = $this->choiceList->getEntities();
  54. foreach ($collection as $entity) {
  55. // identify choices by their collection key
  56. $key = array_search($entity, $availableEntities);
  57. $array[] = $key;
  58. }
  59. } else {
  60. foreach ($collection as $entity) {
  61. $array[] = current($this->choiceList->getIdentifierValues($entity));
  62. }
  63. }
  64. return $array;
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function reverseTransform($keys)
  70. {
  71. $collection = $this->choiceList->getModelManager()->getModelCollectionInstance(
  72. $this->choiceList->getClass()
  73. );
  74. if (!$collection instanceof \ArrayAccess) {
  75. throw new UnexpectedTypeException($collection, '\ArrayAccess');
  76. }
  77. if ('' === $keys || null === $keys) {
  78. return $collection;
  79. }
  80. if (!is_array($keys)) {
  81. throw new UnexpectedTypeException($keys, 'array');
  82. }
  83. $notFound = array();
  84. // optimize this into a SELECT WHERE IN query
  85. foreach ($keys as $key) {
  86. if ($entity = $this->choiceList->getEntity($key)) {
  87. $collection[] = $entity;
  88. } else {
  89. $notFound[] = $key;
  90. }
  91. }
  92. if (count($notFound) > 0) {
  93. throw new TransformationFailedException(sprintf('The entities with keys "%s" could not be found', implode('", "', $notFound)));
  94. }
  95. return $collection;
  96. }
  97. }