FormMapper.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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\BaseApplicationBundle\Form;
  12. use Sonata\BaseApplicationBundle\Builder\FormBuilderInterface;
  13. use Sonata\BaseApplicationBundle\Admin\Admin;
  14. use Sonata\BaseApplicationBundle\Admin\FieldDescription;
  15. use Symfony\Component\Form\Form;
  16. use Symfony\Component\Form\FormInterface;
  17. use Symfony\Component\Form\FormContextInterface;
  18. use Symfony\Component\Validator\ValidatorInterface;
  19. use Symfony\Component\Form\FieldFactory\FieldFactoryInterface;
  20. /**
  21. * This class is use to simulate the Form API
  22. *
  23. */
  24. class FormMapper
  25. {
  26. protected $formBuilder;
  27. protected $form;
  28. protected $admin;
  29. public function __construct(FormBuilderInterface $formBuilder, Form $form, Admin $admin)
  30. {
  31. $this->formBuilder = $formBuilder;
  32. $this->form = $form;
  33. $this->admin = $admin;
  34. }
  35. public function add($name, array $fieldOptions = array(), array $fieldDescriptionOptions = array())
  36. {
  37. $field = false;
  38. if ($name instanceof FieldDescription) {
  39. $fieldDescription = $name;
  40. $fieldDescription->mergeOptions($fieldDescriptionOptions);
  41. } else if ($name instanceof FormInterface) {
  42. $field = $name;
  43. $fieldDescription = new FieldDescription;
  44. $fieldDescription->setOptions($fieldDescriptionOptions);
  45. $fieldDescription->setName($field->getKey());
  46. $this->formBuilder->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
  47. $this->admin->addFormFieldDescription($field->getKey(), $fieldDescription);
  48. } else if (is_string($name) && !$this->admin->hasFormFieldDescription($name)) {
  49. $fieldDescription = new FieldDescription;
  50. $fieldDescription->setOptions($fieldDescriptionOptions);
  51. $fieldDescription->setName($name);
  52. // set default configuration
  53. $this->formBuilder->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
  54. // add the FieldDescription
  55. $this->admin->addFormFieldDescription($name, $fieldDescription);
  56. } else if (is_string($name) && $this->admin->hasFormFieldDescription($name)) {
  57. $fieldDescription = $this->admin->getFormFieldDescription($name);
  58. // update configuration
  59. $this->formBuilder->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
  60. } else {
  61. throw new \RuntimeException('invalid state');
  62. }
  63. $fieldDescription->mergeOption('form_field_options', $fieldOptions);
  64. // nothing to build as a Field is provided
  65. if($field) {
  66. return $this->form->add($field);
  67. }
  68. // add the field with the FormBuilder
  69. return $this->formBuilder->addField(
  70. $this->form,
  71. $fieldDescription
  72. );
  73. }
  74. public function get($name)
  75. {
  76. return $this->form->get($name);
  77. }
  78. public function has($key)
  79. {
  80. return $this->form->has($key);
  81. }
  82. public function remove($key)
  83. {
  84. $this->admin->removeFormFieldDescription($key);
  85. $this->form->remove($key);
  86. }
  87. }