HouseUpgradeValidator.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Module\Farm\Validators;
  3. use App\Module\Farm\Services\HouseService;
  4. use UCore\Validator;
  5. /**
  6. * 房屋升级验证器
  7. *
  8. * 验证房屋是否可以升级
  9. */
  10. class HouseUpgradeValidator extends Validator
  11. {
  12. /**
  13. * 验证房屋升级条件
  14. *
  15. * @param mixed $value 用户ID
  16. * @param array $data 包含用户ID的数组
  17. * @return bool 验证是否通过
  18. */
  19. public function validate(mixed $value, array $data): bool
  20. {
  21. $userId = (int)$value;
  22. try {
  23. // 检查房屋升级条件
  24. HouseService::checkUpgradeRequirements($userId);
  25. // 获取房屋配置信息,供后续使用
  26. $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first();
  27. if (!$farmUser) {
  28. $this->addError('用户农场不存在');
  29. return false;
  30. }
  31. $currentLevel = $farmUser->house_level;
  32. $nextLevel = $currentLevel + 1;
  33. $nextLevelConfig = HouseService::getHouseConfig($nextLevel);
  34. // 将房屋配置保存到验证对象中,供后续使用
  35. $houseConfigKey = $this->args[0] ?? null;
  36. if ($houseConfigKey) {
  37. $this->validation->$houseConfigKey = $nextLevelConfig;
  38. }
  39. return true;
  40. } catch (\Exception $e) {
  41. $this->addError($e->getMessage());
  42. return false;
  43. }
  44. }
  45. }