chore: Treat not found noteInfos as NULL to prevent errors

This commit is contained in:
Dendy 2025-02-06 15:21:51 +09:00
parent ea472e6afb
commit e8fefa064f
1 changed files with 12 additions and 4 deletions

View File

@ -38,12 +38,22 @@ class AnkiService
); );
} }
/** Give an array of IDs, the note Infos are returned. if info for a given
* doesn't exist, it is assigned to null instead of the default [].
*/
public function getNotesInfo(array $noteIds): array public function getNotesInfo(array $noteIds): array
{ {
return $this->request('notesInfo', ['notes' => $noteIds]); $noteInfos = $this->request('notesInfo', ['notes' => $noteIds]);
foreach ($noteInfos as &$noteInfo) {
if ([] === $noteInfo) $noteInfo = null;
} }
public function getNoteInfo(int $noteId): array return $noteInfos;
}
/** Returns info form note given an ID, returns null if it doesn't exist */
public function getNoteInfo(int $noteId): ?array
{ {
return $this->getNotesInfo([$noteId])[0]; return $this->getNotesInfo([$noteId])[0];
} }
@ -61,9 +71,7 @@ class AnkiService
public function updateNote(Note $note) public function updateNote(Note $note)
{ {
$this->request('guiBrowse', ['query' => 'nid:1']); $this->request('guiBrowse', ['query' => 'nid:1']);
$this->request('updateNoteFields', ['note' => $note->toAnki()]); $this->request('updateNoteFields', ['note' => $note->toAnki()]);
$this->request('guiBrowse', ['query' => 'nid:' . $note->getId()]); $this->request('guiBrowse', ['query' => 'nid:' . $note->getId()]);
} }
} }