IsExpiredValidator.php 1.1 KB

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