ItemDismantleValidation.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Module\GameItems\Validations;
  3. use App\Module\GameItems\Validators\DismantleItemValidator;
  4. use App\Module\GameItems\Validators\ItemOwnershipValidator;
  5. use UCore\ValidationCore;
  6. /**
  7. * 物品分解验证类
  8. *
  9. * 用于验证物品分解操作的输入数据,包括用户ID、物品ID和数量
  10. */
  11. class ItemDismantleValidation extends ValidationCore
  12. {
  13. /** @var \App\Module\GameItems\Models\Item|null 分解物品对象,由 DismantleItemValidator 设置 */
  14. public ?\App\Module\GameItems\Models\Item $dismantle_item = null;
  15. /**
  16. * 验证规则
  17. *
  18. * @param array $rules 自定义规则
  19. * @return array
  20. */
  21. public function rules($rules = []): array
  22. {
  23. return [
  24. [
  25. 'user_id,item_id', 'required'
  26. ],
  27. [
  28. 'user_id,item_id,quantity', 'integer', 'min' => 1,
  29. 'msg' => '{attr}必须是大于0的整数'
  30. ],
  31. [
  32. 'instance_id', 'integer', 'min' => 0,
  33. 'msg' => '{attr}必须是大于等于0的整数'
  34. ],
  35. // 验证物品是否可分解
  36. [
  37. 'item_id', new DismantleItemValidator($this, ['dismantle_item']),
  38. 'msg' => '物品验证失败'
  39. ],
  40. // 验证用户是否拥有该物品
  41. [
  42. 'item_id', new ItemOwnershipValidator($this, ['user_id', 'instance_id', 'quantity']),
  43. 'msg' => '物品不存在或数量不足'
  44. ]
  45. ];
  46. }
  47. /**
  48. * 设置默认值
  49. *
  50. * @return array
  51. */
  52. public function default(): array
  53. {
  54. return [
  55. 'instance_id' => 0,
  56. 'quantity' => 1
  57. ];
  58. }
  59. }