chore: Move vocabDef parsing from Note to Term
This commit is contained in:
parent
ee2525797e
commit
6fc6307f6e
|
@ -67,7 +67,7 @@ class Note
|
||||||
$note->mediaInfo = $note->parseMediaInfo($note->fields['Notes']);
|
$note->mediaInfo = $note->parseMediaInfo($note->fields['Notes']);
|
||||||
|
|
||||||
// Set VocabKanji field
|
// Set VocabKanji field
|
||||||
$terms = self::parseVocabDef($note->fields['VocabDef']);
|
$terms = Term::fromVocabDef($note->fields['VocabDef']);
|
||||||
if (null !== $terms) {
|
if (null !== $terms) {
|
||||||
$note->terms = $terms;
|
$note->terms = $terms;
|
||||||
} else {
|
} else {
|
||||||
|
@ -97,7 +97,7 @@ class Note
|
||||||
. $separator
|
. $separator
|
||||||
. $note->fields['VocabDef'];
|
. $note->fields['VocabDef'];
|
||||||
|
|
||||||
$terms = self::parseVocabDef($note->fields['VocabDef']);
|
$terms = Term::fromVocabDef($note->fields['VocabDef']);
|
||||||
$note->terms = $terms ?? dd($note->fields['VocabDef']);
|
$note->terms = $terms ?? dd($note->fields['VocabDef']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,21 +161,6 @@ class Note
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function parseVocabDef(string $vocabDef): ?array
|
|
||||||
{
|
|
||||||
if (mb_trim($vocabDef) === '') return null;
|
|
||||||
|
|
||||||
$terms = [];
|
|
||||||
foreach (preg_split('|<br ?/?>|', $vocabDef) as $line) {
|
|
||||||
$term = Term::fromVocabDefLine(strip_tags($line));
|
|
||||||
// Error parsing term, can't parse using vocabDef
|
|
||||||
if (null === $term) return null;
|
|
||||||
$terms[] = $term;
|
|
||||||
};
|
|
||||||
|
|
||||||
return $terms;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function parseMediaInfo(string $notes): ?array
|
public function parseMediaInfo(string $notes): ?array
|
||||||
{
|
{
|
||||||
$matches = null;
|
$matches = null;
|
||||||
|
|
|
@ -49,7 +49,7 @@ class Term
|
||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fromVocabDefLine(string $vocabDefLine): ?Term
|
private static function fromVocabDefLine(string $vocabDefLine): ?Term
|
||||||
{
|
{
|
||||||
$term = new Term();
|
$term = new Term();
|
||||||
|
|
||||||
|
@ -126,4 +126,19 @@ class Term
|
||||||
|
|
||||||
dd("Unexpected error, couldn't parse definition line", $vocabDefLine);
|
dd("Unexpected error, couldn't parse definition line", $vocabDefLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function fromVocabDef(string $vocabDef): ?array
|
||||||
|
{
|
||||||
|
if (mb_trim($vocabDef) === '') return null;
|
||||||
|
|
||||||
|
$terms = [];
|
||||||
|
foreach (preg_split('|<br ?/?>|', $vocabDef) as $line) {
|
||||||
|
$term = self::fromVocabDefLine(strip_tags($line));
|
||||||
|
// Error parsing term, can't parse using vocabDef
|
||||||
|
if (null === $term) return null;
|
||||||
|
$terms[] = $term;
|
||||||
|
};
|
||||||
|
|
||||||
|
return $terms;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue