ShopBuyValidation.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App\Module\Shop\Validations;
  3. use App\Module\Shop\Validators\ShopItemValidator;
  4. use App\Module\Shop\Validators\ShopBuyLimitValidator;
  5. use App\Module\Shop\Validators\ShopFundValidator;
  6. use UCore\ValidationCore;
  7. /**
  8. * 商店购买验证类
  9. *
  10. * 用于验证商店购买操作的输入数据,包括用户ID、商品ID和购买数量
  11. */
  12. class ShopBuyValidation extends ValidationCore
  13. {
  14. /**
  15. * 验证规则
  16. *
  17. * @param array $rules 自定义规则
  18. * @return array
  19. */
  20. public function rules($rules = []): array
  21. {
  22. return [
  23. [
  24. 'user_id,good_id,number', 'required'
  25. ],
  26. [
  27. 'user_id,good_id,number', 'integer', 'min' => 1,
  28. 'msg' => '{attr}必须是大于0的整数'
  29. ],
  30. // 验证商品是否存在且可购买
  31. [
  32. 'good_id', new ShopItemValidator($this, ['shop_item']),
  33. 'msg' => '商品验证失败'
  34. ],
  35. // 验证购买限制
  36. [
  37. 'good_id', new ShopBuyLimitValidator($this, ['user_id', 'number', 'shop_item']),
  38. 'msg' => '购买限制验证失败'
  39. ],
  40. // 验证用户资金是否充足
  41. [
  42. 'good_id', new ShopFundValidator($this, ['user_id', 'number', 'shop_item']),
  43. 'msg' => '资金不足'
  44. ]
  45. ];
  46. }
  47. /**
  48. * 设置默认值
  49. *
  50. * @return array
  51. */
  52. public function default(): array
  53. {
  54. return [];
  55. }
  56. }