ModelToIdPropertyTransformer.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. $isArray = is_array($entityOrCollection);
  78. if (!$isArray && substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
  79. 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.');
  80. } elseif ($isArray || ($entityOrCollection instanceof \ArrayAccess)) {
  81. $collection = $entityOrCollection;
  82. } else {
  83. 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.');
  84. }
  85. } else {
  86. if (substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
  87. $collection = array($entityOrCollection);
  88. } elseif ($entityOrCollection instanceof \ArrayAccess) {
  89. 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.');
  90. } else {
  91. $collection = array($entityOrCollection);
  92. }
  93. }
  94. if (empty($this->property)) {
  95. throw new \RuntimeException('Please define "property" parameter.');
  96. }
  97. foreach ($collection as $entity) {
  98. $id = current($this->modelManager->getIdentifierValues($entity));
  99. if ($this->toStringCallback !== null) {
  100. if (!is_callable($this->toStringCallback)) {
  101. throw new \RuntimeException('Callback in "to_string_callback" option doesn`t contain callable function.');
  102. }
  103. $label = call_user_func($this->toStringCallback, $entity, $this->property);
  104. } else {
  105. try {
  106. $label = (string) $entity;
  107. } catch (\Exception $e) {
  108. throw new \RuntimeException(sprintf("Unable to convert the entity %s to String, entity must have a '__toString()' method defined", ClassUtils::getClass($entity)), 0, $e);
  109. }
  110. }
  111. $result[] = $id;
  112. $result['_labels'][] = $label;
  113. }
  114. return $result;
  115. }
  116. }