PetEatValidation.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. * 验证规则
  17. *
  18. * @param array $rules 自定义规则
  19. * @return array
  20. */
  21. public function rules($rules = []): array
  22. {
  23. return [
  24. [
  25. 'petId,itemId,num,user_id', 'required'
  26. ],
  27. [
  28. 'petId,itemId,num', 'integer', 'min' => 1,
  29. 'msg' => '{attr}必须是大于0的整数'
  30. ],
  31. // 验证宠物是否存在
  32. [
  33. 'petId', new PetExistsValidator($this),
  34. 'msg' => '宠物不存在或不属于当前用户'
  35. ],
  36. // 验证宠物状态是否正常
  37. [
  38. 'petId', new PetStatusValidator($this, [ PetStatus::NORMAL ]),
  39. 'msg' => '宠物状态不允许喂食'
  40. ],
  41. // 验证物品是否为宠物口粮
  42. [
  43. 'itemId', new PetFoodValidator($this),
  44. 'msg' => '该物品不是宠物口粮'
  45. ],
  46. // 验证物品数量是否足够
  47. [
  48. 'itemId', new ItemQuantityValidator($this, ['user_id', 'num']),
  49. 'msg' => '物品数量不足'
  50. ]
  51. ];
  52. }
  53. /**
  54. * 设置默认值
  55. *
  56. * @return array
  57. */
  58. public function default(): array
  59. {
  60. return [
  61. 'num' => 1
  62. ];
  63. }
  64. }