| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- <?php
- namespace App\Module\GameItems\Validations;
- use App\Module\GameItems\Validators\ChestItemValidator;
- use App\Module\GameItems\Validators\ChestOwnershipValidator;
- use App\Module\GameItems\Validators\ChestOpenCostValidator;
- use UCore\ValidationCore;
- /**
- * 宝箱开启验证类
- *
- * 用于验证宝箱开启操作的输入数据,包括用户ID、宝箱ID和实例ID
- */
- class ChestOpenValidation extends ValidationCore
- {
- /**
- * 验证规则
- *
- * @param array $rules 自定义规则
- * @return array
- */
- public function rules($rules = []): array
- {
- return [
- [
- 'user_id,item_id', 'required'
- ],
- [
- 'user_id,item_id', 'integer', 'min' => 1,
- 'msg' => '{attr}必须是大于0的整数'
- ],
- [
- 'instance_id', 'integer', 'min' => 0,
- 'msg' => '{attr}必须是大于等于0的整数'
- ],
- // 验证物品是否为宝箱类型
- [
- 'item_id', new ChestItemValidator($this, ['chest_item']),
- 'msg' => '物品验证失败'
- ],
- // 验证用户是否拥有该宝箱
- [
- 'item_id', new ChestOwnershipValidator($this, ['user_id', 'instance_id']),
- 'msg' => '宝箱不存在或不属于当前用户'
- ],
- // 验证开启消耗是否充足
- [
- 'item_id', new ChestOpenCostValidator($this, ['user_id', 'quantity']),
- 'msg' => '开启消耗不足'
- ]
- ];
- }
- /**
- * 设置默认值
- *
- * @return array
- */
- public function default(): array
- {
- return [
- 'instance_id' => 0,
- 'quantity' => 1
- ];
- }
- }
|