diff options
| Commit | 809704a717b198f5427bcdae3bf7ce8e8a764e2f (patch) | |
|---|---|---|
| Author | 2024-12-18 21:25:08 +0200 | |
| Committer | 2024-12-18 21:25:08 +0200 | |
| Files | b3adfdd9b138a124cab7830dffc073c9e6a92ecb | |
| uni-projects-809704a717b198f5427bcdae3bf7ce8e8a764e2f.tar uni-projects-809704a717b198f5427bcdae3bf7ce8e8a764e2f.tar.gz uni-projects-809704a717b198f5427bcdae3bf7ce8e8a764e2f.zip | ||
| Parent | ca4c08a327c2f173d93653b270767f9b6df8a969 (diff) | |
hw(Web/3): Initial implementation
2 files changed, 140 insertions, 0 deletions
| -rw-r--r-- | Year 4/Web technologies/hw03.html | 23 | ||||
| -rw-r--r-- | Year 4/Web technologies/hw03.php | 117 |
diff --git a/Year 4/Web technologies/hw03.html b/Year 4/Web technologies/hw03.html new file mode 100644 index 0000000..8d07170 --- /dev/null +++ b/Year 4/Web technologies/hw03.html @@ -0,0 +1,23 @@ +<!DOCTYPE html> +<html> + <head> + <meta charset="UTF-8"> + <title>PHP Test</title> + </head> + <body> + <form action="./hw03.php" method="post"> + <input type="text" name="name"> + <input type="text" name="teacher"> + <input type="text" name="description"> + <input type="text" name="group"> + <!-- <select name="group"> --> + <!-- <option value="m">М</option> --> + <!-- <option value="pm">ПМ</option> --> + <!-- <option value="okn">ОКН</option> --> + <!-- <option value="qkn">ЯКН</option> --> + <!-- </select> --> + <input type="text" name="credits"> + <input type="submit" formaction="./hw03.php"> + </form> + </body> +</html> diff --git a/Year 4/Web technologies/hw03.php b/Year 4/Web technologies/hw03.php new file mode 100644 index 0000000..c7a591e --- /dev/null +++ b/Year 4/Web technologies/hw03.php @@ -0,0 +1,117 @@ +<?php +abstract class Form { + const Name = 'name'; + const Teacher = 'teacher'; + const Description = 'description'; + const Group = 'group'; + const Credits = 'credits'; + + static function to_bg(string $name) { + switch ($name) { + case Form::Name: return 'Име на учебен предмет'; + case Form::Teacher: return 'Преподавател'; + case Form::Description: return 'Описание'; + case Form::Group: return 'Група'; + case Form::Credits: return 'Кредити'; + default: throw new Exception("'$name' isn't an acceptable value!"); + } + } +} + +class Errors { + public $name; + public $teacher; + public $description; + public $group; + public $credits; + + private static function get_error_submitted(string $name) { + if (!array_key_exists($name, $_POST) || mb_strlen($_POST[$name]) == 0) { + return Form::to_bg($name) . ' не е предадено! Полето е задължително!'; + } + } + + private static function get_error_len_min(string $name, int $min) { + if (mb_strlen($_POST[$name]) < $min) { + return Form::to_bg($name) . " е твърде късо! Дължината може да е минимум $min на брой символа!"; + } + } + + private static function get_error_len_max(string $name, int $max) { + if (mb_strlen($_POST[$name]) > $max) { + return Form::to_bg($name) . " е твърде дълго! Дължината може да е максимум $max на брой символа!"; + } + } + + private static function get_error_len(string $name, int $min, int $max) { + return Errors::get_error_len_min($name, $min) ?? + Errors::get_error_len_max($name, $max); + } + + private static function get_error_int(string $name) { + if (!is_numeric($_POST[$name])) { + return Form::to_bg($name) . " не е число!"; + } + elseif (floor($_POST[$name]) != $_POST[$name]) { + return Form::to_bg($name) . " не е цяло число!"; + } + elseif ($_POST[$name] <= 0) { + return Form::to_bg($name) . " не е положително число!"; + } + } + + public function __construct() { + $this->name = $this->get_error_submitted(Form::Name) ?? + $this->get_error_len(Form::Name, 2, 150); + + $this->teacher = $this->get_error_submitted(Form::Teacher) ?? + $this->get_error_len(Form::Teacher, 3, 200); + + $this->description = $this->get_error_submitted(Form::Description) ?? + $this->get_error_len_min(Form::Description, 10); + + $this->group = $this->get_error_submitted(Form::Group); + if (is_null($this->group)) { + switch ($_POST[Form::Group]) { + case 'М': case 'ПМ': case 'ОКН': case 'ЯКН': break; + default: $this->group = Form::to_bg(Form::Group) . ' не е измежду М, ПМ, ОКН и ЯКН!'; + } + } + + $this->credits = $this->get_error_submitted(Form::Credits) ?? + $this->get_error_int(Form::Credits); + } + + public function has_any() { + return !is_null($this->name) || + !is_null($this->teacher) || + !is_null($this->description) || + !is_null($this->group) || + !is_null($this->credits); + } + + public function to_array() { + $output = array(); + if (!is_null($this->name)) { $output[Form::Name] = $this->name; } + if (!is_null($this->teacher)) { $output[Form::Teacher] = $this->teacher; } + if (!is_null($this->description)) { $output[Form::Description] = $this->description; } + if (!is_null($this->group)) { $output[Form::Group] = $this->group; } + if (!is_null($this->credits)) { $output[Form::Credits] = $this->credits; } + return $output; + } +} + +$errors = new Errors(); + +if ($errors->has_any()) { + echo json_encode( + array( + "success" => false, + "errors" => $errors->to_array() + ), + JSON_UNESCAPED_UNICODE + ); +} +else { + echo json_encode(array("success" => true)); +} |
