| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <?php
- namespace App\Module\GameItems\Validations;
- use App\Module\GameItems\Validators\CraftRecipeValidator;
- use App\Module\GameItems\Validators\CraftMaterialsValidator;
- use UCore\ValidationCore;
- /**
- * 物品合成验证类
- *
- * 用于验证物品合成操作的输入数据,包括用户ID、配方ID和数量
- */
- class ItemCraftValidation extends ValidationCore
- {
- /**
- * 验证规则
- *
- * @param array $rules 自定义规则
- * @return array
- */
- public function rules($rules = []): array
- {
- return [
- [
- 'user_id,recipe_id', 'required'
- ],
- [
- 'user_id,recipe_id,quantity', 'integer', 'min' => 1,
- 'msg' => '{attr}必须是大于0的整数'
- ],
- // 验证配方是否存在且可用
- [
- 'recipe_id', new CraftRecipeValidator($this, ['recipe']),
- 'msg' => '配方验证失败'
- ],
- // 验证合成材料是否充足
- [
- 'recipe_id', new CraftMaterialsValidator($this, ['user_id', 'quantity', 'recipe']),
- 'msg' => '合成材料不足'
- ]
- ];
- }
- /**
- * 设置默认值
- *
- * @return array
- */
- public function default(): array
- {
- return [
- 'quantity' => 1
- ];
- }
- }
|