LandUpgradeValidation.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Module\Farm\Validations;
  3. use App\Module\Farm\Validators\LandOwnershipValidator;
  4. use App\Module\Farm\Validators\LandUpgradePathValidator;
  5. use App\Module\Farm\Validators\LandUpgradeMaterialsValidator;
  6. use App\Module\Farm\Validators\LandUpgradeStatusValidator;
  7. use App\Module\Farm\Validators\LandUpgradeHouseLevelValidator;
  8. use App\Module\Farm\Validators\LandUpgradeSpecialLimitValidator;
  9. use UCore\ValidationCore;
  10. /**
  11. * 土地升级验证类
  12. *
  13. * 用于验证土地升级操作的输入数据,包括用户ID、土地ID和目标类型
  14. *
  15. */
  16. class LandUpgradeValidation extends ValidationCore
  17. {
  18. /** @var \App\Module\Farm\Models\FarmLand|null 土地对象,由 LandOwnershipValidator 设置 */
  19. public ?\App\Module\Farm\Models\FarmLand $land = null;
  20. /** @var \App\Module\Farm\Models\FarmLandUpgradeConfig|null 升级配置,由 LandUpgradePathValidator 设置 */
  21. public ?\App\Module\Farm\Models\FarmLandUpgradeConfig $upgrade_config = null;
  22. /**
  23. * 验证规则
  24. *
  25. * @param array $rules 自定义规则
  26. * @return array
  27. */
  28. public function rules($rules = []): array
  29. {
  30. return [
  31. [
  32. 'user_id,land_id', 'required'
  33. ],
  34. [
  35. 'user_id,land_id', 'integer', 'min' => 1,
  36. 'msg' => '{attr}必须是大于0的整数'
  37. ],
  38. // 验证土地是否属于用户
  39. [
  40. 'land_id', new LandOwnershipValidator($this, ['user_id', 'land']),
  41. 'msg' => '土地不存在或不属于当前用户'
  42. ],
  43. // 验证土地状态是否允许升级
  44. [
  45. 'land_id', new LandUpgradeStatusValidator($this, ['land']),
  46. 'msg' => '土地状态不允许升级'
  47. ],
  48. // 验证升级路径是否可用
  49. [
  50. 'land_id', new LandUpgradePathValidator($this, ['user_id', 'upgrade_config']),
  51. 'msg' => '升级路径验证失败'
  52. ],
  53. // 验证房屋等级是否满足要求
  54. [
  55. 'land_id', new LandUpgradeHouseLevelValidator($this, ['user_id', 'upgrade_config']),
  56. 'msg' => '房屋等级不足'
  57. ],
  58. // 验证特殊土地数量限制
  59. [
  60. 'land_id', new LandUpgradeSpecialLimitValidator($this, ['user_id', 'upgrade_config']),
  61. 'msg' => '特殊土地数量已达上限'
  62. ],
  63. // 验证升级材料是否足够
  64. [
  65. 'land_id', new LandUpgradeMaterialsValidator($this, ['user_id', 'upgrade_config']),
  66. 'msg' => '升级材料不足'
  67. ]
  68. ];
  69. }
  70. /**
  71. * 设置默认值
  72. *
  73. * @return array
  74. */
  75. public function default(): array
  76. {
  77. return [];
  78. }
  79. }