| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- <?php
- namespace App\Module\GameItems\Validators;
- use App\Module\GameItems\Models\ItemCraftRecipe;
- use UCore\Validator;
- /**
- * 合成配方验证器
- *
- * 验证合成配方是否存在且可用
- */
- class CraftRecipeValidator extends Validator
- {
- /**
- * 验证合成配方
- *
- * @param mixed $value 配方ID
- * @param array $data 包含其他数据的数组
- * @return bool 验证是否通过
- */
- public function validate(mixed $value, array $data): bool
- {
- $recipeId = (int)$value;
- try {
- // 获取配方信息
- $recipe = ItemCraftRecipe::find($recipeId);
-
- if (!$recipe) {
- $this->addError("合成配方不存在");
- return false;
- }
- if (!$recipe->is_active) {
- $this->addError("该合成配方已禁用");
- return false;
- }
- // 将配方信息保存到验证对象中,供后续使用
- $recipeKey = $this->args[0] ?? null;
- if ($recipeKey) {
- $this->validation->$recipeKey = $recipe;
- }
- return true;
- } catch (\Exception $e) {
- $this->addError('验证合成配方时发生错误: ' . $e->getMessage());
- return false;
- }
- }
- }
|