Input.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace crmeb\form\components;
  3. use crmeb\form\BaseComponent;
  4. use crmeb\form\BuildInterface;
  5. /**
  6. * 输入框组件
  7. * Class Input
  8. * @package crmeb\form\components
  9. * @method Input info(string $info) 设置info
  10. * @method Input placeholder(string $placeholder) 设置placeholder
  11. * @method Input suffix(string $suffix) 输入框头部图标
  12. * @method Input prefix(string $prefix) 输入框尾部图标
  13. * @method Input value($value) 设置value
  14. * @method Input rows(int $rows) 设置rows
  15. * @method Input type(string $type) 设置type
  16. * @method Input maxlength(int $maxlength) 设置maxlength
  17. */
  18. class Input extends BaseComponent implements BuildInterface
  19. {
  20. /**
  21. * 组件名称
  22. */
  23. const NAME = 'input';
  24. /**
  25. * 规则
  26. * @var string[]
  27. */
  28. protected $rule = [
  29. 'title' => '',
  30. 'value' => '',
  31. 'type' => '',
  32. 'field' => '',
  33. 'info' => '',
  34. 'disabled' => false,
  35. 'placeholder' => '',
  36. 'suffix' => '',
  37. 'prefix' => '',
  38. 'rows' => 2,
  39. 'copy' => false,
  40. 'copyText' => '',
  41. 'randToken' => 0,
  42. 'maxlength' => null,
  43. ];
  44. /**
  45. * Input constructor.
  46. * @param string $field
  47. * @param string $title
  48. * @param null $value
  49. */
  50. public function __construct(string $field, string $title, $value = null)
  51. {
  52. $this->rule['title'] = $title;
  53. $this->rule['field'] = $field;
  54. $this->rule['value'] = empty($value) ? '' : $value;
  55. }
  56. /**
  57. * 是否禁用
  58. * @param bool $disabled
  59. * @return $this
  60. */
  61. public function disabled(bool $disabled = true)
  62. {
  63. $this->rule['disabled'] = $disabled;
  64. return $this;
  65. }
  66. /**
  67. * 随机token
  68. * @return $this
  69. */
  70. public function randToken()
  71. {
  72. $this->rule['randToken'] = 1;
  73. return $this;
  74. }
  75. /**
  76. * 随机encodingAESKeyGen
  77. * @return $this
  78. */
  79. public function randAESK()
  80. {
  81. $this->rule['randToken'] = 2;
  82. return $this;
  83. }
  84. /**
  85. * 复制按钮
  86. * @param string $copyText
  87. * @return $this
  88. */
  89. public function copy(string $copyText = '复制')
  90. {
  91. $this->rule['copy'] = true;
  92. $this->rule['copyText'] = $copyText;
  93. return $this;
  94. }
  95. /**
  96. * @return string[]
  97. */
  98. public function toArray(): array
  99. {
  100. $this->rule['name'] = self::NAME;
  101. $this->before();
  102. return $this->rule;
  103. }
  104. /**
  105. * @param $name
  106. * @param $arguments
  107. * @return $this
  108. */
  109. public function __call($name, $arguments)
  110. {
  111. if (in_array($name, ['title', 'field', 'disabled', 'copyText'])) {
  112. return $this;
  113. }
  114. $keys = array_keys($this->rule);
  115. if (in_array($name, $keys)) {
  116. $this->rule[$name] = $arguments[0] ?? null;
  117. }
  118. return $this;
  119. }
  120. }