FormMapper.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. * @param string $name
  33. * @param string $type
  34. * @param array $options
  35. * @param array $fieldDescriptionOptions
  36. * @return \Symfony\Component\Form\FormInterface
  37. */
  38. public function add($name, $type = null, array $options = array(), array $fieldDescriptionOptions = array())
  39. {
  40. if (!isset($fieldDescriptionOptions['type']) && is_string($type)) {
  41. $fieldDescriptionOptions['type'] = $type;
  42. }
  43. $fieldDescription = $this->admin->getModelManager()->getNewFieldDescriptionInstance(
  44. $this->admin->getClass(),
  45. $name instanceof FormBuilder ? $name->getName() : $name,
  46. $fieldDescriptionOptions
  47. );
  48. $this->formContractor->fixFieldDescription($this->admin, $fieldDescription, $fieldDescriptionOptions);
  49. $options = $this->formContractor->getDefaultOptions($type, $fieldDescription, $options);
  50. $this->admin->addFormFieldDescription($name instanceof FormBuilder ? $name->getName() : $name, $fieldDescription);
  51. if ($name instanceof FormBuilder) {
  52. $this->formBuilder->add($name);
  53. } else {
  54. $this->formBuilder->add($name, $type, $options);
  55. }
  56. return $this;
  57. }
  58. /**
  59. * @param string $name
  60. * @return \Symfony\Component\Form\FieldInterface
  61. */
  62. public function get($name)
  63. {
  64. return $this->formBuilder->get($name);
  65. }
  66. /**
  67. * @param string $key
  68. * @return boolean
  69. */
  70. public function has($key)
  71. {
  72. return $this->formBuilder->has($key);
  73. }
  74. /**
  75. * @param string $key
  76. * @return void
  77. */
  78. public function remove($key)
  79. {
  80. $this->admin->removeFormFieldDescription($key);
  81. $this->formBuilder->remove($key);
  82. }
  83. /**
  84. * @return \Symfony\Component\Form\FormBuilder
  85. */
  86. public function getFormBuilder()
  87. {
  88. return $this->formBuilder;
  89. }
  90. /**
  91. * @return \Sonata\AdminBundle\Admin\AdminInterface
  92. */
  93. public function getAdmin()
  94. {
  95. return $this->admin;
  96. }
  97. /**
  98. * @param string $name
  99. * @param mixed $type
  100. * @param array $options
  101. * @return void
  102. */
  103. public function create($name, $type = null, array $options = array())
  104. {
  105. return $this->formBuilder->create($name, $type, $options);
  106. }
  107. public function setHelps(array $helps = array())
  108. {
  109. foreach($helps as $name => $help) {
  110. if ($this->admin->hasFormFieldDescription($name)) {
  111. $this->admin->getFormFieldDescription($name)->setHelp($help);
  112. }
  113. }
  114. return $this;
  115. }
  116. }