| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- <?php
- namespace App\Module\Farm\Validators;
- use UCore\Validation\BaseValidator;
- /**
- * 摘取来源验证器
- * 验证摘取来源信息
- */
- class PickSourceValidator extends BaseValidator
- {
- // 允许的摘取来源类型
- private const ALLOWED_SOURCES = [
- 'manual', // 手动摘取
- 'friend_visit', // 好友访问摘取
- 'system_auto', // 系统自动摘取
- 'task_reward', // 任务奖励摘取
- 'event_bonus', // 活动奖励摘取
- ];
- /**
- * 验证摘取来源
- *
- * @param string $pickSource 摘取来源
- * @param int|null $sourceId 来源ID
- * @return bool
- */
- public function validate(string $pickSource, ?int $sourceId = null): bool
- {
- // 验证摘取来源字符串格式
- if (empty($pickSource)) {
- $this->addError('摘取来源不能为空');
- return false;
- }
- if (!in_array($pickSource, self::ALLOWED_SOURCES)) {
- $allowedSources = implode(', ', self::ALLOWED_SOURCES);
- $this->addError("无效的摘取来源: {$pickSource},允许的来源: {$allowedSources}");
- return false;
- }
- // 检查来源ID的有效性(如果提供)
- if ($sourceId !== null && $sourceId <= 0) {
- $this->addError('来源ID必须大于0');
- return false;
- }
- // 某些来源类型需要提供来源ID
- $requireSourceId = ['friend_visit', 'task_reward', 'event_bonus'];
- if (in_array($pickSource, $requireSourceId) && $sourceId === null) {
- $this->addError("摘取来源 {$pickSource} 需要提供来源ID");
- return false;
- }
- return true;
- }
- /**
- * 获取允许的摘取来源列表
- *
- * @return array
- */
- public static function getAllowedSources(): array
- {
- return self::ALLOWED_SOURCES;
- }
- }
|