对搜索返回的消息做中英双版本
This commit is contained in:
@@ -168,43 +168,43 @@ final class ZiHuaDictionary
|
||||
public static function prepareItemsForSave(array $items): array
|
||||
{
|
||||
if (count($items) !== 36) {
|
||||
throw new InvalidArgumentException('必须恰好 36 条');
|
||||
throw new InvalidArgumentException('Must be exactly 36 items');
|
||||
}
|
||||
$nos = [];
|
||||
$out = [];
|
||||
foreach ($items as $idx => $row) {
|
||||
if (!is_array($row)) {
|
||||
throw new InvalidArgumentException('第 ' . ($idx + 1) . ' 行格式错误');
|
||||
throw new InvalidArgumentException('Row format error');
|
||||
}
|
||||
$no = $row['no'] ?? null;
|
||||
if (!is_numeric($no)) {
|
||||
throw new InvalidArgumentException('编号必须为数字');
|
||||
throw new InvalidArgumentException('No must be numeric');
|
||||
}
|
||||
$n = intval($no, 10);
|
||||
if ($n < 1 || $n > 36) {
|
||||
throw new InvalidArgumentException('编号必须在 1–36');
|
||||
throw new InvalidArgumentException('No must be between 1 and 36');
|
||||
}
|
||||
if (isset($nos[$n])) {
|
||||
throw new InvalidArgumentException('编号重复:' . $n);
|
||||
throw new InvalidArgumentException('Duplicate no');
|
||||
}
|
||||
$nos[$n] = true;
|
||||
|
||||
$name = $row['name'] ?? '';
|
||||
if (!is_string($name) || trim($name) === '') {
|
||||
throw new InvalidArgumentException('编号 ' . $n . ' 名称不能为空');
|
||||
throw new InvalidArgumentException('Name is required');
|
||||
}
|
||||
|
||||
$cat = $row['category'] ?? '';
|
||||
$catTrim = is_string($cat) ? trim($cat) : '';
|
||||
if (!in_array($catTrim, self::CATEGORIES, true)) {
|
||||
throw new InvalidArgumentException('编号 ' . $n . ' 类型无效');
|
||||
throw new InvalidArgumentException('Category is invalid');
|
||||
}
|
||||
|
||||
$out[] = ['no' => $n, 'name' => trim($name), 'category' => $catTrim];
|
||||
}
|
||||
for ($i = 1; $i <= 36; $i++) {
|
||||
if (!isset($nos[$i])) {
|
||||
throw new InvalidArgumentException('缺少编号:' . $i);
|
||||
throw new InvalidArgumentException('Missing no');
|
||||
}
|
||||
}
|
||||
usort($out, static fn (array $a, array $b): int => $a['no'] <=> $b['no']);
|
||||
@@ -219,7 +219,7 @@ final class ZiHuaDictionary
|
||||
usort($items, static fn (array $a, array $b): int => $a['no'] <=> $b['no']);
|
||||
$encoded = json_encode($items, JSON_UNESCAPED_UNICODE);
|
||||
if ($encoded === false) {
|
||||
throw new InvalidArgumentException('JSON 编码失败');
|
||||
throw new InvalidArgumentException('JSON encode failed');
|
||||
}
|
||||
return $encoded;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user