ItemQuantityValidation.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace App\Module\GameItems\Validation;
  3. use App\Module\GameItems\Validators\ItemQuantityValidator;
  4. use UCore\ValidationCore;
  5. /**
  6. * 物品数量验证类
  7. *
  8. * 用于验证用户是否拥有足够数量的物品,支持普通物品和实例物品
  9. */
  10. class ItemQuantityValidation extends ValidationCore
  11. {
  12. /**
  13. * 物品ID
  14. *
  15. * @var int
  16. */
  17. public $item_id;
  18. /**
  19. * 用户ID
  20. *
  21. * @var int
  22. */
  23. public $user_id;
  24. /**
  25. * 物品数量
  26. *
  27. * @var int
  28. */
  29. public $quantity;
  30. /**
  31. * 实例ID
  32. *
  33. * @var int|null
  34. */
  35. public $instance_id;
  36. /**
  37. * 是否包含所有类型物品
  38. *
  39. * @var bool
  40. */
  41. public $include_all_types;
  42. /**
  43. * 验证规则
  44. *
  45. * @param array $rules 自定义规则
  46. * @return array
  47. */
  48. public function rules($rules = []): array
  49. {
  50. return [
  51. [
  52. 'item_id,user_id', 'required'
  53. ],
  54. [
  55. 'item_id,user_id', 'integer', 'min' => 1,
  56. 'msg' => '{attr}必须是大于0的整数'
  57. ],
  58. [
  59. 'quantity', 'integer', 'min' => 1,
  60. 'msg' => '物品数量必须是大于0的整数',
  61. 'skipOnEmpty' => true
  62. ],
  63. [
  64. 'instance_id', 'integer', 'min' => 1,
  65. 'msg' => '实例ID必须是大于0的整数',
  66. 'skipOnEmpty' => true
  67. ],
  68. [
  69. 'include_all_types', 'boolean',
  70. 'skipOnEmpty' => true
  71. ],
  72. [
  73. 'item_id', new ItemQuantityValidator($this, ['user_id', 'quantity', 'instance_id', 'include_all_types']),
  74. 'msg' => '物品数量验证失败'
  75. ]
  76. ];
  77. }
  78. /**
  79. * 设置默认值
  80. *
  81. * @return array
  82. */
  83. public function default(): array
  84. {
  85. return [
  86. 'quantity' => 1,
  87. 'instance_id' => null,
  88. 'include_all_types' => false
  89. ];
  90. }
  91. }