CraftRecipeValidator.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Module\GameItems\Validators;
  3. use App\Module\GameItems\Models\ItemCraftRecipe;
  4. use UCore\Validator;
  5. /**
  6. * 合成配方验证器
  7. *
  8. * 验证合成配方是否存在且可用
  9. */
  10. class CraftRecipeValidator extends Validator
  11. {
  12. /**
  13. * 验证合成配方
  14. *
  15. * @param mixed $value 配方ID
  16. * @param array $data 包含其他数据的数组
  17. * @return bool 验证是否通过
  18. */
  19. public function validate(mixed $value, array $data): bool
  20. {
  21. $recipeId = (int)$value;
  22. try {
  23. // 获取配方信息
  24. $recipe = ItemCraftRecipe::find($recipeId);
  25. if (!$recipe) {
  26. $this->addError("合成配方不存在");
  27. return false;
  28. }
  29. if (!$recipe->is_active) {
  30. $this->addError("该合成配方已禁用");
  31. return false;
  32. }
  33. // 将配方信息保存到验证对象中,供后续使用
  34. $recipeKey = $this->args[0] ?? null;
  35. if ($recipeKey) {
  36. $this->validation->$recipeKey = $recipe;
  37. }
  38. return true;
  39. } catch (\Exception $e) {
  40. $this->addError('验证合成配方时发生错误: ' . $e->getMessage());
  41. return false;
  42. }
  43. }
  44. }