UserFileImgValidator.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Module\File\Validator;
  3. use App\Module\File\Models\FileImg;
  4. use UCore\ValidationCore;
  5. use UCore\Validator;
  6. /**
  7. * 文件归属验证器
  8. *
  9. */
  10. class UserFileImgValidator extends Validator
  11. {
  12. /**
  13. * 验证文件是否属于用户
  14. *
  15. * @param mixed $value 文件ID
  16. * @param array $data 数据数组
  17. * @return bool 验证结果
  18. */
  19. public function validate(mixed $value, array $data): bool
  20. {
  21. $ufiled = $this->args[0] ?? "user_id";
  22. $user_id = $data[$ufiled] ?? null;
  23. return self::check($user_id, $value);
  24. }
  25. /**
  26. * 检查文件是否属于用户
  27. *
  28. * @param int $user_id 用户ID
  29. * @param int $file_id 文件ID
  30. * @return bool 检查结果
  31. */
  32. public function check($user_id, $file_id)
  33. {
  34. $file = FileImg::query()->where([
  35. 'user_id' => $user_id,
  36. 'id' => $file_id
  37. ])->first();
  38. if ($file) {
  39. return true;
  40. }
  41. return false;
  42. }
  43. }