ChestOpenValidation.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Module\GameItems\Validations;
  3. use App\Module\GameItems\Validators\ChestItemValidator;
  4. use App\Module\GameItems\Validators\ChestOwnershipValidator;
  5. use App\Module\GameItems\Validators\ChestOpenCostValidator;
  6. use UCore\ValidationCore;
  7. /**
  8. * 宝箱开启验证类
  9. *
  10. * 用于验证宝箱开启操作的输入数据,包括用户ID、宝箱ID和实例ID
  11. */
  12. class ChestOpenValidation extends ValidationCore
  13. {
  14. /**
  15. * 验证规则
  16. *
  17. * @param array $rules 自定义规则
  18. * @return array
  19. */
  20. public function rules($rules = []): array
  21. {
  22. return [
  23. [
  24. 'user_id,item_id', 'required'
  25. ],
  26. [
  27. 'user_id,item_id', 'integer', 'min' => 1,
  28. 'msg' => '{attr}必须是大于0的整数'
  29. ],
  30. [
  31. 'instance_id', 'integer', 'min' => 0,
  32. 'msg' => '{attr}必须是大于等于0的整数'
  33. ],
  34. // 验证物品是否为宝箱类型
  35. [
  36. 'item_id', new ChestItemValidator($this, ['chest_item']),
  37. 'msg' => '物品验证失败'
  38. ],
  39. // 验证用户是否拥有该宝箱
  40. [
  41. 'item_id', new ChestOwnershipValidator($this, ['user_id', 'instance_id']),
  42. 'msg' => '宝箱不存在或不属于当前用户'
  43. ],
  44. // 验证开启消耗是否充足
  45. [
  46. 'item_id', new ChestOpenCostValidator($this, ['user_id', 'quantity']),
  47. 'msg' => '开启消耗不足'
  48. ]
  49. ];
  50. }
  51. /**
  52. * 设置默认值
  53. *
  54. * @return array
  55. */
  56. public function default(): array
  57. {
  58. return [
  59. 'instance_id' => 0,
  60. 'quantity' => 1
  61. ];
  62. }
  63. }