FormMapper.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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, array $options = array(), array $fieldDescriptionOptions = array())
  101. {
  102. if (!isset($fieldDescriptionOptions['type'])) {
  103. $fieldDescriptionOptions['type'] = $type;
  104. }
  105. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  106. $this->admin->getClass(),
  107. $name,
  108. $fieldDescriptionOptions
  109. );
  110. $this->formContractor->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
  111. $this->admin->addFormFieldDescription($name, $fieldDescription);
  112. $this->formBuilder->add($name, $type, $options);
  113. return $this;
  114. }
  115. /**
  116. * @param string $name
  117. * @return \Symfony\Component\Form\FieldInterface
  118. */
  119. public function get($name)
  120. {
  121. return $this->formBuilder->get($name);
  122. }
  123. /**
  124. * @param string $key
  125. * @return boolean
  126. */
  127. public function has($key)
  128. {
  129. return $this->formBuilder->has($key);
  130. }
  131. /**
  132. * @param string $key
  133. * @return void
  134. */
  135. public function remove($key)
  136. {
  137. $this->admin->removeFormFieldDescription($key);
  138. $this->formBuilder->remove($key);
  139. }
  140. /**
  141. * @return \Symfony\Component\Form\FormBuilder
  142. */
  143. public function getFormBuilder()
  144. {
  145. return $this->formBuilder;
  146. }
  147. /**
  148. * @return \Sonata\AdminBundle\Admin\AdminInterface
  149. */
  150. public function getAdmin()
  151. {
  152. return $this->admin;
  153. }
  154. }