AdminPermissionHttpPath.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Rules;
  3. use App\Models\AdminPermission;
  4. use Illuminate\Contracts\Validation\Rule;
  5. use Illuminate\Support\Str;
  6. class AdminPermissionHttpPath implements Rule
  7. {
  8. protected $errorMethods;
  9. /**
  10. * Determine if the validation rule passes.
  11. *
  12. * @param string $attribute
  13. * @param mixed $value
  14. *
  15. * @return bool
  16. */
  17. public function passes($attribute, $value)
  18. {
  19. $path = array_filter(explode("\n", $value));
  20. foreach ($path as $i) {
  21. if (Str::contains($i, ':')) {
  22. [$methods, $path] = explode(':', $i);
  23. if ($methods) {
  24. $methods = explode(',', $methods);
  25. $errorMethods = array_diff($methods, AdminPermission::$httpMethods);
  26. if (!empty($errorMethods)) {
  27. $this->errorMethods = implode(',', $errorMethods);
  28. return false;
  29. }
  30. }
  31. }
  32. }
  33. return true;
  34. }
  35. /**
  36. * Get the validation error message.
  37. *
  38. * @return string
  39. */
  40. public function message()
  41. {
  42. return "HTTP 方法 [ {$this->errorMethods} ] 无效";
  43. }
  44. }