mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-03-17 20:54:52 +08:00
Merge pull request #197 from zhamao-robot/catcode-add-text-options
新增 CatCode encode 时选择是否编码文本的参数
This commit is contained in:
commit
1fc329c670
@ -10,8 +10,11 @@ class CatCode
|
||||
{
|
||||
/**
|
||||
* 从 MessageSegment 转换为 CatCode 字符串
|
||||
*
|
||||
* @param array|MessageSegment|string $message_segment MessageSegment 对象或数组
|
||||
* @param bool $encode_text 是否对文本进行 CatCode 编码(默认为否)
|
||||
*/
|
||||
public static function fromSegment(array|MessageSegment|string $message_segment): string
|
||||
public static function fromSegment(string|array|MessageSegment $message_segment, bool $encode_text = false): string
|
||||
{
|
||||
// 传入的必须是段数组或段对象
|
||||
if (is_array($message_segment)) {
|
||||
@ -20,30 +23,28 @@ class CatCode
|
||||
if (!$v instanceof MessageSegment) {
|
||||
return '';
|
||||
}
|
||||
$str .= self::segment2CatCode($v);
|
||||
$str .= self::segment2CatCode($v, $encode_text);
|
||||
}
|
||||
return $str;
|
||||
}
|
||||
|
||||
if ($message_segment instanceof MessageSegment) {
|
||||
return self::segment2CatCode($message_segment);
|
||||
return self::segment2CatCode($message_segment, $encode_text);
|
||||
}
|
||||
|
||||
return $message_segment;
|
||||
}
|
||||
|
||||
/**
|
||||
* 转义CatCode的特殊字符
|
||||
*
|
||||
* @param int|string|\Stringable $msg 字符串
|
||||
* @param bool $is_content 如果是转义CatCode本体内容,则为false(默认),如果是参数内的字符串,则为true
|
||||
* @param int|string|\Stringable $msg 字符串
|
||||
* @param bool $is_param 如果是转义CatCode本体内容,则为false(默认),如果是参数内的字符串,则为true
|
||||
* @return string 转义后的CatCode
|
||||
*/
|
||||
public static function encode(\Stringable|int|string $msg, bool $is_content = false): string
|
||||
public static function encode(\Stringable|int|string $msg, bool $is_param = false): string
|
||||
{
|
||||
$msg = str_replace(['&', '[', ']'], ['&', '[', ']'], (string) $msg);
|
||||
if ($is_content) {
|
||||
$msg = str_replace(',', ',', $msg);
|
||||
if ($is_param) {
|
||||
$msg = str_replace([',', '=', "\r", "\n", "\t"], [',', '=', ' ', ' ', '	'], $msg);
|
||||
}
|
||||
return $msg;
|
||||
}
|
||||
@ -51,15 +52,15 @@ class CatCode
|
||||
/**
|
||||
* 反转义字符串中的CatCode敏感符号
|
||||
*
|
||||
* @param int|string|\Stringable $msg 字符串
|
||||
* @param bool $is_content 如果是解码CatCode本体内容,则为false(默认),如果是参数内的字符串,则为true
|
||||
* @param int|string|\Stringable $msg 字符串
|
||||
* @param bool $is_param 如果是解码CatCode本体内容,则为false(默认),如果是参数内的字符串,则为true
|
||||
* @return string 转义后的CatCode
|
||||
*/
|
||||
public static function decode(\Stringable|int|string $msg, bool $is_content = false): string
|
||||
public static function decode(\Stringable|int|string $msg, bool $is_param = false): string
|
||||
{
|
||||
$msg = str_replace(['&', '[', ']'], ['&', '[', ']'], (string) $msg);
|
||||
if ($is_content) {
|
||||
$msg = str_replace(',', ',', $msg);
|
||||
if ($is_param) {
|
||||
$msg = str_replace([',', '=', ' ', ' ', '	'], [',', '=', "\r", "\n", "\t"], $msg);
|
||||
}
|
||||
return $msg;
|
||||
}
|
||||
@ -67,11 +68,12 @@ class CatCode
|
||||
/**
|
||||
* 转换一个 Segment 为 CatCode
|
||||
*
|
||||
* @param MessageSegment $segment 段对象
|
||||
* @param MessageSegment $segment 段对象
|
||||
* @param bool $encode_text 是否对文本进行CatCode转义
|
||||
*/
|
||||
private static function segment2CatCode(MessageSegment $segment): string
|
||||
private static function segment2CatCode(MessageSegment $segment, bool $encode_text = false): string
|
||||
{
|
||||
if ($segment->type === 'text') {
|
||||
if (!$encode_text && $segment->type === 'text') {
|
||||
return $segment->data['text'];
|
||||
}
|
||||
$str = '[CatCode:' . $segment->type;
|
||||
|
||||
@ -15,10 +15,11 @@ class MessageUtil
|
||||
* 将消息段无损转换为 CatCode 字符串
|
||||
*
|
||||
* @param array $message_segment 消息段
|
||||
* @param bool $encode_text 是否对文本进行 CatCode 转义
|
||||
*/
|
||||
public static function arrayToStr(array $message_segment): string
|
||||
public static function arrayToStr(array $message_segment, bool $encode_text = false): string
|
||||
{
|
||||
return CatCode::fromSegment($message_segment);
|
||||
return CatCode::fromSegment($message_segment, $encode_text);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user