ConfigOptions.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Rules;
  3. use App\Models\Config;
  4. use Illuminate\Contracts\Validation\Rule;
  5. use Illuminate\Support\Facades\Validator;
  6. /**
  7. * 单选或者多选类型时,选项配置验证规则
  8. * 必须至少要有一个类似 1=>值 的值
  9. *
  10. * Class ConfigSelectTypeOptions
  11. * @package App\Rules
  12. */
  13. class ConfigSelectTypeOptions implements Rule
  14. {
  15. protected $errorMessage;
  16. public function passes($attribute, $value)
  17. {
  18. // 为空不验证
  19. if (!$value) {
  20. return true;
  21. }
  22. if (!is_string($value)) {
  23. return false;
  24. }
  25. $pairs = explode("\n", $value);
  26. foreach ($pairs as $pair) {
  27. $p = explode('=>', $pair);
  28. if (count($p) < 2) {
  29. return false;
  30. }
  31. $label = $p[1];
  32. // label 有就表示有效,有一个有效,则可以通过
  33. if ($label) {
  34. return true;
  35. }
  36. }
  37. return false;
  38. }
  39. public function message()
  40. {
  41. return '选项设置 无效。';
  42. }
  43. }
  44. class ConfigOptions implements Rule
  45. {
  46. /**
  47. * @var string 配置类型
  48. */
  49. protected $type;
  50. protected $errorMessage;
  51. /**
  52. * Create a new rule instance.
  53. *
  54. * @param string $type
  55. *
  56. * @return void
  57. */
  58. public function __construct(string $type = null)
  59. {
  60. $this->type = $type;
  61. }
  62. /**
  63. * Determine if the validation rule passes.
  64. *
  65. * @param string $attribute
  66. * @param mixed $value
  67. *
  68. * @return bool
  69. */
  70. public function passes($attribute, $value)
  71. {
  72. // 如果 type 类型不对,则不用验证
  73. if (!isset(Config::$typeMap[$this->type])) {
  74. return true;
  75. }
  76. switch ($this->type) {
  77. case Config::TYPE_INPUT:
  78. case Config::TYPE_TEXTAREA:
  79. case Config::TYPE_OTHER:
  80. !is_null($value) && ($this->errorMessage = '选项 必须为空。');
  81. break;
  82. case Config::TYPE_FILE:
  83. if (!is_array($value)) {
  84. $this->errorMessage = '选项 无效。';
  85. break;
  86. }
  87. $validator = Validator::make($value, [
  88. 'max' => 'required|integer|between:1,99',
  89. 'ext' => 'nullable',
  90. ], [], [
  91. 'max' => '最大上传数',
  92. 'ext' => '文件类型',
  93. ]);
  94. break;
  95. case Config::TYPE_SINGLE_SELECT:
  96. case Config::TYPE_MULTIPLE_SELECT:
  97. if (!is_array($value)) {
  98. $this->errorMessage = '选项 无效。';
  99. break;
  100. }
  101. $validator = Validator::make($value, [
  102. 'options' => ['required', new ConfigSelectTypeOptions()],
  103. 'type' => 'required|in:input,select',
  104. ], [], [
  105. 'options' => '选项设置',
  106. 'type' => '选择形式',
  107. ]);
  108. break;
  109. default:
  110. // do nothing
  111. }
  112. if (isset($validator) && $validator->fails()) {
  113. $this->errorMessage = $validator->getMessageBag()->first();
  114. }
  115. return !$this->errorMessage;
  116. }
  117. /**
  118. * Get the validation error message.
  119. *
  120. * @return string
  121. */
  122. public function message()
  123. {
  124. return $this->errorMessage;
  125. }
  126. }