From: LeonardoBizzoni Date: Sun, 17 Apr 2022 10:46:22 +0000 (+0200) Subject: Generic models X-Git-Url: http://git.leonardobizzoni.com/?a=commitdiff_plain;h=ed8a11bd58aa0e3dd6918d149cc614480540ad47;p=highschool-graduation-project Generic models --- diff --git a/README.org b/README.org index a70efaf..5b618f5 100644 --- a/README.org +++ b/README.org @@ -11,8 +11,9 @@ - [[#realizzazione][Realizzazione]] - [[#entry-point---indexphp][Entry point - index.php]] - [[#core][Core]] - - [[#controllers][Controllers]] + - [[#models][Models]] - [[#views][Views]] + - [[#controllers][Controllers]] * Descrizione del progetto Su [questo-sito] è possibile registrare un account con cui si può tenere traccia di: @@ -389,61 +390,115 @@ class BaseController { ?> #+end_src -** Controllers -*** General controller -#+begin_src php :tangle www/controllers/SiteController.php +*** Base model class +#+begin_src php :tangle www/core/BaseModel.php "Leonardo" - ]; + public array $errors = []; - return $this->render("home", $params); + public function loadData($data) { + foreach ($data as $key => $value) { + if (property_exists($this, $key)) { + $this->{$key} = $value; + } + } } - public function contact() { - return $this->render("contact"); - } + abstract public function rules(): array; + + public function validate() { + foreach ($this->rules() as $attribute => $rules) { + $value = $this->{$attribute}; + + foreach ($rules as $rule) { + $ruleName = $rule; + + if (!is_string($ruleName)) { + $ruleName = $rule[0]; + } + + if ($ruleName == self::RULE_REQUIRED && !$value) { + $this->addError($attribute, self::RULE_REQUIRED); + } + if ($ruleName == self::RULE_EMAIL && !filter_var($value, FILTER_VALIDATE_EMAIL)) { + $this->addError($attribute, self::RULE_EMAIL); + } + if ($ruleName == self::RULE_MIN && strlen($value) < $rule["min"]) { + $this->addError($attribute, self::RULE_MIN, $rule); + } + if ($ruleName == self::RULE_MAX && strlen($value) > $rule["max"]) { + $this->addError($attribute, self::RULE_MAX, $rule); + } + if ($ruleName == self::RULE_MATCH && $value != $this->{$rule["match"]}) { + $this->addError($attribute, self::RULE_MATCH, $rule); + } + } + } - public function handleContact(Request $req) { - $body = $req->getBody(); + return empty($this->errors); + } - # $body validation + public function addError(string $attribute, string $rule, $params = []) { + $message = $this->errorMessages()[$rule] ?? ""; + foreach ($params as $key => $value) { + $message = str_replace("{{$key}}", $value, $message); + } + $this->errors[$attribute][] = $message; + } - return "Handling submitted data"; + public function errorMessages() { + return [ + self::RULE_REQUIRED => "This field is required", + self::RULE_EMAIL => "This field must be a valid email address", + self::RULE_MIN => "Min length of this field must be {min}", + self::RULE_MAX => "Max length of this field must be {max}", + self::RULE_MATCH => "This field must be the same as {match}", + ]; } } ?> #+end_src -*** Authentication controller -#+begin_src php :tangle www/controllers/AuthController.php +** Models +*** Registration model +#+begin_src php :tangle www/models/RegisterModel.php setLayout("auth"); - return $this->render("login"); +class RegisterModel extends BaseModel { + public string $firstname; + public string $lastname; + public string $email; + public string $username; + public string $pass; + public string $passConf; + + public function register() { + echo "Creating new user"; } - public function register(Request $req) { - $this->setLayout("auth"); - if ($req->getMethod() == "post") { - return "Handling submitted data"; - } - return $this->render("register"); + public function rules(): array { + return [ + "firstname" => [self::RULE_REQUIRED], + "lastname" => [self::RULE_REQUIRED], + "email" => [self::RULE_REQUIRED, self::RULE_EMAIL], + "pass" => [self::RULE_REQUIRED, [ self::RULE_MIN, "min" => 20 ], [ self::RULE_MAX, "max" => 100 ]], + "passConf" => [self::RULE_REQUIRED, [self::RULE_MATCH, "match" => "pass" ]] + ]; } } +?> #+end_src ** Views @@ -509,7 +564,7 @@ class AuthController extends BaseController{
- +
@@ -531,8 +586,8 @@ class AuthController extends BaseController{
-
- +
+
@@ -542,3 +597,68 @@ class AuthController extends BaseController{ #+begin_src php :tangle www/views/404.php

404 - File not found!

#+end_src + +** Controllers +*** General controller +#+begin_src php :tangle www/controllers/SiteController.php + "Leonardo" + ]; + + return $this->render("home", $params); + } + + public function contact() { + return $this->render("contact"); + } + + public function handleContact(Request $req) { + $body = $req->getBody(); + + # $body validation + + return "Handling submitted data"; + } +} +?> +#+end_src + +*** Authentication controller +#+begin_src php :tangle www/controllers/AuthController.php +setLayout("auth"); + return $this->render("login"); + } + + public function register(Request $req) { + // $this->setLayout("auth"); + $errors = []; + $registerModel = new RegisterModel; + + if ($req->getMethod() == "post") { + $registerModel->loadData($req->getBody()); + + if ($registerModel->validate() && $registerModel->register()) { + return "Success"; + } + } + return $this->render("register", [ "model" => $registerModel ]); + } +} +#+end_src diff --git a/www/controllers/AuthController.php b/www/controllers/AuthController.php index e9b4da7..fc58e8e 100644 --- a/www/controllers/AuthController.php +++ b/www/controllers/AuthController.php @@ -3,18 +3,26 @@ namespace app\controllers; use app\core\BaseController; use app\core\Request; +use app\models\RegisterModel; class AuthController extends BaseController{ public function login() { - $this->setLayout("auth"); + // $this->setLayout("auth"); return $this->render("login"); } public function register(Request $req) { - $this->setLayout("auth"); + // $this->setLayout("auth"); + $errors = []; + $registerModel = new RegisterModel; + if ($req->getMethod() == "post") { - return "Handling submitted data"; + $registerModel->loadData($req->getBody()); + + if ($registerModel->validate() && $registerModel->register()) { + return "Success"; + } } - return $this->render("register"); + return $this->render("register", [ "model" => $registerModel ]); } } diff --git a/www/core/BaseModel.php b/www/core/BaseModel.php new file mode 100644 index 0000000..dd71915 --- /dev/null +++ b/www/core/BaseModel.php @@ -0,0 +1,74 @@ + $value) { + if (property_exists($this, $key)) { + $this->{$key} = $value; + } + } + } + + abstract public function rules(): array; + + public function validate() { + foreach ($this->rules() as $attribute => $rules) { + $value = $this->{$attribute}; + + foreach ($rules as $rule) { + $ruleName = $rule; + + if (!is_string($ruleName)) { + $ruleName = $rule[0]; + } + + if ($ruleName == self::RULE_REQUIRED && !$value) { + $this->addError($attribute, self::RULE_REQUIRED); + } + if ($ruleName == self::RULE_EMAIL && !filter_var($value, FILTER_VALIDATE_EMAIL)) { + $this->addError($attribute, self::RULE_EMAIL); + } + if ($ruleName == self::RULE_MIN && strlen($value) < $rule["min"]) { + $this->addError($attribute, self::RULE_MIN, $rule); + } + if ($ruleName == self::RULE_MAX && strlen($value) > $rule["max"]) { + $this->addError($attribute, self::RULE_MAX, $rule); + } + if ($ruleName == self::RULE_MATCH && $value != $this->{$rule["match"]}) { + $this->addError($attribute, self::RULE_MATCH, $rule); + } + } + } + + return empty($this->errors); + } + + public function addError(string $attribute, string $rule, $params = []) { + $message = $this->errorMessages()[$rule] ?? ""; + foreach ($params as $key => $value) { + $message = str_replace("{{$key}}", $value, $message); + } + $this->errors[$attribute][] = $message; + } + + public function errorMessages() { + return [ + self::RULE_REQUIRED => "This field is required", + self::RULE_EMAIL => "This field must be a valid email address", + self::RULE_MIN => "Min length of this field must be {min}", + self::RULE_MAX => "Max length of this field must be {max}", + self::RULE_MATCH => "This field must be the same as {match}", + ]; + } +} +?> diff --git a/www/models/RegisterModel.php b/www/models/RegisterModel.php new file mode 100644 index 0000000..6a57e9f --- /dev/null +++ b/www/models/RegisterModel.php @@ -0,0 +1,28 @@ + [self::RULE_REQUIRED], + "lastname" => [self::RULE_REQUIRED], + "email" => [self::RULE_REQUIRED, self::RULE_EMAIL], + "pass" => [self::RULE_REQUIRED, [ self::RULE_MIN, "min" => 20 ], [ self::RULE_MAX, "max" => 100 ]], + "passConf" => [self::RULE_REQUIRED, [self::RULE_MATCH, "match" => "pass" ]] + ]; + } +} +?> diff --git a/www/views/register.php b/www/views/register.php index 70dd009..dad7a6a 100644 --- a/www/views/register.php +++ b/www/views/register.php @@ -4,7 +4,7 @@
- +
@@ -26,8 +26,8 @@
-
- +
+