Switchs.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 Switchs
  17. * @package crmeb\form\components
  18. */
  19. class Switchs extends BaseComponent implements BuildInterface
  20. {
  21. /**
  22. * 组件名
  23. */
  24. const NAME = 'switch';
  25. /**
  26. * 规则
  27. * @var array
  28. */
  29. protected $rule = [
  30. 'title' => '',
  31. 'field' => '',
  32. 'value' => '',
  33. 'info' => '',
  34. 'control' => [],
  35. 'options' => [],
  36. ];
  37. /**
  38. * Switchs constructor.
  39. * @param string $field
  40. * @param string $title
  41. * @param int $value
  42. */
  43. public function __construct(string $field, string $title, int $value = null)
  44. {
  45. $this->rule['title'] = $title;
  46. $this->rule['field'] = $field;
  47. $this->rule['value'] = !is_null($value) ? intval($value) : null;
  48. }
  49. /**
  50. * 多组件群添加
  51. * @param array $controls
  52. * @return $this
  53. */
  54. public function controls(array $controls = [])
  55. {
  56. $this->rule['control'] = $controls;
  57. return $this;
  58. }
  59. /**
  60. * 组件联动添加
  61. * @param $value
  62. * @param array $components
  63. * @return $this
  64. */
  65. public function control(int $value, array $components = [])
  66. {
  67. $this->rule['control'][] = ['value' => $value, 'componentsModel' => $components];
  68. return $this;
  69. }
  70. /**
  71. * 开启值和名称设置
  72. * @param string $label
  73. * @param int $value
  74. * @return $this
  75. */
  76. public function trueValue(string $label, int $value)
  77. {
  78. $this->rule['options'][] = ['trueValue' => $value, 'label' => $label];
  79. return $this;
  80. }
  81. /**
  82. * 关闭值和名称设置
  83. * @param string $label
  84. * @param int $value
  85. * @return $this
  86. */
  87. public function falseValue(string $label, int $value)
  88. {
  89. $this->rule['options'][] = ['falseValue' => $value, 'label' => $label];
  90. return $this;
  91. }
  92. /**
  93. * 设置提示信息
  94. * @param string $info
  95. * @return $this
  96. */
  97. public function info(string $info)
  98. {
  99. $this->rule['info'] = $info;
  100. return $this;
  101. }
  102. /**
  103. * 数据转换
  104. * @return array
  105. */
  106. public function toArray(): array
  107. {
  108. $this->rule['name'] = self::NAME;
  109. $control = [];
  110. foreach ($this->rule['control'] as $item) {
  111. $data = ['value' => $item['value'], 'componentsModel' => []];
  112. foreach ($item['componentsModel'] as $value) {
  113. if ($value instanceof BuildInterface) {
  114. $data['componentsModel'][] = $value->toArray();
  115. }
  116. }
  117. $control[] = $data;
  118. }
  119. $this->rule['control'] = $control;
  120. $this->before();
  121. return $this->rule;
  122. }
  123. }