71 lines
1.6 KiB
PHP
71 lines
1.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\common\service;
|
|
|
|
/**
|
|
* 分流链接自动回复语句(一行一条)
|
|
*/
|
|
class SplitAutoReplyService
|
|
{
|
|
/** 最多条数 */
|
|
private const MAX_LINES = 200;
|
|
|
|
/** 单条最大字符数 */
|
|
private const MAX_LINE_LENGTH = 500;
|
|
|
|
/**
|
|
* 解析为多行文本数组
|
|
*
|
|
* @return array<int, string>
|
|
*/
|
|
public static function parseLines(string $raw): array
|
|
{
|
|
$raw = trim($raw);
|
|
if ($raw === '') {
|
|
return [];
|
|
}
|
|
$parts = preg_split('/\r\n|\r|\n/', $raw) ?: [];
|
|
$lines = [];
|
|
foreach ($parts as $part) {
|
|
$line = trim((string) $part);
|
|
if ($line === '') {
|
|
continue;
|
|
}
|
|
if (strlen($line) > self::MAX_LINE_LENGTH) {
|
|
$line = mb_substr($line, 0, self::MAX_LINE_LENGTH, 'UTF-8');
|
|
}
|
|
$lines[] = $line;
|
|
if (count($lines) >= self::MAX_LINES) {
|
|
break;
|
|
}
|
|
}
|
|
return $lines;
|
|
}
|
|
|
|
/**
|
|
* 格式化为数据库存储(换行分隔)
|
|
*
|
|
* @param string|array<int, string> $value
|
|
*/
|
|
public static function formatStorage($value): string
|
|
{
|
|
if (is_array($value)) {
|
|
$lines = $value;
|
|
} else {
|
|
$lines = self::parseLines((string) $value);
|
|
}
|
|
return implode("\n", $lines);
|
|
}
|
|
|
|
/**
|
|
* 供表单 textarea 回显
|
|
*/
|
|
public static function formatDisplay(string $stored): string
|
|
{
|
|
$lines = self::parseLines($stored);
|
|
return implode("\n", $lines);
|
|
}
|
|
}
|