PetEatValidation.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Module\AppGame\Validations;
  3. use App\Module\Pet\Enums\PetStatus;
  4. use App\Module\Pet\Validators\PetExistsValidator;
  5. use App\Module\Pet\Validators\PetFoodValidator;
  6. use App\Module\Pet\Validators\PetStatusValidator;
  7. use App\Module\GameItems\Validators\ItemQuantityValidator;
  8. /**
  9. * 宠物喂食验证类
  10. *
  11. * 用于验证宠物喂食请求的参数
  12. */
  13. class PetEatValidation extends ValidationBase
  14. {
  15. /**
  16. * 用户ID
  17. *
  18. * @var int
  19. */
  20. public $user_id;
  21. /**
  22. * 验证规则
  23. *
  24. * @param array $rules 自定义规则
  25. * @return array
  26. */
  27. public function rules($rules = []): array
  28. {
  29. return [
  30. [
  31. 'pet_id,item_id,num,user_id', 'required'
  32. ],
  33. [
  34. 'pet_id,item_id,num', 'integer', 'min' => 1,
  35. 'msg' => '{attr}必须是大于0的整数'
  36. ],
  37. // 验证宠物是否存在
  38. [
  39. 'pet_id', new PetExistsValidator($this),
  40. 'msg' => '宠物不存在或不属于当前用户'
  41. ],
  42. // 验证宠物状态是否正常
  43. [
  44. 'pet_id', new PetStatusValidator($this, [ PetStatus::NORMAL ]),
  45. 'msg' => '宠物状态不允许喂食'
  46. ],
  47. // 验证物品是否为宠物口粮
  48. [
  49. 'item_id', new PetFoodValidator($this),
  50. 'msg' => '该物品不是宠物口粮'
  51. ],
  52. // 验证物品数量是否足够
  53. [
  54. 'item_id', new ItemQuantityValidator($this, ['user_id', 'num']),
  55. 'msg' => '物品数量不足'
  56. ]
  57. ];
  58. }
  59. /**
  60. * 设置默认值
  61. *
  62. * @return array
  63. */
  64. public function default(): array
  65. {
  66. return [
  67. 'num' => 1
  68. ];
  69. }
  70. }