| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <?php
- namespace App\Module\Farm\Validations;
- use App\Module\Farm\Validators\LandOwnershipValidator;
- use App\Module\Farm\Validators\LandStatusValidator;
- use App\Module\Farm\Validators\SeedItemValidator;
- use App\Module\Farm\Validators\SeedOwnershipValidator;
- use UCore\ValidationCore;
- /**
- * 作物种植验证类
- *
- * 用于验证作物种植操作的输入数据,包括用户ID、土地ID、种子ID等
- */
- class CropPlantValidation extends ValidationCore
- {
- /** @var \App\Module\Farm\Models\FarmLand|null 土地对象,由 LandOwnershipValidator 设置 */
- public ?\App\Module\Farm\Models\FarmLand $land = null;
- /** @var \App\Module\GameItems\Models\Item|null 种子物品对象,由 SeedItemValidator 设置 */
- public ?\App\Module\GameItems\Models\Item $seed_item = null;
- /**
- * 验证规则
- *
- * @param array $rules 自定义规则
- * @return array
- */
- public function rules($rules = []): array
- {
- return [
- [
- 'user_id,land_id,item_id', 'required'
- ],
- [
- 'user_id,land_id,item_id', 'integer', 'min' => 1,
- 'msg' => '{attr}必须是大于0的整数'
- ],
- [
- 'item_instance_id', 'integer', 'min' => 0,
- 'msg' => '{attr}必须是大于等于0的整数'
- ],
- // 验证土地是否属于用户
- [
- 'land_id', new LandOwnershipValidator($this, ['user_id', 'land']),
- 'msg' => '土地不存在或不属于当前用户'
- ],
- // 验证土地状态是否允许种植
- [
- 'land_id', new LandStatusValidator($this, ['land']),
- 'msg' => '土地状态不允许种植'
- ],
- // 验证物品是否为种子类型
- [
- 'item_id', new SeedItemValidator($this, ['seed_item']),
- 'msg' => '物品不是种子类型'
- ],
- // 验证用户是否拥有该种子
- [
- 'item_id', new SeedOwnershipValidator($this, ['user_id', 'item_instance_id']),
- 'msg' => '您没有该种子'
- ]
- ];
- }
- /**
- * 设置默认值
- *
- * @return array
- */
- public function default(): array
- {
- return [
- 'item_instance_id' => 0
- ];
- }
- }
|