ItemQuantityValidation.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. /** @var int|null 物品ID */
  13. public ?int $item_id = null;
  14. /** @var int|null 用户ID */
  15. public ?int $user_id = null;
  16. /** @var int|null 物品数量 */
  17. public ?int $quantity = null;
  18. /** @var int|null 实例ID */
  19. public ?int $instance_id = null;
  20. /** @var bool|null 是否包含所有类型物品 */
  21. public ?bool $include_all_types = null;
  22. /**
  23. * 验证规则
  24. *
  25. * @param array $rules 自定义规则
  26. * @return array
  27. */
  28. public function rules($rules = []): array
  29. {
  30. return [
  31. [
  32. 'item_id,user_id', 'required'
  33. ],
  34. [
  35. 'item_id,user_id', 'integer', 'min' => 1,
  36. 'msg' => '{attr}必须是大于0的整数'
  37. ],
  38. [
  39. 'quantity', 'integer', 'min' => 1,
  40. 'msg' => '物品数量必须是大于0的整数',
  41. 'skipOnEmpty' => true
  42. ],
  43. [
  44. 'instance_id', 'integer', 'min' => 1,
  45. 'msg' => '实例ID必须是大于0的整数',
  46. 'skipOnEmpty' => true
  47. ],
  48. [
  49. 'include_all_types', 'boolean',
  50. 'skipOnEmpty' => true
  51. ],
  52. [
  53. 'item_id', new ItemQuantityValidator($this, ['user_id', 'quantity', 'instance_id', 'include_all_types']),
  54. 'msg' => '物品数量验证失败'
  55. ]
  56. ];
  57. }
  58. /**
  59. * 设置默认值
  60. *
  61. * @return array
  62. */
  63. public function default(): array
  64. {
  65. return [
  66. 'quantity' => 1,
  67. 'instance_id' => null,
  68. 'include_all_types' => false
  69. ];
  70. }
  71. }