FormHandle.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * PHP表单生成器
  4. *
  5. * @package FormBuilder
  6. * @author xaboy <xaboy2005@qq.com>
  7. * @version 2.0
  8. * @license MIT
  9. * @link https://github.com/xaboy/form-builder
  10. * @document http://php.form-create.com
  11. */
  12. namespace FormBuilder;
  13. use FormBuilder\Annotation\AnnotationReader;
  14. use FormBuilder\Contract\ConfigInterface;
  15. use FormBuilder\Contract\FormHandleInterface;
  16. /**
  17. * 表单生成类
  18. *
  19. * Class FormHandle
  20. * @package FormBuilder
  21. */
  22. abstract class FormHandle implements FormHandleInterface
  23. {
  24. protected $action = '';
  25. protected $method = 'POST';
  26. protected $title;
  27. protected $formContentType;
  28. protected $headers = [];
  29. protected $fieldTitles = [];
  30. protected $except = [];
  31. protected $scene;
  32. /**
  33. * 表单 UI
  34. *
  35. * @return mixed
  36. */
  37. abstract public function ui();
  38. final public function getExcept()
  39. {
  40. return $this->except;
  41. }
  42. /**
  43. * 获取表单数据
  44. * @return array
  45. */
  46. protected function getFormData()
  47. {
  48. return [];
  49. }
  50. public function scene($scene = null)
  51. {
  52. if (!is_null($scene)) $this->scene = $scene;
  53. return $this->scene;
  54. }
  55. /**
  56. * 获取表单配置
  57. *
  58. * @return mixed|array|ConfigInterface
  59. */
  60. protected function getFormConfig()
  61. {
  62. return;
  63. }
  64. public function getFieldTitle($field)
  65. {
  66. return isset($this->fieldTitles[$field]) ? $this->fieldTitles[$field] : null;
  67. }
  68. /**
  69. * 获取表单组件
  70. *
  71. * @return array
  72. * @throws \ReflectionException
  73. */
  74. protected function getFormRule()
  75. {
  76. $render = new AnnotationReader($this);
  77. return $render->render();
  78. }
  79. /**
  80. * 创建表单
  81. *
  82. * @return Form
  83. * @throws \ReflectionException
  84. */
  85. protected function createForm()
  86. {
  87. $ui = lcfirst($this->ui());
  88. return call_user_func_array(['FormBuilder\\Form', $ui], $this->getParams());
  89. }
  90. /**
  91. * @return array
  92. * @throws \ReflectionException
  93. */
  94. protected function getParams()
  95. {
  96. $params = [$this->action, $this->getFormRule()];
  97. $config = $this->getFormConfig();
  98. if (is_array($config) || $config instanceof ConfigInterface)
  99. $params[] = $config;
  100. return $params;
  101. }
  102. /**
  103. * 获取表单
  104. *
  105. * @return Form
  106. * @throws \ReflectionException
  107. */
  108. public function form()
  109. {
  110. if ($this->scene && method_exists($this, $this->scene . 'Scene'))
  111. $this->{$this->scene . 'Scene'}();
  112. $form = $this->createForm()->setMethod($this->method);
  113. if (!is_null($this->title)) $form->setTitle($this->title)->headers($this->headers);
  114. $formData = $this->getFormData();
  115. if (is_array($formData)) $form->formData($formData);
  116. if ($this->formContentType) $form->setFormContentType($this->formContentType);
  117. $config = $this->getFormConfig();
  118. if ($config) $form->setConfig($config);
  119. return $form;
  120. }
  121. /**
  122. * @return string
  123. * @throws \ReflectionException
  124. */
  125. public function view()
  126. {
  127. return $this->form()->view();
  128. }
  129. }