| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- namespace App\Module\GameItems\Validations;
- use App\Module\GameItems\Validators\DismantleItemValidator;
- use App\Module\GameItems\Validators\ItemOwnershipValidator;
- use UCore\ValidationCore;
- /**
- * 物品分解验证类
- *
- * 用于验证物品分解操作的输入数据,包括用户ID、物品ID和数量
- */
- class ItemDismantleValidation extends ValidationCore
- {
- /** @var \App\Module\GameItems\Models\Item|null 分解物品对象,由 DismantleItemValidator 设置 */
- public ?\App\Module\GameItems\Models\Item $dismantle_item = null;
- /**
- * 验证规则
- *
- * @param array $rules 自定义规则
- * @return array
- */
- public function rules($rules = []): array
- {
- return [
- [
- 'user_id,item_id', 'required'
- ],
- [
- 'user_id,item_id,quantity', 'integer', 'min' => 1,
- 'msg' => '{attr}必须是大于0的整数'
- ],
- [
- 'instance_id', 'integer', 'min' => 0,
- 'msg' => '{attr}必须是大于等于0的整数'
- ],
- // 验证物品是否可分解
- [
- 'item_id', new DismantleItemValidator($this, ['dismantle_item']),
- 'msg' => '物品验证失败'
- ],
- // 验证用户是否拥有该物品
- [
- 'item_id', new ItemOwnershipValidator($this, ['user_id', 'instance_id', 'quantity']),
- 'msg' => '物品不存在或数量不足'
- ]
- ];
- }
- /**
- * 设置默认值
- *
- * @return array
- */
- public function default(): array
- {
- return [
- 'instance_id' => 0,
- 'quantity' => 1
- ];
- }
- }
|