CraftConditionValidator.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Module\GameItems\Validators;
  3. use App\Module\Game\Services\ConditionService;
  4. use UCore\Validator;
  5. /**
  6. * 合成条件验证器
  7. *
  8. * 基于组系统验证用户是否满足合成条件
  9. */
  10. class CraftConditionValidator extends Validator
  11. {
  12. /**
  13. * 验证合成条件
  14. *
  15. * @param mixed $value 配方ID
  16. * @param array $data 包含用户ID的数组
  17. * @return bool 验证是否通过
  18. */
  19. public function validate(mixed $value, array $data): bool
  20. {
  21. // 从 args 获取参数键名
  22. $userIdKey = $this->args[0] ?? 'user_id';
  23. $recipeKey = $this->args[1] ?? 'recipe';
  24. $userId = $data[$userIdKey] ?? null;
  25. $recipe = $this->validation->$recipeKey ?? null;
  26. if (!$userId || !$recipe) {
  27. $this->addError('验证合成条件时缺少必要参数');
  28. return false;
  29. }
  30. // 如果配方没有条件组,则直接通过
  31. if (!$recipe->condition_group_id) {
  32. return true;
  33. }
  34. try {
  35. // 使用条件组服务检查条件
  36. $checkResult = ConditionService::checkCondition($userId, $recipe->condition_group_id);
  37. if (!$checkResult['success']) {
  38. $this->addError($checkResult['message']);
  39. return false;
  40. }
  41. return true;
  42. } catch (\Exception $e) {
  43. $this->addError('验证合成条件时发生错误: ' . $e->getMessage());
  44. return false;
  45. }
  46. }
  47. }