UploadImage.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 UploadImage
  17. * @package crmeb\form\components
  18. */
  19. class UploadImage extends BaseComponent implements BuildInterface
  20. {
  21. // 组件名
  22. const NAME = 'uploadImage';
  23. //图片类型
  24. const IMAGE = 'image';
  25. //文件类型
  26. const FILE = 'file';
  27. //上传支持类型
  28. const TYPE = [self::IMAGE, self::FILE];
  29. /**
  30. * 规则
  31. * @var array
  32. */
  33. protected $rule = [
  34. 'upload' => [
  35. 'url' => '',
  36. 'size' => 2097152,
  37. 'format' => [],
  38. 'headers' => [],
  39. 'maxNum' => 1,
  40. ],
  41. 'field' => '',
  42. 'type' => '',
  43. 'title' => '',
  44. 'icon' => '',
  45. 'info' => '',
  46. 'value' => ''
  47. ];
  48. /**
  49. * UploadImage constructor.
  50. * @param string $field
  51. * @param string $title
  52. * @param null $value
  53. */
  54. public function __construct(string $field, string $title, $value = null)
  55. {
  56. $this->rule['title'] = $title;
  57. $this->rule['field'] = $field;
  58. $this->rule['value'] = !is_null($value) ? $value : null;
  59. }
  60. /**
  61. * 上传地址
  62. * @param string $url
  63. * @return $this
  64. */
  65. public function url(string $url)
  66. {
  67. $this->rule['upload']['url'] = $url;
  68. return $this;
  69. }
  70. /**
  71. * 上传类型
  72. * @param string $type
  73. * @return $this
  74. */
  75. public function type(string $type)
  76. {
  77. $this->rule['type'] = in_array($type, self::TYPE) ? $type : '';
  78. return $this;
  79. }
  80. /**
  81. * 上传展示icon
  82. * @param string $icon
  83. * @return $this
  84. */
  85. public function icon(string $icon)
  86. {
  87. $this->rule['icon'] = $icon;
  88. return $this;
  89. }
  90. /**
  91. * 上传文件headers
  92. * @param array $headers
  93. * @return $this
  94. */
  95. public function headers(array $headers = [])
  96. {
  97. $this->rule['upload']['headers'] = (object)$headers;
  98. return $this;
  99. }
  100. /**
  101. * 上传文件大小
  102. * @param string $size
  103. * @return $this
  104. */
  105. public function size(string $size)
  106. {
  107. $this->rule['upload']['size'] = $size;
  108. return $this;
  109. }
  110. /**
  111. * 上传文件类型
  112. * @param array $format
  113. * @return $this
  114. */
  115. public function format(array $format = [])
  116. {
  117. $this->rule['upload']['format'] = $format;
  118. return $this;
  119. }
  120. /**
  121. * 组件提示
  122. * @param string $info
  123. * @return $this
  124. */
  125. public function info(string $info)
  126. {
  127. $this->rule['info'] = $info;
  128. return $this;
  129. }
  130. /**
  131. * @return array
  132. */
  133. public function toArray(): array
  134. {
  135. $this->rule['name'] = self::NAME;
  136. $this->before();
  137. return $this->rule;
  138. }
  139. }