ModelToIdPropertyTransformer.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 Doctrine\Common\Util\ClassUtils;
  12. use Sonata\AdminBundle\Model\ModelManagerInterface;
  13. use Symfony\Component\Form\DataTransformerInterface;
  14. /**
  15. * Transform object to ID and property label.
  16. *
  17. * @author Andrej Hudec <pulzarraider@gmail.com>
  18. */
  19. class ModelToIdPropertyTransformer implements DataTransformerInterface
  20. {
  21. /**
  22. * @var ModelManagerInterface
  23. */
  24. protected $modelManager;
  25. /**
  26. * @var string
  27. */
  28. protected $className;
  29. /**
  30. * @var string
  31. */
  32. protected $property;
  33. /**
  34. * @var bool
  35. */
  36. protected $multiple;
  37. /**
  38. * @var callable|null
  39. */
  40. protected $toStringCallback;
  41. /**
  42. * @param ModelManagerInterface $modelManager
  43. * @param string $className
  44. * @param string $property
  45. * @param bool $multiple
  46. * @param null $toStringCallback
  47. */
  48. public function __construct(ModelManagerInterface $modelManager, $className, $property, $multiple = false, $toStringCallback = null)
  49. {
  50. $this->modelManager = $modelManager;
  51. $this->className = $className;
  52. $this->property = $property;
  53. $this->multiple = $multiple;
  54. $this->toStringCallback = $toStringCallback;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function reverseTransform($value)
  60. {
  61. $collection = $this->modelManager->getModelCollectionInstance($this->className);
  62. if (empty($value)) {
  63. if ($this->multiple) {
  64. return $collection;
  65. }
  66. return;
  67. }
  68. if (!$this->multiple) {
  69. return $this->modelManager->find($this->className, $value);
  70. }
  71. if (!is_array($value)) {
  72. throw new \UnexpectedValueException(sprintf('Value should be array, %s given.', gettype($value)));
  73. }
  74. foreach ($value as $key => $id) {
  75. if ($key === '_labels') {
  76. continue;
  77. }
  78. $collection->add($this->modelManager->find($this->className, $id));
  79. }
  80. return $collection;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function transform($entityOrCollection)
  86. {
  87. $result = array();
  88. if (!$entityOrCollection) {
  89. return $result;
  90. }
  91. if ($this->multiple) {
  92. $isArray = is_array($entityOrCollection);
  93. if (!$isArray && substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
  94. throw new \InvalidArgumentException('A multiple selection must be passed a collection not a single value. Make sure that form option "multiple=false" is set for many-to-one relation and "multiple=true" is set for many-to-many or one-to-many relations.');
  95. } elseif ($isArray || ($entityOrCollection instanceof \ArrayAccess)) {
  96. $collection = $entityOrCollection;
  97. } else {
  98. throw new \InvalidArgumentException('A multiple selection must be passed a collection not a single value. Make sure that form option "multiple=false" is set for many-to-one relation and "multiple=true" is set for many-to-many or one-to-many relations.');
  99. }
  100. } else {
  101. if (substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
  102. $collection = array($entityOrCollection);
  103. } elseif ($entityOrCollection instanceof \ArrayAccess) {
  104. throw new \InvalidArgumentException('A single selection must be passed a single value not a collection. Make sure that form option "multiple=false" is set for many-to-one relation and "multiple=true" is set for many-to-many or one-to-many relations.');
  105. } else {
  106. $collection = array($entityOrCollection);
  107. }
  108. }
  109. if (empty($this->property)) {
  110. throw new \RuntimeException('Please define "property" parameter.');
  111. }
  112. foreach ($collection as $entity) {
  113. $id = current($this->modelManager->getIdentifierValues($entity));
  114. if ($this->toStringCallback !== null) {
  115. if (!is_callable($this->toStringCallback)) {
  116. throw new \RuntimeException('Callback in "to_string_callback" option doesn`t contain callable function.');
  117. }
  118. $label = call_user_func($this->toStringCallback, $entity, $this->property);
  119. } else {
  120. try {
  121. $label = (string) $entity;
  122. } catch (\Exception $e) {
  123. throw new \RuntimeException(sprintf("Unable to convert the entity %s to String, entity must have a '__toString()' method defined", ClassUtils::getClass($entity)), 0, $e);
  124. }
  125. }
  126. $result[] = $id;
  127. $result['_labels'][] = $label;
  128. }
  129. return $result;
  130. }
  131. }