From 0f273c0a8ead390346571c4be2296fa6e384eff6 Mon Sep 17 00:00:00 2001 From: "David T. Sadler" Date: Thu, 7 Oct 2021 21:31:17 +0100 Subject: Add bookmarks --- src/DTS/BookmarkRepository.php | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) (limited to 'src/DTS/BookmarkRepository.php') diff --git a/src/DTS/BookmarkRepository.php b/src/DTS/BookmarkRepository.php index deb188c..ab54594 100644 --- a/src/DTS/BookmarkRepository.php +++ b/src/DTS/BookmarkRepository.php @@ -31,8 +31,9 @@ class BookmarkRepository implements \Iterator $this->repository[] = new Bookmark( $data[0], // Id. $data[1], // Url. - $data[2], // Tag. - $data[3], // Added At. + $data[2], // Title. + $data[3], // Tag. + $data[4], // Added At. (bool)$data[4], // Read. ); } @@ -60,6 +61,26 @@ class BookmarkRepository implements \Iterator return $this; } + public function add(Bookmark $bookmark): bool + { + if (($fp = fopen($this->pathToRepository, 'a')) === FALSE) { + throw new \Exception("Unable to open repository {$this->pathToRepository}"); + } + + $saved = fputcsv($fp, [ + $bookmark->id, + $bookmark->url, + $bookmark->title, + $bookmark->tag, + $bookmark->addedAt, + (int)$bookmark->unread, + ]); + + fclose($fp); + + return $saved !== false; + } + public function current(): mixed { return $this->repository[$this->position]; -- cgit v1.2.3-13-gbd6f