$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)); }