PetEatValidation.php 1.8 KB

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