ModelToIdPropertyTransformer.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. use RuntimeException;
  16. /**
  17. * Transform object to ID and property label
  18. *
  19. * @author Andrej Hudec <pulzarraider@gmail.com>
  20. */
  21. class ModelToIdPropertyTransformer implements DataTransformerInterface
  22. {
  23. protected $modelManager;
  24. protected $className;
  25. protected $property;
  26. protected $multiple;
  27. protected $toStringCallback;
  28. /**
  29. * @param ModelManagerInterface $modelManager
  30. * @param string $className
  31. * @param string $property
  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) || empty($value['identifiers'])) {
  48. if (!$this->multiple) {
  49. return null;
  50. } else {
  51. return $collection;
  52. }
  53. }
  54. if (!$this->multiple) {
  55. return $this->modelManager->find($this->className, current($value['identifiers']));
  56. }
  57. foreach ($value['identifiers'] as $id) {
  58. $collection->add($this->modelManager->find($this->className, $id));
  59. }
  60. return $collection;
  61. }
  62. /**
  63. * {@inheritDoc}
  64. */
  65. public function transform($entityOrCollection)
  66. {
  67. $result = array('identifiers' => array(), 'labels' => array());
  68. if (!$entityOrCollection) {
  69. return $result;
  70. }
  71. if (substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
  72. $collection = array($entityOrCollection);
  73. } elseif ($entityOrCollection instanceof \ArrayAccess) {
  74. $collection = $entityOrCollection;
  75. } else {
  76. $collection = array($entityOrCollection);
  77. }
  78. if (empty($this->property)) {
  79. throw new RuntimeException('Please define "property" parameter.');
  80. }
  81. foreach ($collection as $entity) {
  82. $id = current($this->modelManager->getIdentifierValues($entity));
  83. if ($this->toStringCallback !== null) {
  84. if (!is_callable($this->toStringCallback)) {
  85. throw new RuntimeException('Callback in "to_string_callback" option doesn`t contain callable function.');
  86. }
  87. $label = call_user_func($this->toStringCallback, $entity, $this->property);
  88. } else {
  89. try {
  90. $label = (string) $entity;
  91. } catch (\Exception $e) {
  92. throw new RuntimeException(sprintf("Unable to convert the entity %s to String, entity must have a '__toString()' method defined", ClassUtils::getClass($entity)), 0, $e);
  93. }
  94. }
  95. $result['identifiers'][] = $id;
  96. $result['labels'][] = $label;
  97. }
  98. return $result;
  99. }
  100. }