summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid T. Sadler <davidtsadler@googlemail.com>2021-10-26 21:07:43 +0100
committerDavid T. Sadler <davidtsadler@googlemail.com>2021-10-26 21:07:43 +0100
commitae205d810b62dce5e3e11aef07358c11ded48c77 (patch)
tree0196ae98aefb93b719d6b21cc065e22c1ecf992b
parentd4122f116c937e0ec509d8cefe540146ec27a0cd (diff)
Implement old
-rw-r--r--public/create/index.php6
-rw-r--r--public/edit/index.php6
-rw-r--r--public/store/index.php5
-rw-r--r--public/update/index.php5
-rw-r--r--src/DTS/Bookmark.php6
-rw-r--r--src/DTS/Old.php30
-rw-r--r--src/DTS/Session.php2
-rw-r--r--src/DTS/Validated.php6
-rw-r--r--src/DTS/Validator.php3
-rw-r--r--src/templates/create.php13
-rw-r--r--src/templates/edit.php13
-rw-r--r--src/templates/form_fields.php12
12 files changed, 73 insertions, 34 deletions
diff --git a/public/create/index.php b/public/create/index.php
index 786b8f2..7464f71 100644
--- a/public/create/index.php
+++ b/public/create/index.php
@@ -4,6 +4,7 @@ declare(strict_types=1);
use DTS\Bookmark;
use DTS\Errors;
+use DTS\Old;
use DTS\Session;
use DTS\Template;
@@ -19,6 +20,8 @@ if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'GET') {
respondAndExit(405, 'Method Not Allowed');
}
+$old = $session->get('old', new Old());
+
$errors = $session->get('errors', new Errors());
$template = new Template($config['path_to_templates']);
@@ -27,7 +30,8 @@ $bookmark = new Bookmark();
$html = $template->render('create', compact(
'bookmark',
- 'errors'
+ 'errors',
+ 'old'
));
respondAndExit(200, 'OK', $html);
diff --git a/public/edit/index.php b/public/edit/index.php
index fba5a90..b1efeec 100644
--- a/public/edit/index.php
+++ b/public/edit/index.php
@@ -4,6 +4,7 @@ declare(strict_types=1);
use DTS\BookmarkRepository;
use DTS\Errors;
+use DTS\Old;
use DTS\Session;
use DTS\Template;
@@ -19,6 +20,8 @@ if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'GET') {
respondAndExit(405, 'Method Not Allowed');
}
+$old = $session->get('old', new Old());
+
$errors = $session->get('errors', new Errors());
$id = filter_input(INPUT_GET, 'id');
@@ -35,7 +38,8 @@ if ($bookmark === null) {
$html = $template->render('edit', compact(
'bookmark',
- 'errors'
+ 'errors',
+ 'old'
));
respondAndExit(200, 'OK', $html);
diff --git a/public/store/index.php b/public/store/index.php
index 5279e69..8bd0b7f 100644
--- a/public/store/index.php
+++ b/public/store/index.php
@@ -4,6 +4,7 @@ declare(strict_types=1);
use DTS\Bookmark;
use DTS\BookmarkRepository;
+use DTS\Old;
use DTS\Session;
use DTS\Validator;
@@ -20,6 +21,10 @@ if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'POST') {
respondAndExit(405, 'Method Not Allowed');
}
+$old = new Old($_REQUEST);
+
+$session->set('old', $old);
+
$validator = new Validator($_REQUEST);
if ($validator->errors->count()) {
diff --git a/public/update/index.php b/public/update/index.php
index d194dbd..1bccfa4 100644
--- a/public/update/index.php
+++ b/public/update/index.php
@@ -3,6 +3,7 @@
declare(strict_types=1);
use DTS\BookmarkRepository;
+use DTS\Old;
use DTS\Session;
use DTS\Validator;
@@ -29,6 +30,10 @@ if ($bookmark === null) {
respondAndExit(404, 'Not Found');
}
+$old = new Old($_REQUEST);
+
+$session->set('old', $old);
+
$validator = new Validator($_REQUEST);
if ($validator->errors->count()) {
diff --git a/src/DTS/Bookmark.php b/src/DTS/Bookmark.php
index 543a552..da9afa9 100644
--- a/src/DTS/Bookmark.php
+++ b/src/DTS/Bookmark.php
@@ -8,11 +8,11 @@ class Bookmark
{
public string $id;
- public string $url;
+ public string $url = '';
- public string $title;
+ public string $title = '';
- public string $tag;
+ public string $tag = '';
public string $addedAt;
diff --git a/src/DTS/Old.php b/src/DTS/Old.php
new file mode 100644
index 0000000..8545f8d
--- /dev/null
+++ b/src/DTS/Old.php
@@ -0,0 +1,30 @@
+<?php
+
+declare(strict_types=1);
+
+namespace DTS;
+
+class Old
+{
+ const FIELDS = [
+ 'url',
+ 'title',
+ 'tag',
+ ];
+
+ private array $old = [];
+
+ function __construct(array $request = [])
+ {
+ foreach(self::FIELDS as $field) {
+ if (array_key_exists($field, $request)) {
+ $this->old[$field] = $request[$field];
+ }
+ }
+ }
+
+ public function get(string $key, mixed $default = null): mixed
+ {
+ return $this->old[$key] ?? $default;
+ }
+}
diff --git a/src/DTS/Session.php b/src/DTS/Session.php
index e5da42f..445f31c 100644
--- a/src/DTS/Session.php
+++ b/src/DTS/Session.php
@@ -8,7 +8,7 @@ class Session
{
private static ?self $instance = null;
- private array $session;
+ private array $session = [];
public static function getInstance()
{
diff --git a/src/DTS/Validated.php b/src/DTS/Validated.php
index 5df8af7..86c6f64 100644
--- a/src/DTS/Validated.php
+++ b/src/DTS/Validated.php
@@ -6,9 +6,9 @@ namespace DTS;
class Validated
{
- public ?string $url;
+ public string $url = '';
- public ?string $title;
+ public string $title = '';
- public ?string $tag;
+ public string $tag = '';
}
diff --git a/src/DTS/Validator.php b/src/DTS/Validator.php
index 7629e3b..472b38f 100644
--- a/src/DTS/Validator.php
+++ b/src/DTS/Validator.php
@@ -13,7 +13,8 @@ class Validator
public Validated $validated;
- function __construct(array $request) {
+ function __construct(array $request)
+ {
$this->errors = new Errors();
$this->validated = new Validated();
diff --git a/src/templates/create.php b/src/templates/create.php
index 2769290..0124e03 100644
--- a/src/templates/create.php
+++ b/src/templates/create.php
@@ -8,18 +8,7 @@
<body>
<a href="/">Back</a>
<form action="/store" method="POST">
- <input type="text" name="url" maxlength="512" autofocus><br>
- <?php if ($errors->has('url')) { ?>
- <p><?php echo implode(', ', $errors->get('url')); ?></p>
- <?php } ?>
- <input type="text" name="title" maxlength="256" ><br>
- <?php if ($errors->has('title')) { ?>
- <p><?php echo implode(', ', $errors->get('title')); ?></p>
- <?php } ?>
- <input type="text" name="tag" maxlength="8" ><br>
- <?php if ($errors->has('tag')) { ?>
- <p><?php echo implode(', ', $errors->get('tag')); ?></p>
- <?php } ?>
+ <?php require_once('form_fields.php'); ?>
<button type="submit">Add</button>
</form>
</body>
diff --git a/src/templates/edit.php b/src/templates/edit.php
index c1b7bda..e77dd48 100644
--- a/src/templates/edit.php
+++ b/src/templates/edit.php
@@ -9,18 +9,7 @@
<a href="/">Back</a>
<form action="/update" method="POST">
<input type="hidden" name="id" value="<?php echo $bookmark->id; ?>"/>
- <input type="text" name="url" maxlength="512" autofocus value="<?php echo $bookmark->url; ?>"><br>
- <?php if ($errors->has('url')) { ?>
- <p><?php echo implode(', ', $errors->get('url')); ?></p>
- <?php } ?>
- <input type="text" name="title" maxlength="256" value="<?php echo $bookmark->title; ?>"><br>
- <?php if ($errors->has('title')) { ?>
- <p><?php echo implode(', ', $errors->get('title')); ?></p>
- <?php } ?>
- <input type="text" name="tag" maxlength="8" value="<?php echo $bookmark->tag; ?>"><br>
- <?php if ($errors->has('tag')) { ?>
- <p><?php echo implode(', ', $errors->get('tag')); ?></p>
- <?php } ?>
+ <?php require_once('form_fields.php'); ?>
<button type="submit">Update</button>
</form>
</body>
diff --git a/src/templates/form_fields.php b/src/templates/form_fields.php
new file mode 100644
index 0000000..cfacfda
--- /dev/null
+++ b/src/templates/form_fields.php
@@ -0,0 +1,12 @@
+<input type="text" name="url" maxlength="512" value="<?php echo $old->get('url', $bookmark->url); ?>" autofocus><br>
+<?php if ($errors->has('url')) { ?>
+ <p><?php echo implode(', ', $errors->get('url')); ?></p>
+<?php } ?>
+<input type="text" name="title" maxlength="256" value="<?php echo $old->get('title', $bookmark->title); ?>"><br>
+<?php if ($errors->has('title')) { ?>
+ <p><?php echo implode(', ', $errors->get('title')); ?></p>
+<?php } ?>
+<input type="text" name="tag" maxlength="8" value="<?php echo $old->get('tag', $bookmark->tag); ?>"><br>
+<?php if ($errors->has('tag')) { ?>
+ <p><?php echo implode(', ', $errors->get('tag')); ?></p>
+<?php } ?>