- [[#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:
?>
#+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
<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>
<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>
#+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
--- /dev/null
+<?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}",
+ ];
+ }
+}
+?>