IsExpiredValidator.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Module\GameItems\Validators;
  3. use UCore\Validator;
  4. /**
  5. * 物品是否过期
  6. *
  7. */
  8. class IsExpiredValidator extends Validator
  9. {
  10. /**
  11. * @param mixed $value item_user 表的 id
  12. * @param array $data
  13. * @return bool
  14. */
  15. public function validate(mixed $value, array $data): bool
  16. {
  17. if (empty($value)) {
  18. $this->setError('物品ID不能为空');
  19. return false;
  20. }
  21. $itemUser = ItemUser::find($value);
  22. if (!$itemUser) {
  23. $this->setError('物品不存在');
  24. return false;
  25. }
  26. // 1. 检查item_user表的过期时间
  27. if (!is_null($itemUser->expire_at)) {
  28. if (now()->gt($itemUser->expire_at)) {
  29. $this->setError('物品已过期');
  30. return false;
  31. }
  32. return true;
  33. }
  34. // 2. 检查item表的全局过期时间
  35. $item = $itemUser->item;
  36. if ($item->isExpired()) {
  37. $this->setError('物品已全局过期');
  38. return false;
  39. }
  40. return true;
  41. }
  42. }