diff options
Diffstat (limited to 'src/service/FormValidation.php')
| -rw-r--r-- | src/service/FormValidation.php | 88 |
1 files changed, 45 insertions, 43 deletions
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'; |
