diff options
Diffstat (limited to 'src/DTS/BookmarkRepository.php')
| -rw-r--r-- | src/DTS/BookmarkRepository.php | 70 |
1 files changed, 42 insertions, 28 deletions
diff --git a/src/DTS/BookmarkRepository.php b/src/DTS/BookmarkRepository.php index d673338..5d22100 100644 --- a/src/DTS/BookmarkRepository.php +++ b/src/DTS/BookmarkRepository.php @@ -17,30 +17,8 @@ class BookmarkRepository implements \Iterator function __construct(string $pathToRepository) { $this->pathToRepository = $pathToRepository; - } - - public function load(): void - { - if (!is_file($this->pathToRepository) || !is_readable($this->pathToRepository)) { - throw new \Exception("Unable to locate repository {$this->pathToRepository}"); - } - - if (($fp = fopen($this->pathToRepository, 'r')) === FALSE) { - throw new \Exception("Unable to read from repository {$this->pathToRepository}"); - } - - while (($data = fgetcsv($fp)) !== FALSE) { - $this->repository[] = new Bookmark( - $data[0], // Id. - $data[1], // Url. - $data[2], // Title. - $data[3], // Tag. - $data[4], // Added At. - (bool)$data[4], // Read. - ); - } - fclose($fp); + $this->load(); } public function sort(bool $asc = true): BookmarkRepository @@ -67,14 +45,14 @@ class BookmarkRepository implements \Iterator { $index = $this->findBookmarkIndex($id); - return $this->repository[$index] ?? null; + return $this->repository[$index] ?? null; } public function add(Bookmark $bookmark): bool { $this->repository[] = $bookmark; - return $this->store(); + return $this->save(); } public function update(Bookmark $bookmark): bool @@ -83,9 +61,21 @@ class BookmarkRepository implements \Iterator if ($index !== null) { $this->repository[$index] = $bookmark; - print_r($bookmark); - return $this->store(); + return $this->save(); + } + + return false; + } + + public function delete(Bookmark $bookmark): bool + { + $index = $this->findBookmarkIndex($bookmark->id); + + if ($index !== null) { + unset($this->repository[$index]); + + return $this->save(); } return false; @@ -127,7 +117,31 @@ class BookmarkRepository implements \Iterator return null; } - public function store(): bool + private function load(): void + { + if (!is_file($this->pathToRepository) || !is_readable($this->pathToRepository)) { + throw new \Exception("Unable to locate repository {$this->pathToRepository}"); + } + + if (($fp = fopen($this->pathToRepository, 'r')) === FALSE) { + throw new \Exception("Unable to read from repository {$this->pathToRepository}"); + } + + while (($data = fgetcsv($fp)) !== FALSE) { + $this->repository[] = new Bookmark( + $data[0], // Id. + $data[1], // Url. + $data[2], // Title. + $data[3], // Tag. + $data[4], // Added At. + (bool)$data[4], // Read. + ); + } + + fclose($fp); + } + + private function save(): bool { if (($fp = fopen($this->pathToRepository, 'w')) === FALSE) { throw new \Exception("Unable to open repository {$this->pathToRepository}"); |
