|
@@ -121,4 +121,28 @@ class GameplayRuleEnum
|
|
|
]
|
|
|
|
|
|
];
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证并解析用户输入
|
|
|
+ * @param string $input 例如 "大单100" / "5操20"
|
|
|
+ * @return array|null 返回 ['rule' => 玩法, 'amount' => 金额],失败返回 null
|
|
|
+ */
|
|
|
+ public static function validateInput(string $input): ?array
|
|
|
+ {
|
|
|
+ // 获取所有玩法
|
|
|
+ $allRules = array_merge(...self::$RULES);
|
|
|
+
|
|
|
+ // 玩法正则(防止玩法里有特殊符号出错)
|
|
|
+ $rulesPattern = implode('|', array_map('preg_quote', $allRules));
|
|
|
+
|
|
|
+ // 匹配玩法 + 金额
|
|
|
+ if (preg_match('/^(' . $rulesPattern . ')(\d+)$/u', $input, $matches)) {
|
|
|
+ return [
|
|
|
+ 'rule' => $matches[1],
|
|
|
+ 'amount' => (int)$matches[2]
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ return null;
|
|
|
+ }
|
|
|
}
|