Form.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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\Contract\BootstrapInterface;
  14. use FormBuilder\Contract\ConfigInterface;
  15. use FormBuilder\Exception\FormBuilderException;
  16. use FormBuilder\UI\Iview\Bootstrap as IViewBootstrap;
  17. use FormBuilder\UI\Elm\Bootstrap as ElmBootstrap;
  18. class Form
  19. {
  20. protected $headers = [];
  21. protected $formContentType = 'application/x-www-form-urlencoded';
  22. /**
  23. * @var BootstrapInterface
  24. */
  25. protected $ui;
  26. /**
  27. * @var array|ConfigInterface
  28. */
  29. protected $config;
  30. protected $action;
  31. protected $method = 'POST';
  32. protected $title = 'FormBuilder';
  33. protected $rule;
  34. protected $formData = [];
  35. protected $dependScript = [
  36. '<script src="https://unpkg.com/jquery@3.3.1/dist/jquery.min.js"></script>',
  37. '<script src="https://unpkg.com/vue@2.5.13/dist/vue.min.js"></script>',
  38. '<script src="https://unpkg.com/@form-create/data@1.0.0/dist/province_city.js"></script>',
  39. '<script src="https://unpkg.com/@form-create/data@1.0.0/dist/province_city_area.js"></script>'
  40. ];
  41. protected $template;
  42. /**
  43. * Form constructor.
  44. * @param BootstrapInterface $ui
  45. * @param string $action
  46. * @param array $rule
  47. * @param array $config
  48. * @throws FormBuilderException
  49. */
  50. protected function __construct(BootstrapInterface $ui, $action = '', $rule = [], $config = [])
  51. {
  52. $this->action = $action;
  53. $this->rule = $rule;
  54. $this->config = $config;
  55. $this->ui = $ui;
  56. $ui->init($this);
  57. $this->checkFieldUnique();
  58. $this->template = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Template' . DIRECTORY_SEPARATOR . 'form.php';
  59. }
  60. /**
  61. * @return string
  62. */
  63. public function getFormContentType()
  64. {
  65. return $this->formContentType;
  66. }
  67. /**
  68. * @param string $name
  69. * @param string $value
  70. * @return $this
  71. */
  72. public function setHeader($name, $value)
  73. {
  74. $this->headers[$name] = (string)$value;
  75. return $this;
  76. }
  77. /**
  78. * @param array $headers
  79. * @return $this
  80. */
  81. public function headers(array $headers)
  82. {
  83. $this->headers = $headers;
  84. return $this;
  85. }
  86. /**
  87. * @param array $formData
  88. * @return $this
  89. */
  90. public function formData(array $formData)
  91. {
  92. $this->formData = $formData;
  93. return $this;
  94. }
  95. /**
  96. * @param $field
  97. * @param $value
  98. * @return $this
  99. */
  100. public function setValue($field, $value)
  101. {
  102. $this->formData[$field] = $value;
  103. return $this;
  104. }
  105. /**
  106. * @return false|string
  107. */
  108. public function parseHeaders()
  109. {
  110. return json_encode((object)$this->headers);
  111. }
  112. /**
  113. * @param $formContentType
  114. * @return $this
  115. */
  116. public function setFormContentType($formContentType)
  117. {
  118. $this->formContentType = (string)$formContentType;
  119. return $this;
  120. }
  121. public function setDependScript(array $dependScript)
  122. {
  123. $this->dependScript = $dependScript;
  124. return $this;
  125. }
  126. /**
  127. * @param string $title
  128. * @return $this
  129. */
  130. public function setTitle($title)
  131. {
  132. $this->title = (string)$title;
  133. return $this;
  134. }
  135. /**
  136. * @param string $method
  137. * @return $this
  138. */
  139. public function setMethod($method)
  140. {
  141. $this->method = (string)$method;
  142. return $this;
  143. }
  144. /**
  145. * @return string|null
  146. */
  147. public function getTitle()
  148. {
  149. return $this->title;
  150. }
  151. /**
  152. * @return string|null
  153. */
  154. public function getMethod()
  155. {
  156. return $this->method;
  157. }
  158. /**
  159. * @param array $rule
  160. * @return $this
  161. * @throws FormBuilderException
  162. */
  163. public function setRule(array $rule)
  164. {
  165. $this->rule = $rule;
  166. $this->checkFieldUnique();
  167. return $this;
  168. }
  169. /**
  170. * @param array|ConfigInterface $config
  171. * @return $this
  172. */
  173. public function setConfig($config)
  174. {
  175. if (is_array($config) || $config instanceof ConfigInterface)
  176. $this->config = $config;
  177. return $this;
  178. }
  179. /**
  180. * 追加组件
  181. *
  182. * @param $component
  183. * @return $this
  184. * @throws FormBuilderException
  185. */
  186. public function append($component)
  187. {
  188. $this->rule[] = $component;
  189. $this->checkFieldUnique();
  190. return $this;
  191. }
  192. /**
  193. * 开头插入组件
  194. *
  195. * @param $component
  196. * @return $this
  197. * @throws FormBuilderException
  198. */
  199. public function prepend($component)
  200. {
  201. array_unshift($this->rule, $component);
  202. $this->checkFieldUnique();
  203. return $this;
  204. }
  205. /**
  206. * @param $action
  207. * @return $this
  208. */
  209. public function setAction($action)
  210. {
  211. $this->action = (string)$action;
  212. return $this;
  213. }
  214. /**
  215. * @return string
  216. */
  217. public function getAction()
  218. {
  219. return $this->action;
  220. }
  221. /**
  222. * 提交按钮显示状态
  223. *
  224. * @param $isShow
  225. * @return $this
  226. */
  227. public function showSubmitBtn($isShow)
  228. {
  229. if ($this->config instanceof ConfigInterface)
  230. $this->config->submitBtn(!!$isShow);
  231. else
  232. $this->config['submitBtn'] = !!$isShow;
  233. return $this;
  234. }
  235. /**
  236. * 重置按钮显示状态
  237. *
  238. * @param $isShow
  239. * @return $this
  240. */
  241. public function showResetBtn($isShow)
  242. {
  243. if ($this->config instanceof ConfigInterface)
  244. $this->config->resetBtn(!!$isShow);
  245. else
  246. $this->config['resetBtn'] = !!$isShow;
  247. return $this;
  248. }
  249. /**
  250. * 设置组件全局配置
  251. * @param string $componentName
  252. * @param array $config
  253. * @return $this
  254. */
  255. public function componentGlobalConfig($componentName, array $config)
  256. {
  257. if ($this->config instanceof ConfigInterface)
  258. $this->config->componentGlobalConfig($componentName, $config);
  259. else {
  260. if (!isset($this->config['global'])) $this->config['global'] = [];
  261. $this->config['global'][$componentName] = $config;
  262. }
  263. return $this;
  264. }
  265. protected function parseFormComponent($rule)
  266. {
  267. if (Util::isComponent($rule)) {
  268. $rule = $rule->build();
  269. } else {
  270. if (isset($rule['children']) && is_array($rule['children'])) {
  271. foreach ($rule['children'] as $i => $child) {
  272. $rule['children'][$i] = $this->parseFormComponent($child);
  273. }
  274. }
  275. if (isset($rule['control'])) {
  276. foreach ($rule['control'] as $i => $child) {
  277. foreach ($child['rule'] as $k => $rule) {
  278. $child['rule'][$k] = Util::isComponent($rule) ? $rule->build() : $rule;
  279. }
  280. $rule['control'][$i] = $child;
  281. }
  282. }
  283. }
  284. return $rule;
  285. }
  286. public function getDependScript()
  287. {
  288. return $this->dependScript;
  289. }
  290. /**
  291. * @param array $formData
  292. * @param array $rule
  293. * @return array
  294. */
  295. protected function deepSetFormData($formData, $rule)
  296. {
  297. if (!count($formData)) return $rule;
  298. foreach ($rule as $k => $item) {
  299. if (is_array($item)) {
  300. if (isset($item['field']) && isset($formData[$item['field']])) {
  301. $item['value'] = $formData[$item['field']];
  302. }
  303. if (isset($item['children']) && is_array($item['children']) && count($item['children'])) {
  304. $item['children'] = $this->deepSetFormData($formData, $item['children']);
  305. }
  306. if (isset($item['control']) && count($item['control'])) {
  307. foreach ($item['control'] as $_k => $_rule) {
  308. $item['control'][$_k]['rule'] = $this->deepSetFormData($formData, $_rule['rule']);
  309. }
  310. }
  311. }
  312. $rule[$k] = $item;
  313. }
  314. return $rule;
  315. }
  316. /**
  317. * 获取表单生成规则
  318. *
  319. * @return array
  320. */
  321. public function formRule()
  322. {
  323. $rules = [];
  324. foreach ($this->rule as $rule) {
  325. $rules[] = $this->parseFormComponent($rule);
  326. }
  327. return $this->deepSetFormData($this->formData, $rules);
  328. }
  329. /**
  330. * @return false|string
  331. */
  332. public function parseFormRule()
  333. {
  334. return json_encode($this->formRule());
  335. }
  336. /**
  337. * @return false|string
  338. */
  339. public function parseFormConfig()
  340. {
  341. return json_encode((object)$this->formConfig());
  342. }
  343. /**
  344. * @return string
  345. */
  346. public function parseDependScript()
  347. {
  348. return implode("\r\n", $this->dependScript);
  349. }
  350. /**
  351. * 获取表单配置
  352. *
  353. * @return array
  354. */
  355. public function formConfig()
  356. {
  357. $config = $this->config;
  358. if ($config instanceof ConfigInterface) return $config->getConfig();
  359. foreach ($config as $k => $v) {
  360. $config[$k] = $this->parseFormComponent($v);
  361. }
  362. return $config;
  363. }
  364. /**
  365. * 获取表单创建的 js 代码
  366. *
  367. * @return false|string
  368. */
  369. public function formScript()
  370. {
  371. return $this->template(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Template' . DIRECTORY_SEPARATOR . 'createScript.min.php');
  372. }
  373. /**
  374. * 获取表单视图
  375. *
  376. * @return string
  377. */
  378. public function view()
  379. {
  380. return $this->template($this->template);
  381. }
  382. /**
  383. * 自定义表单页面
  384. *
  385. * @param $templateDir
  386. * @return false|string
  387. */
  388. public function template($templateDir)
  389. {
  390. ob_start();
  391. $form = $this;
  392. require $templateDir;
  393. $html = ob_get_clean();
  394. return $html;
  395. }
  396. /**
  397. * 设置模板
  398. *
  399. * @param string $templateDir
  400. * @return $this
  401. */
  402. public function setTemplate($templateDir)
  403. {
  404. $this->template = $templateDir;
  405. return $this;
  406. }
  407. /**
  408. * 检查field 是否重复
  409. *
  410. * @param null $rules
  411. * @param array $fields
  412. * @return array
  413. * @throws FormBuilderException
  414. */
  415. protected function checkFieldUnique($rules = null, $fields = [])
  416. {
  417. if (is_null($rules)) $rules = $this->rule;
  418. foreach ($rules as $rule) {
  419. $rule = $this->parseFormComponent($rule);
  420. $field = isset($rule['field']) ? $rule['field'] : null;
  421. if (isset($rule['children']) && count($rule['children']))
  422. $fields = $this->checkFieldUnique($rule['children'], $fields);
  423. if (is_null($field) || $field === '')
  424. continue;
  425. else if (isset($fields[$field]))
  426. throw new FormBuilderException('组件的 field 不能重复');
  427. else
  428. $fields[$field] = true;
  429. }
  430. return $fields;
  431. }
  432. /**
  433. * Iview 版表单生成器
  434. *
  435. * @param string $action
  436. * @param array $rule
  437. * @param array|ConfigInterface $config
  438. * @return Form
  439. * @throws FormBuilderException
  440. */
  441. public static function iview($action = '', $rule = [], $config = [])
  442. {
  443. return new self(new IViewBootstrap(), $action, $rule, $config);
  444. }
  445. /**
  446. * Iview v4 版表单生成器
  447. *
  448. * @param string $action
  449. * @param array $rule
  450. * @param array|ConfigInterface $config
  451. * @return Form
  452. * @throws FormBuilderException
  453. */
  454. public static function iview4($action = '', $rule = [], $config = [])
  455. {
  456. return new self(new IViewBootstrap(4), $action, $rule, $config);
  457. }
  458. /**
  459. * element-ui 版表单生成器
  460. *
  461. * @param string $action
  462. * @param array $rule
  463. * @param array|ConfigInterface $config
  464. * @return Form
  465. * @throws FormBuilderException
  466. */
  467. public static function elm($action = '', $rule = [], $config = [])
  468. {
  469. return new self(new ElmBootstrap(), $action, $rule, $config);
  470. }
  471. }