InputNumber.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 InputNumber
  17. * @package crmeb\form\components
  18. */
  19. class InputNumber extends BaseComponent implements BuildInterface
  20. {
  21. /**
  22. * 组件名
  23. */
  24. const NAME = 'inputNumber';
  25. /**
  26. * 规则
  27. * @var string[]
  28. */
  29. protected $rule = [
  30. 'title' => '',
  31. 'value' => '',
  32. 'type' => '',
  33. 'field' => '',
  34. 'prefix' => '',
  35. 'suffix' => '',
  36. 'info' => '',
  37. 'min' => null,
  38. 'max' => 99999999
  39. ];
  40. /**
  41. * InputNumber constructor.
  42. * @param string $field
  43. * @param string $title
  44. * @param null $value
  45. */
  46. public function __construct(string $field, string $title, $value = null)
  47. {
  48. $this->rule['title'] = $title;
  49. $this->rule['field'] = $field;
  50. $this->rule['value'] = floatval($value);
  51. }
  52. /**
  53. * 提示语
  54. * @param string $info
  55. * @return $this
  56. */
  57. public function info(string $info)
  58. {
  59. $this->rule['info'] = $info;
  60. return $this;
  61. }
  62. /**
  63. * 最小值
  64. * @param int $min
  65. * @return $this
  66. */
  67. public function min(int $min)
  68. {
  69. $this->rule['min'] = $min;
  70. return $this;
  71. }
  72. /**
  73. * 最大值
  74. * @param int $max
  75. * @return $this
  76. */
  77. public function max(int $max)
  78. {
  79. $this->rule['max'] = $max;
  80. return $this;
  81. }
  82. /**
  83. * @return array|string[]
  84. */
  85. public function toArray(): array
  86. {
  87. $this->rule['name'] = self::NAME;
  88. $this->before();
  89. return $this->rule;
  90. }
  91. /**
  92. * @param $name
  93. * @param $arguments
  94. * @return $this
  95. */
  96. public function __call($name, $arguments)
  97. {
  98. if (in_array($name, ['title', 'field'])) {
  99. return $this;
  100. }
  101. $keys = array_keys($this->rule);
  102. if (in_array($name, $keys)) {
  103. $this->rule[$name] = $arguments[0] ?? null;
  104. }
  105. }
  106. }