From 40997195b7ee07cb1bda978186c1804371e1f16e Mon Sep 17 00:00:00 2001 From: "David T. Sadler" Date: Sat, 13 Nov 2021 09:22:25 +0000 Subject: Create site --- src/DTS/Validator.php | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/DTS/Validator.php (limited to 'src/DTS/Validator.php') 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 @@ +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); + } + } +} -- cgit v1.2.3-13-gbd6f