Config.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. */
  11. namespace FormBuilder\UI\Iview;
  12. use FormBuilder\Contract\ConfigInterface;
  13. use FormBuilder\UI\Iview\Components\Button;
  14. use FormBuilder\UI\Iview\Components\Poptip;
  15. use FormBuilder\UI\Iview\Components\Tooltip;
  16. use FormBuilder\UI\Iview\Style\FormStyle;
  17. use FormBuilder\UI\Iview\Style\Row;
  18. class Config implements ConfigInterface
  19. {
  20. const INFO_TYPE_POPTIP = 'poptip';
  21. const INFO_TYPE_TOOLTIP = 'tooltip';
  22. protected $config;
  23. public function __construct(array $config = [])
  24. {
  25. $this->config = $config;
  26. }
  27. public function info($type)
  28. {
  29. if (is_array($type) || $type instanceof Poptip || $type instanceof Tooltip)
  30. $this->config['info'] = $type;
  31. return $this;
  32. }
  33. /**
  34. * @param string $type tooltip,poptip
  35. * @return Poptip|Tooltip
  36. */
  37. public function createInfo($type = self::INFO_TYPE_POPTIP)
  38. {
  39. if (strtolower($type) === self::INFO_TYPE_TOOLTIP)
  40. $info = new Tooltip();
  41. else
  42. $info = new Poptip();
  43. $this->info($info);
  44. return $info;
  45. }
  46. /**
  47. * 表单整体显示规则配置
  48. *
  49. * @param FormStyle|array $formStyle
  50. * @return $this
  51. */
  52. public function formStyle($formStyle)
  53. {
  54. $this->config['form'] = $formStyle;
  55. return $this;
  56. }
  57. public function createFormStyle(array $rule = [])
  58. {
  59. $formStyle = new FormStyle($rule);
  60. $this->formStyle($formStyle);
  61. return $formStyle;
  62. }
  63. /**
  64. * 表单组件布局配置
  65. *
  66. * @param Row|array $row
  67. * @return $this
  68. */
  69. public function row($row)
  70. {
  71. $this->config['row'] = $row;
  72. return $this;
  73. }
  74. public function createRow(array $rule = [])
  75. {
  76. $row = new Row($rule);
  77. $this->row($row);
  78. return $row;
  79. }
  80. /**
  81. * 提交按钮样式和布局配置
  82. *
  83. * @param Button|array|bool $btn
  84. * @return $this
  85. */
  86. public function submitBtn($btn)
  87. {
  88. $this->config['submitBtn'] = $btn;
  89. return $this;
  90. }
  91. public function createSubmitBtn()
  92. {
  93. $submitBtn = new Button();
  94. $this->submitBtn($submitBtn);
  95. return $submitBtn;
  96. }
  97. /**
  98. * 开启事件注入
  99. *
  100. * @param bool $bool
  101. * @return $this
  102. */
  103. public function injectEvent($bool)
  104. {
  105. $this->config['injectEvent'] = !!$bool;
  106. return $this;
  107. }
  108. /**
  109. * 重置按钮样式和布局配置
  110. *
  111. * @param Button|array|bool $btn
  112. * @return $this
  113. */
  114. public function resetBtn($btn)
  115. {
  116. $this->config['resetBtn'] = $btn;
  117. return $this;
  118. }
  119. /**
  120. * @return Button
  121. */
  122. public function createResetBtn()
  123. {
  124. $resetBtn = new Button();
  125. $this->resetBtn($resetBtn);
  126. return $resetBtn;
  127. }
  128. /**
  129. * @param Button $btn
  130. * @return mixed
  131. */
  132. protected function parseButton(Button $btn)
  133. {
  134. $rule = $btn->build();
  135. if (isset($rule['col']))
  136. $rule['props']->col = $rule['col'];
  137. return $rule['props'];
  138. }
  139. /**
  140. * @param $info
  141. * @return mixed
  142. */
  143. protected function parseInfo($info)
  144. {
  145. if ($info instanceof Poptip || $info instanceof Tooltip) {
  146. $rule = $info->build();
  147. $info = $rule['props'];
  148. $info->type = $rule['type'];
  149. }
  150. return $info;
  151. }
  152. /**
  153. * @return array
  154. */
  155. public function getConfig()
  156. {
  157. $config = $this->config;
  158. if (isset($config['form']) && ($form = $config['form']) instanceof FormStyle)
  159. $config['form'] = $form->getStyle();
  160. if (isset($config['row']) && ($row = $config['row']) instanceof Row)
  161. $config['row'] = $row->getStyle();
  162. if (isset($config['submitBtn']) && ($submitBtn = $config['submitBtn']) instanceof Button)
  163. $config['submitBtn'] = $this->parseButton($submitBtn);
  164. if (isset($config['resetBtn']) && ($resetBtn = $config['resetBtn']) instanceof Button)
  165. $config['resetBtn'] = $this->parseButton($resetBtn);
  166. if (isset($config['info']))
  167. $config['info'] = $this->parseInfo($config['info']);
  168. return $config;
  169. }
  170. }