diff options
| author | David T. Sadler <davidtsadler@googlemail.com> | 2021-10-07 21:31:17 +0100 |
|---|---|---|
| committer | David T. Sadler <davidtsadler@googlemail.com> | 2021-10-07 21:31:17 +0100 |
| commit | 0f273c0a8ead390346571c4be2296fa6e384eff6 (patch) | |
| tree | 292fc180a277a5324df73c5c8b02c8c673e812a3 /src | |
| parent | 34ba17ceeecd3ac6be9e31be45ee76757da2dec0 (diff) | |
Add bookmarks
Diffstat (limited to 'src')
| -rw-r--r-- | src/DTS/Bookmark.php | 9 | ||||
| -rw-r--r-- | src/DTS/BookmarkRepository.php | 25 | ||||
| -rw-r--r-- | src/DTS/Functions.php | 2 | ||||
| -rw-r--r-- | src/templates/add.php | 16 | ||||
| -rw-r--r-- | src/templates/index.php | 1 |
5 files changed, 46 insertions, 7 deletions
diff --git a/src/DTS/Bookmark.php b/src/DTS/Bookmark.php index 93c7a29..560fad4 100644 --- a/src/DTS/Bookmark.php +++ b/src/DTS/Bookmark.php @@ -10,18 +10,21 @@ class Bookmark public string $url; + public string $title; + public string $tag; public string $addedAt; - public bool $read; + public bool $unread; - function __construct(string $id, string $url, string $tag, string $addedAt, bool $read) + function __construct(string $id, string $url, string $title, string $tag, string $addedAt, bool $unread) { $this->id = $id; $this->url = $url; + $this->title = $title; $this->tag = $tag; $this->addedAt = $addedAt; - $this->read = $read; + $this->unread = $unread; } } 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]; diff --git a/src/DTS/Functions.php b/src/DTS/Functions.php index b7f9cb3..d7ca835 100644 --- a/src/DTS/Functions.php +++ b/src/DTS/Functions.php @@ -4,7 +4,7 @@ declare(strict_types=1); namespace DTS\Functions; -function respondAndExit(int $responseCode, string $header, string $body, array $headers = []): void +function respondAndExit(int $responseCode, string $header, string $body = '', array $headers = []): void { header($header, false, $responseCode); diff --git a/src/templates/add.php b/src/templates/add.php new file mode 100644 index 0000000..a44c4d3 --- /dev/null +++ b/src/templates/add.php @@ -0,0 +1,16 @@ +<!doctype html> +<html lang="en"> + <head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>Bookmarks</title> + </head> + <body> + <form action="/add" method="POST"> + <input type="text" name="url" maxlength="512" autofocus><br> + <input type="text" name="title" maxlength="256" ><br> + <input type="text" name="tag" maxlength="8" ><br> + <button type="submit">Add</button> + </form> + </body> +</html> diff --git a/src/templates/index.php b/src/templates/index.php index 034f09c..9656551 100644 --- a/src/templates/index.php +++ b/src/templates/index.php @@ -6,7 +6,6 @@ <title>Bookmarks</title> </head> <body> - <h1>Hello <?php echo 'World'; ?></h1> <ul> <?php foreach ($bookmarks as $bookmark) { ?> <li><?php echo "[$bookmark->id] $bookmark->url"; ?></li> |
