From e8fefa064f508e37c79947c71057eed247ce110f Mon Sep 17 00:00:00 2001 From: Dendy Faist Date: Thu, 6 Feb 2025 15:21:51 +0900 Subject: [PATCH] chore: Treat not found noteInfos as NULL to prevent errors --- src/Service/AnkiService.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Service/AnkiService.php b/src/Service/AnkiService.php index 37b4480..cb369c0 100644 --- a/src/Service/AnkiService.php +++ b/src/Service/AnkiService.php @@ -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 { - return $this->request('notesInfo', ['notes' => $noteIds]); + $noteInfos = $this->request('notesInfo', ['notes' => $noteIds]); + + foreach ($noteInfos as &$noteInfo) { + if ([] === $noteInfo) $noteInfo = null; + } + + return $noteInfos; } - public function getNoteInfo(int $noteId): array + /** 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]; } @@ -61,9 +71,7 @@ class AnkiService public function updateNote(Note $note) { $this->request('guiBrowse', ['query' => 'nid:1']); - $this->request('updateNoteFields', ['note' => $note->toAnki()]); - $this->request('guiBrowse', ['query' => 'nid:' . $note->getId()]); } }