['label' => 'Title on the photo', 'type' => 'text', 'help' => 'One line.', 'max' => 120] * * Types: `text` (one line), `textarea` (paragraphs), `markdown` (a long text in Markdown), `lines` (one item * per line), `media` (a picture from the media library; the value is its address), `url`, `number`, `date`, * `toggle` (a switch) and `select` (needs `options`: a list of values, or value => label). `required`, `default` and * `rules` (extra Laravel rules) are optional on every type. */ final class Fields { public const TYPES = ['text', 'textarea', 'markdown', 'lines', 'media', 'url', 'number', 'date', 'toggle', 'select']; private const LENGTHS = ['text' => 255, 'textarea' => 20000, 'markdown' => 100000, 'lines' => 10000, 'media' => 2048, 'url' => 2048, 'date' => 40, 'select' => 255]; /** * The definitions in one shape, with a key, a label and a known type; what is wrong is dropped. * * @param array $fields key => definition * @return array, max: ?int, rules: list}> */ public static function normalize(array $fields): array { $out = []; foreach ($fields as $key => $field) { if (! is_string($key) || ! preg_match('/^[a-z][a-z0-9_]{0,59}$/', $key) || ! is_array($field)) { continue; } $type = in_array($field['type'] ?? 'text', self::TYPES, true) ? ($field['type'] ?? 'text') : 'text'; $options = []; $given = (array) ($field['options'] ?? []); foreach ($given as $value => $label) { $options[(string) (array_is_list($given) ? $label : $value)] = (string) $label; } if ($type === 'select' && $options === []) { continue; } $out[$key] = [ 'key' => $key, 'label' => (string) ($field['label'] ?? ucfirst(str_replace('_', ' ', $key))), 'type' => $type, 'help' => isset($field['help']) ? (string) $field['help'] : null, 'required' => (bool) ($field['required'] ?? false), 'default' => $field['default'] ?? self::empty($type), 'options' => $options, 'max' => isset($field['max']) ? max(1, (int) $field['max']) : null, 'rules' => array_values((array) ($field['rules'] ?? [])), ]; } return $out; } /** * The Laravel rules for the values, keyed ``. * * @param array> $fields normalized * @return array> */ public static function rules(array $fields, string $prefix = ''): array { $rules = []; foreach ($fields as $key => $field) { $type = $field['type']; $max = $field['max'] ?? self::LENGTHS[$type] ?? null; $presence = $field['required'] ? 'required' : 'nullable'; $rules[$prefix.$key] = [$presence, ...match ($type) { 'toggle' => ['boolean'], 'number' => ['numeric'], 'date' => ['date'], 'select' => ['string', Rule::in(array_keys($field['options']))], 'media', 'url' => ['string', 'max:'.$max, 'regex:'.self::ADDRESS], default => ['string', 'max:'.$max], }, ...$field['rules']]; } return $rules; } /** A web address or a path of the site. Never `javascript:` and its kin. */ public const ADDRESS = '#^(/(?!/)|https?://)[^\s<>"\']*$#i'; /** * What is stored: only the known keys, in the type of the field, the default where nothing is given. * * @param array> $fields normalized * @param array $input * @return array */ public static function clean(array $fields, array $input): array { $out = []; foreach ($fields as $key => $field) { $value = array_key_exists($key, $input) ? $input[$key] : $field['default']; $out[$key] = match ($field['type']) { 'toggle' => filter_var($value, FILTER_VALIDATE_BOOLEAN), 'number' => $value === null || $value === '' ? null : $value + 0, default => $value === null ? '' : (is_scalar($value) ? trim((string) $value) : ''), }; } return $out; } /** * The stored values over the defaults, for the screens and for the public site. * * @param array> $fields normalized * @param array $stored * @return array */ public static function values(array $fields, array $stored): array { $out = []; foreach ($fields as $key => $field) { $out[$key] = array_key_exists($key, $stored) ? $stored[$key] : $field['default']; } return $out; } private static function empty(string $type): mixed { return match ($type) { 'toggle' => false, 'number' => null, default => '', }; } }