ListMapper.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\Datagrid;
  12. use Sonata\AdminBundle\Admin\AdminInterface;
  13. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  14. use Sonata\AdminBundle\Admin\FieldDescriptionCollection;
  15. use Sonata\AdminBundle\Builder\ListBuilderInterface;
  16. /**
  17. * This class is used to simulate the Form API
  18. *
  19. */
  20. class ListMapper
  21. {
  22. protected $listBuilder;
  23. protected $list;
  24. protected $admin;
  25. /**
  26. * @param ListBuilderInterface $listBuilder
  27. * @param FieldDescriptionCollection $list
  28. * @param AdminInterface $admin
  29. */
  30. public function __construct(ListBuilderInterface $listBuilder, FieldDescriptionCollection $list, AdminInterface $admin)
  31. {
  32. $this->listBuilder = $listBuilder;
  33. $this->list = $list;
  34. $this->admin = $admin;
  35. }
  36. /**
  37. * @param string $name
  38. * @param null $type
  39. * @param array $fieldDescriptionOptions
  40. *
  41. * @return ListMapper
  42. */
  43. public function addIdentifier($name, $type = null, array $fieldDescriptionOptions = array())
  44. {
  45. $fieldDescriptionOptions['identifier'] = true;
  46. if (!isset($fieldDescriptionOptions['route']['name'])) {
  47. $fieldDescriptionOptions['route']['name'] = 'edit';
  48. }
  49. if (!isset($fieldDescriptionOptions['route']['parameters'])) {
  50. $fieldDescriptionOptions['route']['parameters'] = array();
  51. }
  52. return $this->add($name, $type, $fieldDescriptionOptions);
  53. }
  54. /**
  55. * @throws \RuntimeException
  56. *
  57. * @param mixed $name
  58. * @param mixed $type
  59. * @param array $fieldDescriptionOptions
  60. *
  61. * @return ListMapper
  62. */
  63. public function add($name, $type = null, array $fieldDescriptionOptions = array())
  64. {
  65. if ($name instanceof FieldDescriptionInterface) {
  66. $fieldDescription = $name;
  67. $fieldDescription->mergeOptions($fieldDescriptionOptions);
  68. } elseif (is_string($name) && !$this->admin->hasListFieldDescription($name)) {
  69. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  70. $this->admin->getClass(),
  71. $name,
  72. $fieldDescriptionOptions
  73. );
  74. } else {
  75. throw new \RuntimeException('Unknown or duplicate field name in list mapper. Field name should be either of FieldDescriptionInterface interface or string. Names should be unique.');
  76. }
  77. if (!$fieldDescription->getLabel()) {
  78. $fieldDescription->setOption('label', $this->admin->getLabelTranslatorStrategy()->getLabel($fieldDescription->getName(), 'list', 'label'));
  79. }
  80. // add the field with the FormBuilder
  81. $this->listBuilder->addField($this->list, $type, $fieldDescription, $this->admin);
  82. return $this;
  83. }
  84. /**
  85. * @param string $name
  86. *
  87. * @return FieldDescriptionInterface
  88. */
  89. public function get($name)
  90. {
  91. return $this->list->get($name);
  92. }
  93. /**
  94. * @param string $key
  95. *
  96. * @return bool
  97. */
  98. public function has($key)
  99. {
  100. return $this->list->has($key);
  101. }
  102. /**
  103. * @param string $key
  104. *
  105. * @return ListMapper
  106. */
  107. public function remove($key)
  108. {
  109. $this->admin->removeListFieldDescription($key);
  110. $this->list->remove($key);
  111. return $this;
  112. }
  113. /**
  114. * @param array $keys field names
  115. *
  116. * @return ListMapper
  117. */
  118. public function reorder(array $keys)
  119. {
  120. $this->list->reorder($keys);
  121. return $this;
  122. }
  123. }