feat: Implement SentenceListeningNote creating based on Note

This commit is contained in:
Dendy 2025-04-09 08:21:37 +02:00
parent 605d65e468
commit 9093b1a0f8
3 changed files with 32 additions and 17 deletions

View File

@ -206,14 +206,7 @@ class CreateProductionCommand extends Command
));
$theChosenNote = $this->ankiService->getNote($noteIds[array_key_last($noteIds)]);
$fields = $theChosenNote->getFields();
$fields['VocabKanji'] = $theChosenTerm->getKanji();
$fields['VocabFurigana'] = $theChosenTerm->getReading();
$fields['VocabDef'] = $theChosenTerm->toAnkiVocabDef();
$fields['SentKanji'] = strip_tags($fields['SentKanji']);
$theChosenNote->setFields($fields);
dd($theChosenTerm->toAnkiVocabDef(), $theChosenNote->getFields());
dd(SentenceListeningNote::fromNote($theChosenNote, $theChosenTerm));
printf(
<<<FMNT

View File

@ -7,13 +7,13 @@ class Note
const HIGHLIGHT_PATTERN = '/<span\s+([^>]*)>(.*?)<\/span>/i';
const HIGHLIGHT_ATTR_KANJI = 'style="color: rgb(255, 78, 8);"';
private readonly int $id;
private readonly int $mod;
private readonly string $model;
private string $profile;
private array $cardIds = [];
protected array $fields = [];
private array $tags = [];
protected ?int $id;
protected ?int $mod;
protected string $model;
protected string $profile;
protected array $cardIds = [];
protected array $fields = [];
protected array $tags = [];
// -------------------------------------------------- Getters & setters ---

View File

@ -15,9 +15,12 @@ class SentenceListeningNote extends Note
{
return $this->term;
}
public function setTerm(Term $terms): static
public function setTerm(Term $term): static
{
$this->term = $terms;
$this->fields['VocabKanji'] = $term->getKanji();
$this->fields['VocabFurigana'] = $term->getReading();
$this->fields['VocabDef'] = $term->toAnkiVocabDef();
$this->term = $term;
return $this;
}
@ -43,6 +46,25 @@ class SentenceListeningNote extends Note
return $note;
}
public static function fromNote(Note $origNote, Term $term): static
{
$slNote = new static();
foreach (get_object_vars($origNote) as $prop => $value) {
$slNote->$prop = $value;
}
// Related fields are updated using the setter
$slNote->setTerm($term);
// Remove highlighting
$slNote->fields['SentKanji'] = strip_tags($slNote->fields['SentKanji']);
// Reset relations and basic data
$slNote->id = null;
$slNote->model = self::MODEL_NAME;
$slNote->cardIds = [];
return $slNote;
}
public function toAnki(): array
{
return $this->fields;