FormFieldRulesBuilder.php
142 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Kirki\App\Supports\Form; |
| 4 | |
| 5 | defined('ABSPATH') || exit; |
| 6 | |
| 7 | use Kirki\App\Constants\Form\FormFieldTypes; |
| 8 | use Kirki\App\Rules\Form\AcceptedFileTypeRule; |
| 9 | use Kirki\App\Rules\Form\MaxFileSizeRule; |
| 10 | |
| 11 | /** |
| 12 | * Turns a Kirki form's `fields` configuration into rules the framework |
| 13 | * Validator understands. |
| 14 | * |
| 15 | * A field's `type` maps to a format rule (email, number, ...) and `required` |
| 16 | * toggles between the `required`/`nullable` rule — the only two constraints |
| 17 | * the form builder actually lets users configure. File fields get their own |
| 18 | * rule set (accepted type + max size) since their value lives in `$_FILES`, |
| 19 | * not the submitted form data. |
| 20 | */ |
| 21 | class FormFieldRulesBuilder |
| 22 | { |
| 23 | /** |
| 24 | * The format rule for each field type that maps directly onto a single |
| 25 | * Validator rule. |
| 26 | * |
| 27 | * @var array<string, string> |
| 28 | */ |
| 29 | protected const FORMAT_RULES = [ |
| 30 | FormFieldTypes::EMAIL => 'email', |
| 31 | FormFieldTypes::NUMBER => 'number', |
| 32 | FormFieldTypes::DATE => 'date', |
| 33 | // Preserves the legacy (imprecise) datetime-local pattern rather than |
| 34 | // the stricter `datetime` rule, to avoid rejecting values the old |
| 35 | // validator accepted. |
| 36 | FormFieldTypes::DATETIME_LOCAL => 'regex:/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/', |
| 37 | FormFieldTypes::TEL => 'regex:/^[0-9]{10}+$/', |
| 38 | ]; |
| 39 | |
| 40 | /** |
| 41 | * Build the Validator rules for a form's fields. |
| 42 | * |
| 43 | * @param array $fields The form's field configuration. |
| 44 | * @return array<string, array<int, string>> |
| 45 | */ |
| 46 | public static function rules(array $fields) |
| 47 | { |
| 48 | $rules = []; |
| 49 | |
| 50 | foreach ($fields as $name => $field) { |
| 51 | $rules[$name] = ($field['type'] ?? null) === FormFieldTypes::FILE |
| 52 | ? static::rules_for_file_field($field) |
| 53 | : static::rules_for_field($field); |
| 54 | } |
| 55 | |
| 56 | return $rules; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Build the data to validate: submitted form data with empty values |
| 61 | * normalized to null, and file fields' values pulled from `$_FILES`. |
| 62 | * |
| 63 | * The Validator only skips format rules for a field it never received; |
| 64 | * an empty string is still "received". Normalizing empty values to null |
| 65 | * here keeps optional fields from failing format checks while `required` |
| 66 | * still catches them via its own missing-value check. |
| 67 | * |
| 68 | * @param array $form_data The submitted form data. |
| 69 | * @param array $fields The form's field configuration. |
| 70 | * @param array $files The file array of our own file class. |
| 71 | * @return array |
| 72 | */ |
| 73 | public static function data_for_validation(array $form_data, array $fields, array $files = []) |
| 74 | { |
| 75 | foreach ($files as $field_name => $file) { |
| 76 | $field = $fields[$field_name] ?? []; |
| 77 | |
| 78 | if (($field['type'] ?? null) !== FormFieldTypes::FILE) { |
| 79 | continue; |
| 80 | } |
| 81 | |
| 82 | if (!$file->is_valid()) { |
| 83 | $form_data[$field_name] = null; |
| 84 | continue; |
| 85 | } |
| 86 | |
| 87 | $form_data[$field_name] = $file; |
| 88 | } |
| 89 | |
| 90 | foreach ($fields as $name => $field) { |
| 91 | if (($field['type'] ?? null) !== FormFieldTypes::FILE && isset($form_data[$name]) && empty($form_data[$name])) { |
| 92 | $form_data[$name] = null; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | return $form_data; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * The Validator rules for a single field. |
| 101 | * |
| 102 | * @param array $field The field configuration. |
| 103 | * @return array<int, string> |
| 104 | */ |
| 105 | protected static function rules_for_field(array $field) |
| 106 | { |
| 107 | $rules = [static::required_rule($field)]; |
| 108 | |
| 109 | if (isset(static::FORMAT_RULES[$field['type'] ?? null])) { |
| 110 | $rules[] = static::FORMAT_RULES[$field['type']]; |
| 111 | } |
| 112 | |
| 113 | return $rules; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * The Validator rules for a single file field. |
| 118 | * |
| 119 | * @param array $field The field configuration. |
| 120 | * @return array<int, string> |
| 121 | */ |
| 122 | protected static function rules_for_file_field(array $field) |
| 123 | { |
| 124 | return [ |
| 125 | static::required_rule($field), |
| 126 | AcceptedFileTypeRule::class . ':' . ($field['accept'] ?? 'default'), |
| 127 | MaxFileSizeRule::class . ':' . ($field['max-file-size'] ?? 2), |
| 128 | ]; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * The `required`/`nullable` rule for a field. |
| 133 | * |
| 134 | * @param array $field The field configuration. |
| 135 | * @return string |
| 136 | */ |
| 137 | protected static function required_rule(array $field) |
| 138 | { |
| 139 | return !empty($field['required']) ? 'required' : 'nullable'; |
| 140 | } |
| 141 | } |
| 142 |