summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDavid T. Sadler <davidtsadler@googlemail.com>2021-11-02 20:30:56 +0000
committerDavid T. Sadler <davidtsadler@googlemail.com>2021-11-02 20:30:56 +0000
commit5add8d18e6f984e0e3296a7e092a6a1ef061668f (patch)
tree01680a014934f6bf8be9f0d2432a3ff6a1eac765 /src
parent558959d4d7dcceff000fd5861f2f46451ebbd8a9 (diff)
Remove id from file
Diffstat (limited to 'src')
-rw-r--r--src/DTS/Bookmark.php2
-rw-r--r--src/DTS/BookmarkRepository.php40
2 files changed, 13 insertions, 29 deletions
diff --git a/src/DTS/Bookmark.php b/src/DTS/Bookmark.php
index 479b575..ef14826 100644
--- a/src/DTS/Bookmark.php
+++ b/src/DTS/Bookmark.php
@@ -6,7 +6,7 @@ namespace DTS;
class Bookmark
{
- public string $id;
+ public int $id;
public string $url = '';
diff --git a/src/DTS/BookmarkRepository.php b/src/DTS/BookmarkRepository.php
index a2d80cd..01f6de4 100644
--- a/src/DTS/BookmarkRepository.php
+++ b/src/DTS/BookmarkRepository.php
@@ -43,9 +43,7 @@ class BookmarkRepository implements \Iterator
public function find(string $id): ?Bookmark
{
- $index = $this->findBookmarkIndex($id);
-
- return $this->repository[$index] ?? null;
+ return $this->repository[$id] ?? null;
}
public function add(Bookmark $bookmark): bool
@@ -57,10 +55,8 @@ class BookmarkRepository implements \Iterator
public function update(Bookmark $bookmark): bool
{
- $index = $this->findBookmarkIndex($bookmark->id);
-
- if ($index !== null) {
- $this->repository[$index] = $bookmark;
+ if (array_key_exists($bookmark->id, $this->repository)) {
+ $this->repository[$bookmark->id] = $bookmark;
return $this->save();
}
@@ -70,10 +66,8 @@ class BookmarkRepository implements \Iterator
public function delete(Bookmark $bookmark): bool
{
- $index = $this->findBookmarkIndex($bookmark->id);
-
- if ($index !== null) {
- unset($this->repository[$index]);
+ if (array_key_exists($bookmark->id, $this->repository)) {
+ unset($this->repository[$bookmark->id]);
return $this->save();
}
@@ -106,17 +100,6 @@ class BookmarkRepository implements \Iterator
return isset($this->repository[$this->position]);
}
- private function findBookmarkIndex(string $id): ?int
- {
- foreach ($this->repository as $index => $bookmark) {
- if ($bookmark->id == $id) {
- return $index;
- }
- }
-
- return null;
- }
-
private function load(): void
{
if (!is_file($this->pathToRepository) || !is_readable($this->pathToRepository)) {
@@ -127,14 +110,16 @@ class BookmarkRepository implements \Iterator
throw new \Exception("Unable to read from repository {$this->pathToRepository}");
}
+ $id = 0;
+
while (($data = fgetcsv($fp)) !== FALSE) {
$bookmark = new Bookmark();
- $bookmark->id = $data[0];
- $bookmark->url = $data[1];
- $bookmark->title = $data[2];
- $bookmark->tag = $data[3];
- $bookmark->addedAt = $data[4];
+ $bookmark->id = $id++;
+ $bookmark->url = $data[0];
+ $bookmark->title = $data[1];
+ $bookmark->tag = $data[2];
+ $bookmark->addedAt = $data[3];
$this->repository[] = $bookmark;
}
@@ -152,7 +137,6 @@ class BookmarkRepository implements \Iterator
foreach ($this->repository as $bookmark) {
$success = $success && fputcsv($fp, [
- $bookmark->id,
$bookmark->url,
$bookmark->title,
$bookmark->tag,