summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--public/store/index.php1
-rw-r--r--src/DTS/Bookmark.php2
-rw-r--r--src/DTS/BookmarkRepository.php40
3 files changed, 13 insertions, 30 deletions
diff --git a/public/store/index.php b/public/store/index.php
index 804702a..a25d6f3 100644
--- a/public/store/index.php
+++ b/public/store/index.php
@@ -39,7 +39,6 @@ $bookmarks = new BookmarkRepository($config['path_to_repository']);
$bookmark = new Bookmark();
-$bookmark->id = bin2hex(random_bytes(32));
$bookmark->url = $validated->url;
$bookmark->title = $validated->title;
$bookmark->tag = $validated->tag;
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,