diff options
| author | polo <ordipolo@gmx.fr> | 2026-07-26 00:31:39 +0200 |
|---|---|---|
| committer | polo <ordipolo@gmx.fr> | 2026-07-26 00:31:39 +0200 |
| commit | 0458c5dc4a8e36737f45d6813af24b08f0958688 (patch) | |
| tree | 76099198499b495d2d7be863fd675fb035b95786 /src | |
| parent | d0c39f9c0d2724111a7b1019eacb1b675fd5fd94 (diff) | |
| download | cms-0458c5dc4a8e36737f45d6813af24b08f0958688.tar.gz cms-0458c5dc4a8e36737f45d6813af24b08f0958688.tar.bz2 cms-0458c5dc4a8e36737f45d6813af24b08f0958688.zip | |
contrôle du nom de stratégie dans le constructeur
Diffstat (limited to 'src')
| -rw-r--r-- | src/service/FormValidation.php | 52 |
1 files changed, 16 insertions, 36 deletions
diff --git a/src/service/FormValidation.php b/src/service/FormValidation.php index 4677bef..cdd1ae3 100644 --- a/src/service/FormValidation.php +++ b/src/service/FormValidation.php | |||
| @@ -6,49 +6,29 @@ declare(strict_types=1); | |||
| 6 | class FormValidation | 6 | class FormValidation |
| 7 | { | 7 | { |
| 8 | private array $data; // tableau associatif (probablement $_POST) | 8 | private array $data; // tableau associatif (probablement $_POST) |
| 9 | private string $validation_strategy; // à remplacer plus tard par un objet (pattern stratégie) d'interface ValidationStrategy | 9 | private Closure $callStrategy; |
| 10 | private array $errors; | 10 | private array $errors; |
| 11 | private bool $validated = false; | 11 | private bool $validated = false; |
| 12 | 12 | ||
| 13 | public function __construct(array $data, string $validation_strategy){ | 13 | public function __construct(array $data, string $strategy_name){ |
| 14 | $this->data = $data; | 14 | $this->data = $data; |
| 15 | $this->validation_strategy = $validation_strategy; | 15 | $this->callStrategy = match($strategy_name){ |
| 16 | // bloc formulaire de contact | ||
| 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 | }; | ||
| 16 | } | 26 | } |
| 17 | 27 | ||
| 18 | public function validate(): bool | 28 | public function validate(): bool |
| 19 | { | 29 | { |
| 20 | $this->errors = []; | 30 | $this->errors = []; |
| 21 | 31 | ($this->callStrategy)(); | |
| 22 | // pattern stratégie en une seule classe | ||
| 23 | switch($this->validation_strategy){ | ||
| 24 | // bloc formulaire de contact | ||
| 25 | case 'email_send': | ||
| 26 | $this->emailStrategy(); | ||
| 27 | break; | ||
| 28 | case 'email_params': // paramètrage en mode admin | ||
| 29 | $this->emailParamsStrategy(); | ||
| 30 | break; | ||
| 31 | |||
| 32 | // formulaires pages spéciales | ||
| 33 | case 'create_user': | ||
| 34 | $this->createUserStrategy(); | ||
| 35 | break; | ||
| 36 | case 'connection': | ||
| 37 | $this->connectionStrategy(); | ||
| 38 | break; | ||
| 39 | case 'username_update': | ||
| 40 | $this->usernameUpdateStrategy(); | ||
| 41 | break; | ||
| 42 | case 'password_update': | ||
| 43 | $this->passwordUpdateStrategy(); | ||
| 44 | break; | ||
| 45 | |||
| 46 | default: | ||
| 47 | http_response_code(500); // c'est un peu comme jeter une exception | ||
| 48 | echo json_encode(['success' => false, 'error' => 'server_error']); | ||
| 49 | die; | ||
| 50 | } | ||
| 51 | |||
| 52 | $this->validated = true; | 32 | $this->validated = true; |
| 53 | return empty($this->errors); | 33 | return empty($this->errors); |
| 54 | } | 34 | } |
| @@ -73,10 +53,10 @@ class FormValidation | |||
| 73 | } | 53 | } |
| 74 | 54 | ||
| 75 | if($captcha_try == 0){ | 55 | if($captcha_try == 0){ |
| 76 | $error = 'error_non_valid_captcha'; | 56 | $this->errors[] = 'error_non_valid_captcha'; |
| 77 | } | 57 | } |
| 78 | elseif($captcha_solution == 0){ // ne peut pas arriver, si? | 58 | elseif($captcha_solution == 0){ // ne peut pas arriver, si? |
| 79 | $error = 'captcha_server_error'; | 59 | $this->errors[] = 'captcha_server_error'; |
| 80 | } | 60 | } |
| 81 | elseif($captcha_try !== $captcha_solution){ | 61 | elseif($captcha_try !== $captcha_solution){ |
| 82 | $this->errors[] = 'bad_solution_captcha'; | 62 | $this->errors[] = 'bad_solution_captcha'; |
