validator.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Form;
  4. final class Validator
  5. {
  6. /** @var array<string, array<string, mixed>> */
  7. private array $fields;
  8. public function __construct(FormSchema $schema)
  9. {
  10. $this->fields = $schema->getAllFields();
  11. }
  12. /**
  13. * @param array<string, mixed> $data
  14. * @param array<string, array<int, array<string, mixed>>> $uploads
  15. * @return array<string, string>
  16. */
  17. public function validateSubmit(array $data, array $uploads): array
  18. {
  19. $errors = [];
  20. foreach ($this->fields as $key => $field) {
  21. $type = $field['type'] ?? 'text';
  22. $visible = $this->isVisible($field, $data);
  23. if (!$visible) {
  24. continue;
  25. }
  26. $required = $this->isRequired($field, $data);
  27. if ($type === 'file') {
  28. $hasFile = !empty($uploads[$key]);
  29. if ($required && !$hasFile) {
  30. $errors[$key] = 'Dieses Upload-Feld ist erforderlich.';
  31. }
  32. continue;
  33. }
  34. $value = $data[$key] ?? null;
  35. if ($required && $this->isEmptyValue($value, $type, $field)) {
  36. $errors[$key] = 'Dieses Feld ist erforderlich.';
  37. continue;
  38. }
  39. if ($this->isEmptyValue($value, $type, $field)) {
  40. continue;
  41. }
  42. if ($type === 'email' && filter_var((string) $value, FILTER_VALIDATE_EMAIL) === false) {
  43. $errors[$key] = 'Bitte eine gültige E-Mail-Adresse eingeben.';
  44. }
  45. if ($type === 'select' && isset($field['options']) && is_array($field['options'])) {
  46. $selected = (string) $value;
  47. $validSelection = false;
  48. foreach ($field['options'] as $option) {
  49. if (!is_array($option)) {
  50. continue;
  51. }
  52. if ((string) ($option['value'] ?? '') !== $selected) {
  53. continue;
  54. }
  55. $validSelection = $this->isOptionVisible($option, $data);
  56. break;
  57. }
  58. if (!$validSelection) {
  59. $errors[$key] = 'Ungültige Auswahl.';
  60. }
  61. }
  62. if (isset($field['max_length']) && is_int($field['max_length'])) {
  63. if (strlen((string) $value) > $field['max_length']) {
  64. $errors[$key] = 'Eingabe ist zu lang.';
  65. }
  66. }
  67. }
  68. return $errors;
  69. }
  70. /** @param array<string, mixed> $field */
  71. private function isRequired(array $field, array $data): bool
  72. {
  73. if (!$this->isVisible($field, $data)) {
  74. return false;
  75. }
  76. if (($field['required'] ?? false) === true) {
  77. return true;
  78. }
  79. if (!isset($field['required_if']) || !is_array($field['required_if'])) {
  80. return false;
  81. }
  82. return $this->ruleMatches($field['required_if'], $data);
  83. }
  84. /** @param array<string, mixed> $field */
  85. private function isVisible(array $field, array $data): bool
  86. {
  87. if (!isset($field['visible_if']) || !is_array($field['visible_if'])) {
  88. return true;
  89. }
  90. return $this->ruleMatches($field['visible_if'], $data);
  91. }
  92. /** @param array<string, mixed> $rule */
  93. private function ruleMatches(array $rule, array $data): bool
  94. {
  95. $sourceField = (string) ($rule['field'] ?? '');
  96. $equals = (string) ($rule['equals'] ?? '');
  97. if ($sourceField === '') {
  98. return false;
  99. }
  100. return $this->resolveRuleValue($sourceField, $data) === $equals;
  101. }
  102. /** @param array<string, mixed> $data */
  103. private function resolveRuleValue(string $sourceField, array $data): string
  104. {
  105. if (array_key_exists($sourceField, $data)) {
  106. return (string) ($data[$sourceField] ?? '');
  107. }
  108. $derived = $this->derivedRuleValues($data);
  109. return (string) ($derived[$sourceField] ?? '');
  110. }
  111. /**
  112. * @param array<string, mixed> $data
  113. * @return array<string, string>
  114. */
  115. private function derivedRuleValues(array $data): array
  116. {
  117. $adultFlag = $this->deriveAdultFlag((string) ($data['geburtsdatum'] ?? ''));
  118. if ($adultFlag === null) {
  119. return [
  120. 'is_minor' => '',
  121. ];
  122. }
  123. return [
  124. 'is_minor' => $adultFlag ? '0' : '1',
  125. ];
  126. }
  127. private function deriveAdultFlag(string $birthdate): ?bool
  128. {
  129. $birthdate = trim($birthdate);
  130. if ($birthdate === '') {
  131. return null;
  132. }
  133. $date = \DateTimeImmutable::createFromFormat('!Y-m-d', $birthdate);
  134. if (!$date || $date->format('Y-m-d') !== $birthdate) {
  135. return null;
  136. }
  137. $today = new \DateTimeImmutable('today');
  138. if ($date > $today) {
  139. return null;
  140. }
  141. $age = $date->diff($today)->y;
  142. return $age >= 18;
  143. }
  144. /** @param array<string, mixed> $option */
  145. private function isOptionVisible(array $option, array $data): bool
  146. {
  147. if (isset($option['visible_if']) && is_array($option['visible_if']) && !$this->ruleMatches($option['visible_if'], $data)) {
  148. return false;
  149. }
  150. if (isset($option['hidden_if']) && is_array($option['hidden_if']) && $this->ruleMatches($option['hidden_if'], $data)) {
  151. return false;
  152. }
  153. return true;
  154. }
  155. /** @param array<string, mixed> $field */
  156. private function isEmptyValue(mixed $value, string $type, array $field): bool
  157. {
  158. if ($type === 'checkbox') {
  159. return !in_array((string) $value, ['1', 'on', 'true'], true);
  160. }
  161. if ($type === 'table') {
  162. return $this->isTableValueEmpty($value, $field);
  163. }
  164. return $value === null || trim((string) $value) === '';
  165. }
  166. /** @param array<string, mixed> $field */
  167. private function isTableValueEmpty(mixed $value, array $field): bool
  168. {
  169. $raw = trim((string) $value);
  170. if ($raw === '') {
  171. return true;
  172. }
  173. $lines = preg_split('/\R/', $raw);
  174. if (!is_array($lines)) {
  175. return true;
  176. }
  177. $rows = [];
  178. foreach ($lines as $line) {
  179. $line = trim((string) $line);
  180. if ($line === '') {
  181. continue;
  182. }
  183. $rows[] = str_getcsv($line);
  184. }
  185. if (empty($rows)) {
  186. return true;
  187. }
  188. $columnHeaders = [];
  189. if (isset($field['columns']) && is_array($field['columns'])) {
  190. foreach ($field['columns'] as $column) {
  191. if (!is_array($column)) {
  192. continue;
  193. }
  194. $label = trim((string) ($column['label'] ?? ''));
  195. if ($label !== '') {
  196. $columnHeaders[] = $label;
  197. }
  198. }
  199. }
  200. $dataRows = $rows;
  201. if (!empty($columnHeaders) && $this->tableRowMatchesHeader($rows[0], $columnHeaders)) {
  202. $dataRows = array_slice($rows, 1);
  203. }
  204. if (empty($dataRows)) {
  205. return true;
  206. }
  207. foreach ($dataRows as $dataRow) {
  208. foreach ((array) $dataRow as $cell) {
  209. if (trim((string) $cell) !== '') {
  210. return false;
  211. }
  212. }
  213. }
  214. return true;
  215. }
  216. /**
  217. * @param array<int, string> $row
  218. * @param array<int, string> $header
  219. */
  220. private function tableRowMatchesHeader(array $row, array $header): bool
  221. {
  222. if (count($row) !== count($header)) {
  223. return false;
  224. }
  225. foreach ($header as $index => $headerValue) {
  226. if (strtolower(trim((string) ($row[$index] ?? ''))) !== strtolower(trim($headerValue))) {
  227. return false;
  228. }
  229. }
  230. return true;
  231. }
  232. }