Files
xin-procurement/app/Exports/SheetName.php
T
2026-08-17 18:13:33 +08:00

27 lines
801 B
PHP

<?php
namespace App\Exports;
/**
* Excel 工作表名合法化:剥离非法字符、31 字截断、重名追加 #id
*/
final class SheetName
{
/**
* @param string $name 原始名称(门店名/供应商名)
* @param int $id 实体ID(重名时追加)
* @param array<int, string> $used 已用名列表(引用传入,调用方维护)
*/
public static function make(string $name, int $id, array &$used): string
{
$base = str_replace(['[', ']', ':', '*', '?', '/', '\\'], '', $name);
$base = mb_substr($base === '' ? '未命名' : $base, 0, 28);
$title = $base;
if (in_array($title, $used, true)) {
$title = mb_substr($base, 0, 25) . '#' . $id;
}
$used[] = $title;
return $title;
}
}