Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
30.43% |
21 / 69 |
|
30.00% |
6 / 20 |
CRAP | |
0.00% |
0 / 1 |
| Form | |
30.43% |
21 / 69 |
|
30.00% |
6 / 20 |
695.75 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| generateCsrf | |
62.50% |
5 / 8 |
|
0.00% |
0 / 1 |
3.47 | |||
| csrfSessionName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| csrfName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| validateCsrf | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| name | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| fields | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addFields | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| required | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setRequired | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| addValidator | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| process | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| bind | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| value | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
| values | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setValues | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| addValues | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addError | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| validate | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
132 | |||
| error | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Dynart\Micro; |
| 4 | |
| 5 | /** |
| 6 | * Represents a form |
| 7 | * @package Dynart\Micro |
| 8 | */ |
| 9 | class Form { |
| 10 | |
| 11 | /** |
| 12 | * Stores the name of the form |
| 13 | * @var string |
| 14 | */ |
| 15 | protected $name = 'form'; |
| 16 | |
| 17 | /** |
| 18 | * Is this form uses CSRF? |
| 19 | * @var bool |
| 20 | */ |
| 21 | protected $csrf = true; |
| 22 | |
| 23 | /** |
| 24 | * Holds the fields |
| 25 | * @var array |
| 26 | */ |
| 27 | protected $fields = []; |
| 28 | |
| 29 | /** |
| 30 | * A list of the required field names |
| 31 | * @var array |
| 32 | */ |
| 33 | protected $required = []; |
| 34 | |
| 35 | /** |
| 36 | * The values of the fields in [name => value] format |
| 37 | * @var array |
| 38 | */ |
| 39 | protected $values = []; |
| 40 | |
| 41 | /** |
| 42 | * The error messages of the fields in [name => message] format |
| 43 | * @var array |
| 44 | */ |
| 45 | protected $errors = []; |
| 46 | |
| 47 | /** |
| 48 | * Validators for the fields in [name => [validator1, validator2]] format |
| 49 | * @var Validator[][] |
| 50 | */ |
| 51 | protected $validators = []; |
| 52 | |
| 53 | /** @var Session */ |
| 54 | protected $session; |
| 55 | |
| 56 | /** @var Request */ |
| 57 | protected $request; |
| 58 | |
| 59 | /** |
| 60 | * Creates the form with given name and `$csrf` value |
| 61 | * @param Request $request The HTTP request |
| 62 | * @param Session $session The session used for the CSRF check |
| 63 | * @param string $name The name of the form, can be an empty string (usually for filter forms) |
| 64 | * @param bool $csrf Is the form should use a CSRF field and validate it on `process()`? |
| 65 | */ |
| 66 | public function __construct(Request $request, Session $session, string $name = 'form', bool $csrf = true) { |
| 67 | $this->request = $request; |
| 68 | $this->session = $session; |
| 69 | $this->name = $name; |
| 70 | $this->csrf = $csrf; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * If the `$csrf` is true, generates a CSRF field and a CSRF value in the session |
| 75 | * @throws MicroException If couldn't gather sufficient entropy for random_bytes |
| 76 | */ |
| 77 | public function generateCsrf() { |
| 78 | if (!$this->csrf) { |
| 79 | return; |
| 80 | } |
| 81 | try { |
| 82 | $value = bin2hex(random_bytes(128)); |
| 83 | } catch (\Exception $e) { |
| 84 | throw new MicroException("Couldn't gather sufficient entropy"); |
| 85 | } |
| 86 | $this->addFields([$this->csrfName() => ['type' => 'hidden']]); |
| 87 | $this->setValues([$this->csrfName() => $value]); |
| 88 | $this->session->set($this->csrfSessionName(), $value); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns with the CSRF session name |
| 93 | * @return string |
| 94 | */ |
| 95 | public function csrfSessionName() { |
| 96 | return 'form.'.$this->name.'.csrf'; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns with the CSRF field name |
| 101 | * @return string |
| 102 | */ |
| 103 | public function csrfName() { |
| 104 | return '_csrf'; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns true if the CSRF session value equals with the CSRF field value |
| 109 | * @return bool |
| 110 | */ |
| 111 | public function validateCsrf() { |
| 112 | return $this->csrf |
| 113 | ? $this->session->get($this->csrfSessionName()) == $this->value($this->csrfName()) |
| 114 | : true; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Returns the name of this form |
| 119 | * @return string |
| 120 | */ |
| 121 | public function name() { |
| 122 | return $this->name; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Returns the fields of this form in [name => [field_data]] format |
| 127 | * @return array |
| 128 | */ |
| 129 | public function fields() { |
| 130 | return $this->fields; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Adds fields to the form (merges them with the existing ones) |
| 135 | * @param array $fields The fields in [name => [field_data]] format |
| 136 | * @param bool $required Is this field required to be filled out? |
| 137 | */ |
| 138 | public function addFields(array $fields, $required = true) { |
| 139 | $this->fields = array_merge($this->fields, $fields); |
| 140 | if ($required) { |
| 141 | $this->required = array_merge($this->required, array_keys($fields)); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Returns wether a field must be filled or not |
| 147 | * @param string $name |
| 148 | * @return bool If true the field must be filled out |
| 149 | */ |
| 150 | public function required(string $name) { |
| 151 | return in_array($name, $this->required); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Sets a field to be required or not |
| 156 | * @param string $name The name of the field |
| 157 | * @param bool $required Is it required? |
| 158 | */ |
| 159 | public function setRequired(string $name, bool $required) { |
| 160 | if ($required) { |
| 161 | if (!in_array($name, $this->required)) { |
| 162 | $this->required[] = $name; |
| 163 | } |
| 164 | } else { |
| 165 | $this->required = array_diff($this->required, [$name]); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Adds a validator for a field |
| 171 | * @param string $name The name of the field |
| 172 | * @param Validator $validator The validator |
| 173 | */ |
| 174 | public function addValidator(string $name, Validator $validator) { |
| 175 | if (!isset($this->validators[$name])) { |
| 176 | $this->validators[$name] = []; |
| 177 | } |
| 178 | $this->validators[$name][] = $validator; |
| 179 | $validator->setForm($this); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Processes a form if the request method is `$httpMethod`, adds the CSRF field if `$csrf` is true |
| 184 | * @param string $httpMethod The required HTTP method |
| 185 | * @return bool Returns true if the form is valid |
| 186 | */ |
| 187 | public function process(string $httpMethod = 'POST'): bool { |
| 188 | $result = false; |
| 189 | if ($this->request->httpMethod() == $httpMethod) { |
| 190 | $this->bind(); |
| 191 | $result = $this->validate(); |
| 192 | } |
| 193 | $this->generateCsrf(); |
| 194 | return $result; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Binds the request values to the field values |
| 199 | * |
| 200 | * If the form has a name it will use the `form_name[]` value from the request, |
| 201 | * otherwise: one field name one request parameter name. |
| 202 | */ |
| 203 | public function bind(): void { |
| 204 | if ($this->name) { |
| 205 | $this->values = $this->request->get($this->name, []); |
| 206 | } else { |
| 207 | foreach ($this->fields as $name => $field) { |
| 208 | $this->values[$name] = $this->request->get($name); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Returns a value for a field |
| 215 | * @param string $name The name of the field |
| 216 | * @param bool $escape Should the value to be escaped for a HTML attribute? |
| 217 | * @return null|string The value of the field |
| 218 | */ |
| 219 | public function value(string $name, $escape = false) { |
| 220 | $value = null; |
| 221 | if (array_key_exists($name, $this->values)) { |
| 222 | $value = $this->values[$name]; |
| 223 | if ($escape) { |
| 224 | $value = htmlspecialchars($value, ENT_QUOTES); |
| 225 | } |
| 226 | } |
| 227 | return $value; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Returns with the values for the fields in [name => value] form |
| 232 | * @return array |
| 233 | */ |
| 234 | public function values(): array { |
| 235 | return $this->values; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Sets the values for the fields (clears the previous ones) |
| 240 | * @param array $values |
| 241 | */ |
| 242 | public function setValues(array $values): void { |
| 243 | $this->values = $values; |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Adds the values for the fields (merges them with the existing ones) |
| 248 | * @param array $values |
| 249 | */ |
| 250 | public function addValues(array $values): void { |
| 251 | $this->values = array_merge($this->values, $values); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Adds an error to the form itself |
| 256 | * @param string $error |
| 257 | */ |
| 258 | public function addError(string $error): void { |
| 259 | if (!isset($this->errors['_form'])) { |
| 260 | $this->errors['_form'] = []; |
| 261 | } |
| 262 | $this->errors['_form'][] = $error; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Runs the validators per field if the field is required or has value |
| 267 | * |
| 268 | * If one validator fails for a field the other validators will NOT run for that field. |
| 269 | * |
| 270 | * @return bool The form validation was successful? |
| 271 | */ |
| 272 | public function validate(): bool { |
| 273 | if (!$this->validateCsrf()) { |
| 274 | $this->addError('CSRF token is invalid.'); |
| 275 | } |
| 276 | foreach (array_keys($this->fields) as $name) { |
| 277 | if ($this->required($name) && !$this->value($name)) { |
| 278 | $this->errors[$name] = 'Required.'; // TODO: Translation |
| 279 | } |
| 280 | } |
| 281 | foreach ($this->validators as $name => $validators) { |
| 282 | if (isset($this->errors[$name])) { |
| 283 | continue; |
| 284 | } |
| 285 | if (!$this->value($name) && !$this->required($name)) { |
| 286 | continue; |
| 287 | } |
| 288 | foreach ($validators as $validator) { |
| 289 | if (!$validator->validate($this->value($name))) { |
| 290 | $this->errors[$name] = $validator->message(); |
| 291 | break; |
| 292 | } |
| 293 | } |
| 294 | } |
| 295 | return empty($this->errors); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Returns an error message for a field |
| 300 | * @param string $name The field name |
| 301 | * @return string|null The error message or null |
| 302 | */ |
| 303 | public function error(string $name) { |
| 304 | return isset($this->errors[$name]) ? $this->errors[$name] : null; |
| 305 | } |
| 306 | |
| 307 | } |