CropPlantValidation.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace App\Module\Farm\Validations;
  3. use App\Module\Farm\Validators\LandOwnershipValidator;
  4. use App\Module\Farm\Validators\LandStatusValidator;
  5. use App\Module\Farm\Validators\SeedItemValidator;
  6. use App\Module\Farm\Validators\SeedOwnershipValidator;
  7. use UCore\ValidationCore;
  8. /**
  9. * 作物种植验证类
  10. *
  11. * 用于验证作物种植操作的输入数据,包括用户ID、土地ID、种子ID等
  12. */
  13. class CropPlantValidation 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 种子物品对象,由 SeedItemValidator 设置 */
  18. public ?\App\Module\GameItems\Models\Item $seed_item = null;
  19. /**
  20. * 验证规则
  21. *
  22. * @param array $rules 自定义规则
  23. * @return array
  24. */
  25. public function rules($rules = []): array
  26. {
  27. return [
  28. [
  29. 'user_id,land_id,item_id', 'required'
  30. ],
  31. [
  32. 'user_id,land_id,item_id', 'integer', 'min' => 1,
  33. 'msg' => '{attr}必须是大于0的整数'
  34. ],
  35. [
  36. 'item_instance_id', 'integer', 'min' => 0,
  37. 'msg' => '{attr}必须是大于等于0的整数'
  38. ],
  39. // 验证土地是否属于用户
  40. [
  41. 'land_id', new LandOwnershipValidator($this, ['user_id', 'land']),
  42. 'msg' => '土地不存在或不属于当前用户'
  43. ],
  44. // 验证土地状态是否允许种植
  45. [
  46. 'land_id', new LandStatusValidator($this, ['land']),
  47. 'msg' => '土地状态不允许种植'
  48. ],
  49. // 验证物品是否为种子类型
  50. [
  51. 'item_id', new SeedItemValidator($this, ['seed_item']),
  52. 'msg' => '物品不是种子类型'
  53. ],
  54. // 验证用户是否拥有该种子
  55. [
  56. 'item_id', new SeedOwnershipValidator($this, ['user_id', 'item_instance_id']),
  57. 'msg' => '您没有该种子'
  58. ]
  59. ];
  60. }
  61. /**
  62. * 设置默认值
  63. *
  64. * @return array
  65. */
  66. public function default(): array
  67. {
  68. return [
  69. 'item_instance_id' => 0
  70. ];
  71. }
  72. }