FormMapper.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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;
  12. use Sonata\AdminBundle\Builder\FormContractorInterface;
  13. use Sonata\AdminBundle\Admin\AdminInterface;
  14. use Sonata\AdminBundle\Admin\FieldDescriptionInterface;
  15. use Symfony\Component\Form\FormBuilder;
  16. /**
  17. * This class is use to simulate the Form API
  18. *
  19. */
  20. class FormMapper
  21. {
  22. protected $formBuilder;
  23. protected $formContractor;
  24. protected $admin;
  25. public function __construct(FormContractorInterface $formContractor, FormBuilder $formBuilder, AdminInterface $admin)
  26. {
  27. $this->formBuilder = $formBuilder;
  28. $this->formContractor = $formContractor;
  29. $this->admin = $admin;
  30. }
  31. /**
  32. * The method add a new field to the provided FormBuilder, there are 4 ways to add new field :
  33. *
  34. * - if $name is a string with no related FieldDescription, then the form will use the FormFactory
  35. * to instantiate a new Field
  36. * - if $name is a FormDescription, the method uses information defined in the FormDescription to
  37. * instantiate a new Field
  38. * - if $name is a FormBuilder, then a FieldDescriptionInterface is created, the FormBuilder is added to
  39. * the form
  40. * - if $name is a string with a related FieldDescription, then the method uses information defined in the
  41. * FormDescription to instantiate a new Field
  42. *
  43. * @throws \RuntimeException
  44. * @param string $name
  45. * @param array $fieldOptions
  46. * @param array $fieldDescriptionOptions
  47. * @return \Symfony\Component\Form\FieldInterface|void
  48. */
  49. public function add($name, array $fieldOptions = array(), array $fieldDescriptionOptions = array())
  50. {
  51. $fieldType = false;
  52. if ($name instanceof FieldDescriptionInterface) {
  53. $fieldDescription = $name;
  54. $fieldDescription->mergeOptions($fieldDescriptionOptions);
  55. } else if ($name instanceof FormBuilder) {
  56. $fieldType = $name;
  57. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  58. $this->admin->getClass(),
  59. $fieldType->getName(),
  60. $fieldDescriptionOptions
  61. );
  62. $this->formContractor->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
  63. $this->admin->addFormFieldDescription($fieldType->getName(), $fieldDescription);
  64. } else if (is_string($name) && !$this->admin->hasFormFieldDescription($name)) {
  65. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  66. $this->admin->getClass(),
  67. $name,
  68. $fieldDescriptionOptions
  69. );
  70. // set default configuration
  71. $this->formContractor->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
  72. // add the FieldDescription
  73. $this->admin->addFormFieldDescription($name, $fieldDescription);
  74. } else if (is_string($name) && $this->admin->hasFormFieldDescription($name)) {
  75. $fieldDescription = $this->admin->getFormFieldDescription($name);
  76. // update configuration
  77. $this->formContractor->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
  78. } else {
  79. throw new \RuntimeException('invalid state');
  80. }
  81. $fieldDescription->mergeOption('form_field_options', $fieldOptions);
  82. // nothing to build as a Field is provided
  83. if ($fieldType) {
  84. $this->formBuilder->add($fieldType);
  85. }
  86. // add the field with the FormBuilder
  87. $this->formContractor->addField(
  88. $this->formBuilder,
  89. $fieldDescription
  90. );
  91. return $this;
  92. }
  93. /**
  94. * @param string $name
  95. * @param string $type
  96. * @param array $options
  97. * @param array $fieldDescriptionOptions
  98. * @return \Symfony\Component\Form\FormInterface
  99. */
  100. public function addType($name, $type = null, array $options = array(), array $fieldDescriptionOptions = array())
  101. {
  102. if (!isset($fieldDescriptionOptions['type']) && is_string($type)) {
  103. $fieldDescriptionOptions['type'] = $type;
  104. }
  105. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  106. $this->admin->getClass(),
  107. $name instanceof FormBuilder ? $name->getName() : $name,
  108. $fieldDescriptionOptions
  109. );
  110. $this->formContractor->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
  111. $this->admin->addFormFieldDescription($name instanceof FormBuilder ? $name->getName() : $name, $fieldDescription);
  112. if ($name instanceof FormBuilder) {
  113. $this->formBuilder->add($name);
  114. } else {
  115. $this->formBuilder->add($name, $type, $options);
  116. }
  117. return $this;
  118. }
  119. /**
  120. * @param string $name
  121. * @return \Symfony\Component\Form\FieldInterface
  122. */
  123. public function get($name)
  124. {
  125. return $this->formBuilder->get($name);
  126. }
  127. /**
  128. * @param string $key
  129. * @return boolean
  130. */
  131. public function has($key)
  132. {
  133. return $this->formBuilder->has($key);
  134. }
  135. /**
  136. * @param string $key
  137. * @return void
  138. */
  139. public function remove($key)
  140. {
  141. $this->admin->removeFormFieldDescription($key);
  142. $this->formBuilder->remove($key);
  143. }
  144. /**
  145. * @return \Symfony\Component\Form\FormBuilder
  146. */
  147. public function getFormBuilder()
  148. {
  149. return $this->formBuilder;
  150. }
  151. /**
  152. * @return \Sonata\AdminBundle\Admin\AdminInterface
  153. */
  154. public function getAdmin()
  155. {
  156. return $this->admin;
  157. }
  158. /**
  159. * @param string $name
  160. * @param mixed $type
  161. * @param array $options
  162. * @return void
  163. */
  164. public function create($name, $type = null, array $options = array())
  165. {
  166. return $this->formBuilder->create($name, $type, $options);
  167. }
  168. public function setHelps(array $helps = array())
  169. {
  170. foreach($helps as $name => $help) {
  171. if ($this->admin->hasFormFieldDescription($name)) {
  172. $this->admin->getFormFieldDescription($name)->setHelp($help);
  173. }
  174. }
  175. return $this;
  176. }
  177. }