Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commitf1ca4a625aa0a79f9b9fb12528b347f5c28bdba5 (patch)
Author
 Syndamia <kamen@syndamia.com>
2024-12-18 21:53:36 +0200
Committer
 Syndamia <kamen@syndamia.com>
2024-12-18 21:53:36 +0200
Files7e27fa1536b7fcac07ec41f644e1022c6c113b51
uni-projects-f1ca4a625aa0a79f9b9fb12528b347f5c28bdba5.tar
uni-projects-f1ca4a625aa0a79f9b9fb12528b347f5c28bdba5.tar.gz
uni-projects-f1ca4a625aa0a79f9b9fb12528b347f5c28bdba5.zip
Parent809704a717b198f5427bcdae3bf7ce8e8a764e2f (diff)
hw(Web/3): Simplifid by using an array attribute instead of multiple attributes
1 files changed, 24 insertions, 39 deletions
-rw-r--r--Year 4/Web technologies/hw03.php63
diff --git a/Year 4/Web technologies/hw03.php b/Year 4/Web technologies/hw03.php
index c7a591e..ebb7576 100644
--- a/Year 4/Web technologies/hw03.php
+++ b/Year 4/Web technologies/hw03.php
@@ -19,12 +19,6 @@ abstract class Form {
}
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) . ' не е предадено! Полето е задължително!';
@@ -48,6 +42,12 @@ class Errors {
Errors::get_error_len_max($name, $max);
}
+ private static function get_error_enumerate(string $name, array $valid_values) {
+ if (!in_array($_POST[$name], $valid_values)) {
+ return Form::to_bg($name) . ' не е измежду ' . implode(', ', $valid_values) . '!';
+ }
+ }
+
private static function get_error_int(string $name) {
if (!is_numeric($_POST[$name])) {
return Form::to_bg($name) . " не е число!";
@@ -60,54 +60,39 @@ class Errors {
}
}
- public function __construct() {
- $this->name = $this->get_error_submitted(Form::Name) ??
- $this->get_error_len(Form::Name, 2, 150);
+ private $messages;
- $this->teacher = $this->get_error_submitted(Form::Teacher) ??
- $this->get_error_len(Form::Teacher, 3, 200);
+ public function __construct() {
+ $this->messages[Form::Name] = Errors::get_error_submitted(Form::Name) ??
+ Errors::get_error_len(Form::Name, 2, 150);
- $this->description = $this->get_error_submitted(Form::Description) ??
- $this->get_error_len_min(Form::Description, 10);
+ $this->messages[Form::Teacher] = Errors::get_error_submitted(Form::Teacher) ??
+ Errors::get_error_len(Form::Teacher, 3, 200);
- $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->messages[Form::Description] = Errors::get_error_submitted(Form::Description) ??
+ Errors::get_error_len_min(Form::Description, 10);
- $this->credits = $this->get_error_submitted(Form::Credits) ??
- $this->get_error_int(Form::Credits);
- }
+ $this->messages[Form::Group] = Errors::get_error_submitted(Form::Group) ??
+ Errors::get_error_enumerate(Form::Group, array(
+ 'М', 'ПМ', 'ОКН', 'ЯКН'));
- 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);
+ $this->messages[Form::Credits] = Errors::get_error_submitted(Form::Credits) ??
+ Errors::get_error_int(Form::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;
+ public function get_messages() {
+ return array_filter($this->messages, function ($m) { return !is_null($m); });
}
}
$errors = new Errors();
+$messages = $errors->get_messages();
-if ($errors->has_any()) {
+if (!empty($messages)) {
echo json_encode(
array(
"success" => false,
- "errors" => $errors->to_array()
+ "errors" => $messages
),
JSON_UNESCAPED_UNICODE
);