| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- <?php
- namespace App\Module\Farm\Validators;
- use App\Module\GameItems\Enums\ITEM_TYPE;
- use App\Module\GameItems\Models\Item;
- use App\Module\Farm\Models\FarmSeed;
- use UCore\Validator;
- /**
- * 种子物品验证器
- *
- * 验证物品是否为种子类型且配置正确
- */
- class SeedItemValidator extends Validator
- {
- /**
- * 验证种子物品
- *
- * @param mixed $value 物品ID
- * @param array $data 包含其他数据的数组
- * @return bool 验证是否通过
- */
- public function validate(mixed $value, array $data): bool
- {
- $itemId = (int)$value;
- try {
- // 获取物品信息
- $item = Item::find($itemId);
- if (!$item) {
- $this->addError("物品不存在");
- return false;
- }
- // 检查种子是否有配置信息
- $seedConfig = FarmSeed::where('item_id', $itemId)->first();
- if (!$seedConfig) {
- $this->addError("种子没有配置信息");
- return false;
- }
- // 将种子信息保存到验证对象中,供后续使用
- $seedItemKey = $this->args[0] ?? null;
- if ($seedItemKey) {
- $this->validation->$seedItemKey = $item;
- }
- return true;
- } catch (\Exception $e) {
- $this->addError('验证种子物品时发生错误: ' . $e->getMessage());
- return false;
- }
- }
- }
|