summaryrefslogtreecommitdiff
path: root/src/DTS/Validator.php
diff options
context:
space:
mode:
authorDavid T. Sadler <davidtsadler@googlemail.com>2021-11-13 09:22:25 +0000
committerDavid T. Sadler <davidtsadler@googlemail.com>2021-11-13 09:22:25 +0000
commit40997195b7ee07cb1bda978186c1804371e1f16e (patch)
tree17a6e4bd6eaca795cfa8d57b9aa6ea8ad7066593 /src/DTS/Validator.php
parent35dc6c245d93560d7ac81df6323e8b22e571aa95 (diff)
Create site
Diffstat (limited to 'src/DTS/Validator.php')
-rw-r--r--src/DTS/Validator.php59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/DTS/Validator.php b/src/DTS/Validator.php
new file mode 100644
index 0000000..2f9c255
--- /dev/null
+++ b/src/DTS/Validator.php
@@ -0,0 +1,59 @@
+<?php
+
+declare(strict_types=1);
+
+namespace DTS;
+
+use DTS\Errors;
+use DTS\Validated;
+
+class Validator
+{
+ public Errors $errors;
+
+ public Validated $validated;
+
+ function __construct(array $request)
+ {
+ $this->errors = new Errors();
+
+ $this->validated = new Validated();
+
+ $this->validateTask($request['task'], 2, 256);
+
+ $this->validateTag($request['tag'], 2, 16);
+ }
+
+ private function validateTask(string $task, int $minLength, int $maxLength): void
+ {
+ $task = trim($task);
+
+ if (strlen($task) < $minLength || strlen($task) > $maxLength) {
+ $this->errors->add('task', "Must be between $minLength and $maxLength in characters in length");
+ }
+
+ if (!$this->errors->has('tite')) {
+ $this->validated->task = $task;
+ }
+ }
+
+ private function validateTag(string $tag, int $minLength, int $maxLength): void
+ {
+ $tag = trim($tag);
+
+ if ($tag === '') {
+ return;
+ }
+
+ if (strlen($tag) < $minLength || strlen($tag) > $maxLength) {
+ $this->errors->add('tag', "Must be between $minLength and $maxLength in characters in length");
+ }
+ if (preg_match('/\W/', $tag) === 1) {
+ $this->errors->add('tag', 'May only contain word characters');
+ }
+
+ if (!$this->errors->has('tag')) {
+ $this->validated->tag = strtolower($tag);
+ }
+ }
+}