]> git.leonardobizzoni.com Git - highschool-graduation-project/commitdiff
Generic models
authorLeonardoBizzoni <leo2002714@gmail.com>
Sun, 17 Apr 2022 10:46:22 +0000 (12:46 +0200)
committerLeonardoBizzoni <leo2002714@gmail.com>
Sun, 17 Apr 2022 10:46:22 +0000 (12:46 +0200)
README.org
www/controllers/AuthController.php
www/core/BaseModel.php [new file with mode: 0644]
www/models/RegisterModel.php [new file with mode: 0644]
www/views/register.php

index a70efaf5a18704a1a167b1ee1f8010d16b4f3b9c..5b618f584fd4f76d7db7bdce8257f8e62849aa80 100644 (file)
@@ -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
 <?php
-namespace app\controllers;
+namespace app\core;
 
-use app\core\BaseController;
-use app\core\Request;
+abstract class BaseModel {
+    public const RULE_REQUIRED = "required";
+    public const RULE_EMAIL = "email";
+    public const RULE_MIN = "min";
+    public const RULE_MAX = "max";
+    public const RULE_MATCH = "match";
+    public const RULE_UNIQUE = "unique";
 
-class SiteController extends BaseController {
-    public function home() {
-        $params = [
-            "name" => "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
 <?php
-namespace app\controllers;
+namespace app\models;
 
-use app\core\BaseController;
-use app\core\Request;
+use app\core\BaseModel;
 
-class AuthController extends BaseController{
-    public function login() {
-        $this->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{
     <form method="POST" class="row g-3">
         <div class="col-md-4">
             <label class="form-label">Firstname</label>
-            <input name="name" type="text" class="form-control">
+            <input name="firstname" type="text" class="form-control">
         </div>
         <div class="col-md-4">
             <label class="form-label">Lastname</label>
@@ -531,8 +586,8 @@ class AuthController extends BaseController{
             <label class="form-label">Password confirm</label>
             <input name="passConf" name="pass" type="password" class="form-control">
         </div>
-        <div class="col-12">
-            <button type="submit" class="btn btn-primary">Sign in</button>
+        <div class="col-md-1">
+            <button type="submit" class="btn btn-primary">Submit</button>
         </div>
     </form>
 </div>
@@ -542,3 +597,68 @@ class AuthController extends BaseController{
 #+begin_src php :tangle www/views/404.php
 <h1>404 - File not found!</h1>
 #+end_src
+
+** Controllers
+*** General controller
+#+begin_src php :tangle www/controllers/SiteController.php
+<?php
+namespace app\controllers;
+
+use app\core\BaseController;
+use app\core\Request;
+
+class SiteController extends BaseController {
+    public function home() {
+        $params = [
+            "name" => "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
+<?php
+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");
+        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
index e9b4da724b65570cd35e971552b631ed4bda0781..fc58e8e1cd28284c23bebd35a62bc12af3efefec 100644 (file)
@@ -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 (file)
index 0000000..dd71915
--- /dev/null
@@ -0,0 +1,74 @@
+<?php
+namespace app\core;
+
+abstract class BaseModel {
+    public const RULE_REQUIRED = "required";
+    public const RULE_EMAIL = "email";
+    public const RULE_MIN = "min";
+    public const RULE_MAX = "max";
+    public const RULE_MATCH = "match";
+    public const RULE_UNIQUE = "unique";
+
+    public array $errors = [];
+
+    public function loadData($data) {
+        foreach ($data as $key => $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 (file)
index 0000000..6a57e9f
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+namespace app\models;
+
+use app\core\BaseModel;
+
+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 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" ]]
+        ];
+    }
+}
+?>
index 70dd0093259a84a5c26f8d60be9842064e90a456..dad7a6aa11000aad28ae8aba7e55fb69cd41212f 100644 (file)
@@ -4,7 +4,7 @@
     <form method="POST" class="row g-3">
         <div class="col-md-4">
             <label class="form-label">Firstname</label>
-            <input name="name" type="text" class="form-control">
+            <input name="firstname" type="text" class="form-control">
         </div>
         <div class="col-md-4">
             <label class="form-label">Lastname</label>
@@ -26,8 +26,8 @@
             <label class="form-label">Password confirm</label>
             <input name="passConf" name="pass" type="password" class="form-control">
         </div>
-        <div class="col-12">
-            <button type="submit" class="btn btn-primary">Sign in</button>
+        <div class="col-md-1">
+            <button type="submit" class="btn btn-primary">Submit</button>
         </div>
     </form>
 </div>