ModelToIdPropertyTransformer.php 4.8 KB

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