| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace App\Module\Farm\Validators;
- use App\Module\GameItems\Models\Item;
- use App\Module\GameItems\Services\ItemService;
- use Illuminate\Support\Facades\Log;
- use UCore\Validator;
- /**
- * 肥料物品验证器
- *
- * 验证物品是否为有效的肥料,并获取肥料属性
- */
- class FertilizerItemValidator extends Validator
- {
- /**
- * 验证肥料物品
- *
- * @param mixed $value 物品ID
- * @param array $data 包含其他数据的数组
- * @return bool
- */
- public function validate(mixed $value, array $data): bool
- {
- try {
- Log::info('FertilizerItemValidator开始验证', [
- 'item_id' => $value,
- 'data' => $data
- ]);
- // 获取物品信息
- $item = Item::find($value);
- if (!$item) {
- Log::warning('FertilizerItemValidator: 物品不存在', ['item_id' => $value]);
- $this->addError('物品不存在');
- return false;
- }
- // 获取肥料的crop_growth_time属性
- $cropGrowthTime = ItemService::getItemNumericAttribute($value, 'crop_growth_time', 0);
- Log::info('FertilizerItemValidator: 获取到crop_growth_time', [
- 'item_id' => $value,
- 'crop_growth_time' => $cropGrowthTime
- ]);
- // 验证是否为有效的肥料(crop_growth_time > 0)
- if ($cropGrowthTime <= 0) {
- Log::warning('FertilizerItemValidator: 物品不是有效的肥料', [
- 'item_id' => $value,
- 'crop_growth_time' => $cropGrowthTime
- ]);
- $this->addError('该物品不是有效的肥料');
- return false;
- }
- // 设置验证对象的属性
- if (isset($this->args[0])) {
- $this->validation->{$this->args[0]} = $item;
- }
- if (isset($this->args[1])) {
- $this->validation->{$this->args[1]} = $cropGrowthTime;
- }
- Log::info('FertilizerItemValidator: 验证通过', [
- 'item_id' => $value,
- 'crop_growth_time' => $cropGrowthTime
- ]);
- return true;
- } catch (\Exception $e) {
- $this->addError('验证肥料物品时发生错误:' . $e->getMessage());
- return false;
- }
- }
- }
|