FertilizerValidation.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Module\Farm\Validations;
  3. use App\Module\Farm\Validators\LandOwnershipValidator;
  4. use App\Module\Farm\Validators\FertilizerItemValidator;
  5. use App\Module\Farm\Validators\FertilizerUsageValidator;
  6. use App\Module\GameItems\Validators\ItemOwnershipValidator;
  7. use UCore\ValidationCore;
  8. /**
  9. * 施肥验证类
  10. *
  11. * 用于验证施肥操作的输入数据,包括用户ID、土地ID、肥料物品ID等
  12. */
  13. class FertilizerValidation extends ValidationCore
  14. {
  15. /** @var \App\Module\Farm\Models\FarmLand|null 土地对象,由 LandOwnershipValidator 设置 */
  16. public ?\App\Module\Farm\Models\FarmLand $land = null;
  17. /** @var \App\Module\GameItems\Models\Item|null 肥料物品对象,由 FertilizerItemValidator 设置 */
  18. public ?\App\Module\GameItems\Models\Item $fertilizer_item = null;
  19. /** @var \App\Module\Farm\Models\FarmCrop|null 作物对象,由 FertilizerUsageValidator 设置 */
  20. public ?\App\Module\Farm\Models\FarmCrop $crop = null;
  21. /** @var int 肥料减少的生长时间(秒),由 FertilizerItemValidator 设置 */
  22. public int $crop_growth_time = 0;
  23. /**
  24. * 验证规则
  25. *
  26. * @param array $rules 自定义规则
  27. * @return array
  28. */
  29. public function rules($rules = []): array
  30. {
  31. return [
  32. [
  33. 'user_id,land_id,item_id', 'required'
  34. ],
  35. [
  36. 'user_id,land_id,item_id', 'integer', 'min' => 1,
  37. 'msg' => '{attr}必须是大于0的整数'
  38. ],
  39. // 验证土地是否属于用户
  40. [
  41. 'land_id', new LandOwnershipValidator($this, ['user_id', 'land']),
  42. 'msg' => '土地不存在或不属于当前用户'
  43. ],
  44. // 验证用户是否拥有该物品
  45. [
  46. 'item_id', new ItemOwnershipValidator($this, ['user_id', 'instance_id', 'quantity']),
  47. 'msg' => '您没有该肥料物品'
  48. ],
  49. // 验证物品是否为肥料类型
  50. [
  51. 'item_id', new FertilizerItemValidator($this, ['fertilizer_item', 'crop_growth_time']),
  52. 'msg' => '该物品不是有效的肥料'
  53. ],
  54. // 验证是否可以使用肥料
  55. [
  56. 'land_id', new FertilizerUsageValidator($this, ['land', 'crop']),
  57. 'msg' => '当前土地状态或作物生长阶段不允许使用肥料'
  58. ]
  59. ];
  60. }
  61. /**
  62. * 设置默认值
  63. *
  64. * @return array
  65. */
  66. public function default(): array
  67. {
  68. return [
  69. 'instance_id' => 0,
  70. 'quantity' => 1
  71. ];
  72. }
  73. }