ModelToIdPropertyTransformer.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 RuntimeException;
  13. use Sonata\AdminBundle\Model\ModelManagerInterface;
  14. use Symfony\Component\Form\DataTransformerInterface;
  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) || empty($value['identifiers'])) {
  47. if (!$this->multiple) {
  48. return;
  49. } else {
  50. return $collection;
  51. }
  52. }
  53. if (!$this->multiple) {
  54. return $this->modelManager->find($this->className, current($value['identifiers']));
  55. }
  56. foreach ($value['identifiers'] as $id) {
  57. $collection->add($this->modelManager->find($this->className, $id));
  58. }
  59. return $collection;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function transform($entityOrCollection)
  65. {
  66. $result = array('identifiers' => array(), 'labels' => array());
  67. if (!$entityOrCollection) {
  68. return $result;
  69. }
  70. if ($this->multiple) {
  71. $isArray = is_array($entityOrCollection);
  72. if (!$isArray && substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
  73. 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.');
  74. } elseif ($isArray || ($entityOrCollection instanceof \ArrayAccess)) {
  75. $collection = $entityOrCollection;
  76. } else {
  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. }
  79. } else {
  80. if (substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
  81. $collection = array($entityOrCollection);
  82. } elseif ($entityOrCollection instanceof \ArrayAccess) {
  83. 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.');
  84. } else {
  85. $collection = array($entityOrCollection);
  86. }
  87. }
  88. if (empty($this->property)) {
  89. throw new RuntimeException('Please define "property" parameter.');
  90. }
  91. foreach ($collection as $entity) {
  92. $id = current($this->modelManager->getIdentifierValues($entity));
  93. if ($this->toStringCallback !== null) {
  94. if (!is_callable($this->toStringCallback)) {
  95. throw new RuntimeException('Callback in "to_string_callback" option doesn`t contain callable function.');
  96. }
  97. $label = call_user_func($this->toStringCallback, $entity, $this->property);
  98. } else {
  99. try {
  100. $label = (string) $entity;
  101. } catch (\Exception $e) {
  102. throw new RuntimeException(sprintf("Unable to convert the entity %s to String, entity must have a '__toString()' method defined", ClassUtils::getClass($entity)), 0, $e);
  103. }
  104. }
  105. $result['identifiers'][] = $id;
  106. $result['labels'][] = $label;
  107. }
  108. return $result;
  109. }
  110. }