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