fix: Return a void array when failing to parse terms instead of null

This commit is contained in:
Dendy 2025-02-06 20:09:06 +09:00
parent c19250e29e
commit 0ac5d035a2
2 changed files with 6 additions and 4 deletions

View File

@ -70,7 +70,7 @@ class Note
$terms = Term::fromNoteFields($note->fields);
// If not defined, find them from the highlighted parts in the sentence
if (empty($note->terms)) {
if (empty($terms)) {
// 1. Get all spans in the text
preg_match_all(
self::HIGHLIGHT_PATTERN,
@ -90,6 +90,8 @@ class Note
}
}
if (empty($terms)) dd($note);
// Set to null whatever is null
$readings = array_map(
fn($x) => in_array($x, ['_', '_', '']) ? null : $x,

View File

@ -139,7 +139,7 @@ class Term
return Term::fromVocabDefLine($kanji . $separator . $def);
}
public static function fromNoteFields(array $fields): ?array
public static function fromNoteFields(array $fields): array
{
// -------------------- Trying to extract it with the modern syntax ---
// 言葉: word
@ -159,7 +159,7 @@ class Term
$kanjis = explode('', $fields['VocabKanji']);
$defs = explode('', $fields['VocabDef']);
// Number of legacy definitions is different from number of kanji
if (count($kanjis) !== count($defs)) return null;
if (count($kanjis) !== count($defs)) return [];
$terms = [];
foreach (array_combine($kanjis, $defs) as $kanji => $def) {
@ -167,6 +167,6 @@ class Term
}
// Search for nulls, if found, it's owari da
return in_array(null, $terms, true) ? null : $terms;
return in_array(null, $terms, true) ? [] : $terms;
}
}