From 809704a717b198f5427bcdae3bf7ce8e8a764e2f Mon Sep 17 00:00:00 2001 From: Syndamia Date: Wed, 18 Dec 2024 21:25:08 +0200 Subject: hw(Web/3): Initial implementation --- Year 4/Web technologies/hw03.html | 23 ++++++++ Year 4/Web technologies/hw03.php | 117 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 Year 4/Web technologies/hw03.html create mode 100644 Year 4/Web technologies/hw03.php 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 @@ + + + + + PHP Test + + +
+ + + + + + + + + + + + +
+ + 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 @@ + $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)); +} -- cgit v1.3.1-23-gd82d