CropPlantValidation.php 2.6 KB

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