| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- <?php
- namespace App\Module\Farm\Validators;
- use App\Module\Farm\Services\HouseService;
- use UCore\Validator;
- /**
- * 房屋升级验证器
- *
- * 验证房屋是否可以升级
- */
- class HouseUpgradeValidator extends Validator
- {
- /**
- * 验证房屋升级条件
- *
- * @param mixed $value 用户ID
- * @param array $data 包含用户ID的数组
- * @return bool 验证是否通过
- */
- public function validate(mixed $value, array $data): bool
- {
- $userId = (int)$value;
- try {
- // 检查房屋升级条件
- HouseService::checkUpgradeRequirements($userId);
- // 获取房屋配置信息,供后续使用
- $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first();
- if (!$farmUser) {
- $this->addError('用户农场不存在');
- return false;
- }
- $currentLevel = $farmUser->house_level;
- $nextLevel = $currentLevel + 1;
- $nextLevelConfig = HouseService::getHouseConfig($nextLevel);
- // 将房屋配置保存到验证对象中,供后续使用
- $houseConfigKey = $this->args[0] ?? null;
- if ($houseConfigKey) {
- $this->validation->$houseConfigKey = $nextLevelConfig;
- }
- return true;
- } catch (\Exception $e) {
- $this->addError($e->getMessage());
- return false;
- }
- }
- }
|