AllowSettingConfig.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. namespace App\Http\Requests\API\Config;
  3. use App\Repositories\Enums\EmailConfigFieldEnum;
  4. use App\Repositories\Enums\BrowserConfigFiledEnum;
  5. class AllowSettingConfig
  6. {
  7. public function check(string $group, string $key, mixed $value)
  8. {
  9. if (! method_exists($this, $group)) {
  10. throw new \Exception(sprintf("%s configuration group is not supported", $group));
  11. }
  12. $groupRules = $this->$group();
  13. if (! isset($groupRules[$key])) {
  14. throw new \Exception(sprintf("The %s configuration field under the %s group is not supported", $key, $group));
  15. }
  16. $result = validator([$key => $value], [$key => $groupRules[$key]]);
  17. if ($result->fails()) {
  18. throw new \Exception($result->errors()->first());
  19. }
  20. }
  21. /**
  22. * email 字段验证
  23. *
  24. * @return string[]
  25. */
  26. private function email(): array
  27. {
  28. return EmailConfigFieldEnum::checkRules();
  29. }
  30. private function browser(): array
  31. {
  32. return BrowserConfigFiledEnum::checkRules();
  33. }
  34. }