ModelToIdPropertyTransformer.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. protected $modelManager;
  22. protected $className;
  23. protected $property;
  24. protected $multiple;
  25. protected $toStringCallback;
  26. /**
  27. * @param ModelManagerInterface $modelManager
  28. * @param string $className
  29. * @param string $property
  30. * @param bool $multiple
  31. * @param null $toStringCallback
  32. */
  33. public function __construct(ModelManagerInterface $modelManager, $className, $property, $multiple = false, $toStringCallback = null)
  34. {
  35. $this->modelManager = $modelManager;
  36. $this->className = $className;
  37. $this->property = $property;
  38. $this->multiple = $multiple;
  39. $this->toStringCallback = $toStringCallback;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function reverseTransform($value)
  45. {
  46. $collection = $this->modelManager->getModelCollectionInstance($this->className);
  47. if (empty($value)) {
  48. if ($this->multiple) {
  49. return $collection;
  50. }
  51. return;
  52. }
  53. if (!$this->multiple) {
  54. return $this->modelManager->find($this->className, $value);
  55. }
  56. if (!is_array($value)) {
  57. throw new \UnexpectedValueException(sprintf('Value should be array, %s given.', gettype($value)));
  58. }
  59. foreach ($value as $key => $id) {
  60. if ($key === '_labels') {
  61. continue;
  62. }
  63. $collection->add($this->modelManager->find($this->className, $id));
  64. }
  65. return $collection;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function transform($entityOrCollection)
  71. {
  72. $result = array();
  73. if (!$entityOrCollection) {
  74. return $result;
  75. }
  76. if ($this->multiple) {
  77. if (substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
  78. 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.');
  79. } elseif ($entityOrCollection instanceof \ArrayAccess) {
  80. $collection = $entityOrCollection;
  81. } else {
  82. 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.');
  83. }
  84. } else {
  85. if (substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
  86. $collection = array($entityOrCollection);
  87. } elseif ($entityOrCollection instanceof \ArrayAccess) {
  88. 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.');
  89. } else {
  90. $collection = array($entityOrCollection);
  91. }
  92. }
  93. if (empty($this->property)) {
  94. throw new \RuntimeException('Please define "property" parameter.');
  95. }
  96. foreach ($collection as $entity) {
  97. $id = current($this->modelManager->getIdentifierValues($entity));
  98. if ($this->toStringCallback !== null) {
  99. if (!is_callable($this->toStringCallback)) {
  100. throw new \RuntimeException('Callback in "to_string_callback" option doesn`t contain callable function.');
  101. }
  102. $label = call_user_func($this->toStringCallback, $entity, $this->property);
  103. } else {
  104. try {
  105. $label = (string) $entity;
  106. } catch (\Exception $e) {
  107. throw new \RuntimeException(sprintf("Unable to convert the entity %s to String, entity must have a '__toString()' method defined", ClassUtils::getClass($entity)), 0, $e);
  108. }
  109. }
  110. $result[] = $id;
  111. $result['_labels'][] = $label;
  112. }
  113. return $result;
  114. }
  115. }