Radio.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\form\components;
  12. use crmeb\form\BaseComponent;
  13. use crmeb\form\BuildInterface;
  14. /**
  15. * 单选框组件
  16. * Class Radio
  17. * @package crmeb\form\components
  18. */
  19. class Radio extends BaseComponent implements BuildInterface
  20. {
  21. /**
  22. * 组件名
  23. */
  24. const NAME = 'radio';
  25. /**
  26. * 组件规则
  27. * @var array
  28. */
  29. protected $rule = [
  30. 'title' => '',
  31. 'field' => '',
  32. 'value' => 0,
  33. 'info' => '',
  34. 'vertical' => false,
  35. 'control' => [],
  36. 'options' => [],
  37. ];
  38. /**
  39. * Radio constructor.
  40. * @param string $title
  41. * @param string $field
  42. * @param null $value
  43. */
  44. public function __construct(string $field, string $title, $value = null)
  45. {
  46. $this->rule['title'] = $title;
  47. $this->rule['field'] = $field;
  48. $this->rule['value'] = !is_null($value) ? $value : null;
  49. }
  50. /**
  51. * 多个组件联动
  52. * @param array $controls
  53. * @return $this
  54. */
  55. public function controls(array $controls = [])
  56. {
  57. $this->rule['control'] = $controls;
  58. return $this;
  59. }
  60. /**
  61. * options数据 ['label'=>'确定','value'=>1]
  62. * @param array $options
  63. * @return $this
  64. */
  65. public function options(array $options = [])
  66. {
  67. $this->rule['options'] = $options;
  68. return $this;
  69. }
  70. /**
  71. * 组件联动
  72. * @param $value
  73. * @param array $components
  74. * @return $this
  75. */
  76. public function control($value, array $components = [])
  77. {
  78. $this->rule['control'][] = ['value' => $value, 'componentsModel' => $components];
  79. return $this;
  80. }
  81. /**
  82. * 设置提示语
  83. * @param string $info
  84. * @return $this
  85. */
  86. public function info(string $info)
  87. {
  88. $this->rule['info'] = $info;
  89. return $this;
  90. }
  91. /**
  92. * 是否垂直展示
  93. * @param bool $vertical
  94. * @return $this
  95. */
  96. public function vertical(bool $vertical)
  97. {
  98. $this->rule['vertical'] = $vertical;
  99. return $this;
  100. }
  101. /**
  102. * 数据转换
  103. * @return array
  104. */
  105. public function toArray(): array
  106. {
  107. $this->rule['name'] = self::NAME;
  108. $control = [];
  109. foreach ($this->rule['control'] as $item) {
  110. $data = ['value' => $item['value'], 'componentsModel' => []];
  111. foreach ($item['componentsModel'] as $value) {
  112. if ($value instanceof BuildInterface) {
  113. $data['componentsModel'][] = $value->toArray();
  114. }
  115. }
  116. $control[] = $data;
  117. }
  118. $this->rule['control'] = $control;
  119. $this->before();
  120. return $this->rule;
  121. }
  122. }