diff options
| author | polo <ordipolo@gmx.fr> | 2026-07-26 13:42:25 +0200 |
|---|---|---|
| committer | polo <ordipolo@gmx.fr> | 2026-07-26 13:42:25 +0200 |
| commit | 2c89c0ec184c0ed76562c899722bb3615cd83add (patch) | |
| tree | c0006cf58164533ae34ec113d683e8a5f106098d | |
| parent | 0458c5dc4a8e36737f45d6813af24b08f0958688 (diff) | |
| download | cms-2c89c0ec184c0ed76562c899722bb3615cd83add.tar.gz cms-2c89c0ec184c0ed76562c899722bb3615cd83add.tar.bz2 cms-2c89c0ec184c0ed76562c899722bb3615cd83add.zip | |
choix d'une stratégie par type d'objet, suppression des isset inutiles, suppression de $validated
| -rw-r--r-- | public/index.php | 9 | ||||
| -rw-r--r-- | src/controller/ContactFormController.php | 4 | ||||
| -rw-r--r-- | src/controller/UserController.php | 8 | ||||
| -rw-r--r-- | src/service/FormValidation.php | 88 |
4 files changed, 53 insertions, 56 deletions
diff --git a/public/index.php b/public/index.php index 2ab799b..19011d1 100644 --- a/public/index.php +++ b/public/index.php | |||
| @@ -45,21 +45,16 @@ if(IS_ADMIN){ | |||
| 45 | // remplit la BDD initiale, ne fonctionne que si la BDD est vide | 45 | // remplit la BDD initiale, ne fonctionne que si la BDD est vide |
| 46 | DatabaseSettingUp::run($entityManager); | 46 | DatabaseSettingUp::run($entityManager); |
| 47 | 47 | ||
| 48 | $request = Request::createFromGlobals(); | ||
| 49 | |||
| 50 | 48 | ||
| 51 | /* -- partie 2: routage et contrôleurs -- */ | 49 | /* -- partie 2: routage et contrôleurs -- */ |
| 52 | 50 | $request = Request::createFromGlobals(); | |
| 53 | define('CURRENT_PAGE', htmlspecialchars($request->query->get('page') ?? '')); | 51 | define('CURRENT_PAGE', htmlspecialchars($request->query->get('page') ?? '')); |
| 54 | 52 | ||
| 55 | //Router::dispatch($request, $entityManager); | ||
| 56 | //$response = Router::dispatch($request, $entityManager); | ||
| 57 | |||
| 58 | $router = new Router($request, $entityManager); | 53 | $router = new Router($request, $entityManager); |
| 59 | $response = $router->dispatch(); | 54 | $response = $router->dispatch(); |
| 60 | $response->send(); | 55 | $response->send(); |
| 61 | 56 | ||
| 62 | // gestion des erreurs | 57 | // avec gestion des erreurs |
| 63 | /*try{ | 58 | /*try{ |
| 64 | $response = $router->dispatch(); | 59 | $response = $router->dispatch(); |
| 65 | } | 60 | } |
diff --git a/src/controller/ContactFormController.php b/src/controller/ContactFormController.php index cbc1837..e078af2 100644 --- a/src/controller/ContactFormController.php +++ b/src/controller/ContactFormController.php | |||
| @@ -28,7 +28,7 @@ class ContactFormController | |||
| 28 | } | 28 | } |
| 29 | static public function setEmailParam(EntityManager $entityManager, array $json): JsonResponse | 29 | static public function setEmailParam(EntityManager $entityManager, array $json): JsonResponse |
| 30 | { | 30 | { |
| 31 | $form = new FormValidation($json, 'email_params'); | 31 | $form = new FormValidation($json, ValidationStrategy::EmailParam); |
| 32 | 32 | ||
| 33 | if($form->validate()){ | 33 | if($form->validate()){ |
| 34 | $form_data = $entityManager->find(EmailForm::class, $json['id']); | 34 | $form_data = $entityManager->find(EmailForm::class, $json['id']); |
| @@ -45,7 +45,7 @@ class ContactFormController | |||
| 45 | // les deux méthodes suivantes sont "factorisables", elles ne se distinguent que par la gestion ou non du formulaire rempli par le visiteur | 45 | // les deux méthodes suivantes sont "factorisables", elles ne se distinguent que par la gestion ou non du formulaire rempli par le visiteur |
| 46 | static public function sendVisitorEmail(EntityManager $entityManager, array $json): JsonResponse | 46 | static public function sendVisitorEmail(EntityManager $entityManager, array $json): JsonResponse |
| 47 | { | 47 | { |
| 48 | $form = new FormValidation($json, 'email_send'); | 48 | $form = new FormValidation($json, ValidationStrategy::EmailSend); |
| 49 | 49 | ||
| 50 | $error = ''; | 50 | $error = ''; |
| 51 | if($form->validate()){ | 51 | if($form->validate()){ |
diff --git a/src/controller/UserController.php b/src/controller/UserController.php index 03686ee..750b01b 100644 --- a/src/controller/UserController.php +++ b/src/controller/UserController.php | |||
| @@ -25,7 +25,7 @@ class UserController | |||
| 25 | { | 25 | { |
| 26 | unset($_SESSION['user']); | 26 | unset($_SESSION['user']); |
| 27 | 27 | ||
| 28 | $form = new FormValidation($_POST, 'create_user'); | 28 | $form = new FormValidation($_POST, ValidationStrategy::CreateUser); |
| 29 | 29 | ||
| 30 | $url = new URL; | 30 | $url = new URL; |
| 31 | $error = ''; | 31 | $error = ''; |
| @@ -58,7 +58,7 @@ class UserController | |||
| 58 | } | 58 | } |
| 59 | unset($_SESSION['user']); | 59 | unset($_SESSION['user']); |
| 60 | 60 | ||
| 61 | $form = new FormValidation($_POST, 'connection'); | 61 | $form = new FormValidation($_POST, ValidationStrategy::Connection); |
| 62 | 62 | ||
| 63 | $error = ''; | 63 | $error = ''; |
| 64 | if($form->validate()){ | 64 | if($form->validate()){ |
| @@ -135,7 +135,7 @@ class UserController | |||
| 135 | $url = new URL(['page' => 'user_edit']); | 135 | $url = new URL(['page' => 'user_edit']); |
| 136 | isset($_GET['from']) ? $url->addParams(['from' => $_GET['from']]) : null; | 136 | isset($_GET['from']) ? $url->addParams(['from' => $_GET['from']]) : null; |
| 137 | 137 | ||
| 138 | $form = new FormValidation($_POST, 'username_update'); | 138 | $form = new FormValidation($_POST, ValidationStrategy::UsernameUpdate); |
| 139 | 139 | ||
| 140 | $error = ''; | 140 | $error = ''; |
| 141 | if($form->validate()){ | 141 | if($form->validate()){ |
| @@ -174,7 +174,7 @@ class UserController | |||
| 174 | $url = new URL(['page' => 'user_edit']); | 174 | $url = new URL(['page' => 'user_edit']); |
| 175 | isset($_GET['from']) ? $url->addParams(['from' => $_GET['from']]) : null; | 175 | isset($_GET['from']) ? $url->addParams(['from' => $_GET['from']]) : null; |
| 176 | 176 | ||
| 177 | $form = new FormValidation($_POST, 'password_update'); | 177 | $form = new FormValidation($_POST, ValidationStrategy::PasswordUpdate); |
| 178 | 178 | ||
| 179 | $error = ''; | 179 | $error = ''; |
| 180 | if($form->validate()){ | 180 | if($form->validate()){ |
diff --git a/src/service/FormValidation.php b/src/service/FormValidation.php index cdd1ae3..9646a6b 100644 --- a/src/service/FormValidation.php +++ b/src/service/FormValidation.php | |||
| @@ -1,35 +1,50 @@ | |||
| 1 | <?php | 1 | <?php |
| 2 | // src/service/FormValidation.php | 2 | // src/service/FormValidation.php |
| 3 | // | ||
| 4 | // pattern strategy simplifié, permet évolution future vers le vrai pattern | ||
| 3 | 5 | ||
| 4 | declare(strict_types=1); | 6 | declare(strict_types=1); |
| 5 | 7 | ||
| 8 | // classe spéciale permettant de bénéficier du typage de classe au lieu de passer une chaîne à FormValidation | ||
| 9 | enum ValidationStrategy { | ||
| 10 | // bloc formulaire de contact | ||
| 11 | case EmailSend; | ||
| 12 | case EmailParam; | ||
| 13 | // formulaires pages spéciales | ||
| 14 | case CreateUser; | ||
| 15 | case Connection; | ||
| 16 | case UsernameUpdate; | ||
| 17 | case PasswordUpdate; | ||
| 18 | |||
| 19 | public function method(): string | ||
| 20 | { | ||
| 21 | return match($this){ | ||
| 22 | self::EmailSend => 'emailStrategy', | ||
| 23 | self::EmailParam => 'emailParamStrategy', | ||
| 24 | self::CreateUser => 'createUserStrategy', | ||
| 25 | self::Connection => 'connectionStrategy', | ||
| 26 | self::UsernameUpdate => 'usernameUpdateStrategy', | ||
| 27 | self::PasswordUpdate => 'passwordUpdateStrategy' | ||
| 28 | }; | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 6 | class FormValidation | 32 | class FormValidation |
| 7 | { | 33 | { |
| 8 | private array $data; // tableau associatif (probablement $_POST) | 34 | private array $data; // tableau associatif (probablement $_POST) |
| 9 | private Closure $callStrategy; | 35 | private Closure $callStrategy; |
| 10 | private array $errors; | 36 | private array $errors; |
| 11 | private bool $validated = false; | ||
| 12 | 37 | ||
| 13 | public function __construct(array $data, string $strategy_name){ | 38 | public function __construct(array $data, ValidationStrategy $strategy){ |
| 14 | $this->data = $data; | 39 | $this->data = $data; |
| 15 | $this->callStrategy = match($strategy_name){ | 40 | $method = $strategy->method(); |
| 16 | // bloc formulaire de contact | 41 | $this->callStrategy = fn() => $this->$method(); |
| 17 | 'email_send' => fn() => $this->emailStrategy(), // raccourci pour: 'email_send' => function () { return $this->emailStrategy(); }, | ||
| 18 | 'email_params' => fn() => $this->emailParamsStrategy(), | ||
| 19 | // formulaires pages spéciales | ||
| 20 | 'create_user' => fn() => $this->createUserStrategy(), | ||
| 21 | 'connection' => fn() => $this->connectionStrategy(), | ||
| 22 | 'username_update' => fn() => $this->usernameUpdateStrategy(), | ||
| 23 | 'password_update' => fn() => $this->passwordUpdateStrategy(), | ||
| 24 | default => throw new LogicException("server_error_strategy_not_found") | ||
| 25 | }; | ||
| 26 | } | 42 | } |
| 27 | 43 | ||
| 28 | public function validate(): bool | 44 | public function validate(): bool |
| 29 | { | 45 | { |
| 30 | $this->errors = []; | 46 | $this->errors = []; |
| 31 | ($this->callStrategy)(); | 47 | ($this->callStrategy)(); // appel de la callback |
| 32 | $this->validated = true; | ||
| 33 | return empty($this->errors); | 48 | return empty($this->errors); |
| 34 | } | 49 | } |
| 35 | 50 | ||
| @@ -40,7 +55,7 @@ class FormValidation | |||
| 40 | 55 | ||
| 41 | public function getField(string $field): string | 56 | public function getField(string $field): string |
| 42 | { | 57 | { |
| 43 | return $this->validated ? $this->data[$field] : ''; | 58 | return $this->data[$field] ?? ''; |
| 44 | } | 59 | } |
| 45 | 60 | ||
| 46 | // méthodes de validation | 61 | // méthodes de validation |
| @@ -76,9 +91,7 @@ class FormValidation | |||
| 76 | { | 91 | { |
| 77 | $this->captchaValidate(false); | 92 | $this->captchaValidate(false); |
| 78 | 93 | ||
| 79 | if(!isset($this->data['name']) || empty($this->data['name']) | 94 | if(empty($this->data['name']) || empty($this->data['email']) || empty($this->data['message']) |
| 80 | || !isset($this->data['email']) || empty($this->data['email']) | ||
| 81 | || !isset($this->data['message']) || empty($this->data['message']) | ||
| 82 | || !isset($this->data['hidden']) || !empty($this->data['hidden'])){ | 95 | || !isset($this->data['hidden']) || !empty($this->data['hidden'])){ |
| 83 | $this->errors[] = 'missing_fields'; | 96 | $this->errors[] = 'missing_fields'; |
| 84 | } | 97 | } |
| @@ -91,34 +104,28 @@ class FormValidation | |||
| 91 | $this->data['email'] = htmlspecialchars(trim($this->data['email'])); | 104 | $this->data['email'] = htmlspecialchars(trim($this->data['email'])); |
| 92 | $this->data['message'] = htmlspecialchars($this->data['message']); | 105 | $this->data['message'] = htmlspecialchars($this->data['message']); |
| 93 | } | 106 | } |
| 94 | private function emailParamsStrategy(): void | 107 | private function emailParamStrategy(): void |
| 95 | { | 108 | { |
| 96 | if(!isset($this->data['id'], $this->data['what_param'], $this->data['value'], $this->data['hidden']) | 109 | if(empty($this->data['id']) || empty($this->data['what_param']) || empty($this->data['value']) |
| 97 | || !empty($this->data['hidden'])){ | 110 | || !isset($this->data['hidden']) || !empty($this->data['hidden'])){ |
| 98 | $this->errors[] = 'missing_fields'; | 111 | $this->errors[] = 'missing_fields'; |
| 99 | } | 112 | } |
| 100 | 113 | ||
| 101 | elseif($this->data['value'] !== ''){ | 114 | if(!in_array($this->data['what_param'], ['smtp_host', 'smtp_secure', 'smtp_username', 'smtp_password', 'email_dest'])){ |
| 102 | if(!in_array($this->data['what_param'], ['smtp_host', 'smtp_secure', 'smtp_username', 'smtp_password', 'email_dest'])){ | 115 | $this->errors[] = 'unknown_parameter'; |
| 103 | $this->errors[] = 'unknown_parameter'; | 116 | } |
| 104 | } | 117 | elseif($this->data['what_param'] === 'smtp_username' || $this->data['what_param'] === 'email_dest'){ |
| 105 | elseif($this->data['what_param'] === 'smtp_username' || $this->data['what_param'] === 'email_dest'){ | 118 | if(!filter_var($this->data['value'], FILTER_VALIDATE_EMAIL)){ |
| 106 | if(!filter_var($this->data['value'], FILTER_VALIDATE_EMAIL)){ | 119 | $this->errors[] = 'invalide_email_address'; |
| 107 | $this->errors[] = 'invalide_email_address'; | ||
| 108 | } | ||
| 109 | } | 120 | } |
| 110 | } | 121 | } |
| 111 | |||
| 112 | // htmlspecialchars exécutés à l'affichage dans FormBuilder | ||
| 113 | } | 122 | } |
| 114 | private function createUserStrategy(): void | 123 | private function createUserStrategy(): void |
| 115 | { | 124 | { |
| 116 | $this->captchaValidate(); | 125 | $this->captchaValidate(); |
| 117 | 126 | ||
| 118 | // test mauvais paramètres | 127 | // test mauvais paramètres |
| 119 | if(!isset($this->data['login']) || empty($this->data['login']) | 128 | if(empty($this->data['login']) || empty($this->data['password']) || empty($this->data['password_confirmation']) |
| 120 | || !isset($this->data['password']) || empty($this->data['password']) | ||
| 121 | || !isset($this->data['password_confirmation']) || empty($this->data['password_confirmation']) | ||
| 122 | || !isset($this->data['create_user_hidden']) || !empty($this->data['create_user_hidden'])) | 129 | || !isset($this->data['create_user_hidden']) || !empty($this->data['create_user_hidden'])) |
| 123 | { | 130 | { |
| 124 | $this->errors[] = 'bad_login_or_password'; | 131 | $this->errors[] = 'bad_login_or_password'; |
| @@ -137,8 +144,7 @@ class FormValidation | |||
| 137 | { | 144 | { |
| 138 | $this->captchaValidate(); | 145 | $this->captchaValidate(); |
| 139 | 146 | ||
| 140 | if(!isset($this->data['login']) || empty($this->data['login']) | 147 | if(empty($this->data['login']) || empty($this->data['password']) |
| 141 | || !isset($this->data['password']) || empty($this->data['password']) | ||
| 142 | || !isset($this->data['connection_hidden']) || !empty($this->data['connection_hidden'])) | 148 | || !isset($this->data['connection_hidden']) || !empty($this->data['connection_hidden'])) |
| 143 | { | 149 | { |
| 144 | $this->errors[] = 'bad_login_or_password'; | 150 | $this->errors[] = 'bad_login_or_password'; |
| @@ -148,9 +154,7 @@ class FormValidation | |||
| 148 | { | 154 | { |
| 149 | $this->captchaValidate(); | 155 | $this->captchaValidate(); |
| 150 | 156 | ||
| 151 | if(!isset($this->data['login']) || empty($this->data['login']) | 157 | if(empty($this->data['login']) || empty($this->data['password']) || empty($this->data['new_login']) |
| 152 | || !isset($this->data['password']) || empty($this->data['password']) | ||
| 153 | || !isset($this->data['new_login']) || empty($this->data['new_login']) | ||
| 154 | || !isset($this->data['modify_username_hidden']) || !empty($this->data['modify_username_hidden'])) | 158 | || !isset($this->data['modify_username_hidden']) || !empty($this->data['modify_username_hidden'])) |
| 155 | { | 159 | { |
| 156 | $this->errors[] = 'bad_login_or_password'; | 160 | $this->errors[] = 'bad_login_or_password'; |
| @@ -172,9 +176,7 @@ class FormValidation | |||
| 172 | { | 176 | { |
| 173 | $this->captchaValidate(); | 177 | $this->captchaValidate(); |
| 174 | 178 | ||
| 175 | if(!isset($this->data['login']) || empty($this->data['login']) | 179 | if(empty($this->data['login']) || empty($this->data['password']) || empty($this->data['new_password']) |
| 176 | || !isset($this->data['password']) || empty($this->data['password']) | ||
| 177 | || !isset($this->data['new_password']) || empty($this->data['new_password']) | ||
| 178 | || !isset($this->data['modify_password_hidden']) || !empty($this->data['modify_password_hidden'])) | 180 | || !isset($this->data['modify_password_hidden']) || !empty($this->data['modify_password_hidden'])) |
| 179 | { | 181 | { |
| 180 | $this->errors[] = 'bad_login_or_password'; | 182 | $this->errors[] = 'bad_login_or_password'; |
