| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- namespace App\Module\GameItems\Validation;
- use App\Module\GameItems\Validators\ItemQuantityValidator;
- use UCore\ValidationCore;
- /**
- * 物品数量验证类
- *
- * 用于验证用户是否拥有足够数量的物品,支持普通物品和实例物品
- */
- class ItemQuantityValidation extends ValidationCore
- {
- /**
- * 物品ID
- *
- * @var int
- */
- public $item_id;
- /**
- * 用户ID
- *
- * @var int
- */
- public $user_id;
- /**
- * 物品数量
- *
- * @var int
- */
- public $quantity;
- /**
- * 实例ID
- *
- * @var int|null
- */
- public $instance_id;
- /**
- * 是否包含所有类型物品
- *
- * @var bool
- */
- public $include_all_types;
- /**
- * 验证规则
- *
- * @param array $rules 自定义规则
- * @return array
- */
- public function rules($rules = []): array
- {
- return [
- [
- 'item_id,user_id', 'required'
- ],
- [
- 'item_id,user_id', 'integer', 'min' => 1,
- 'msg' => '{attr}必须是大于0的整数'
- ],
- [
- 'quantity', 'integer', 'min' => 1,
- 'msg' => '物品数量必须是大于0的整数',
- 'skipOnEmpty' => true
- ],
- [
- 'instance_id', 'integer', 'min' => 1,
- 'msg' => '实例ID必须是大于0的整数',
- 'skipOnEmpty' => true
- ],
- [
- 'include_all_types', 'boolean',
- 'skipOnEmpty' => true
- ],
- [
- 'item_id', new ItemQuantityValidator($this, ['user_id', 'quantity', 'instance_id', 'include_all_types']),
- 'msg' => '物品数量验证失败'
- ]
- ];
- }
- /**
- * 设置默认值
- *
- * @return array
- */
- public function default(): array
- {
- return [
- 'quantity' => 1,
- 'instance_id' => null,
- 'include_all_types' => false
- ];
- }
- }
|