$options */ private function __construct( public readonly string $name, public readonly string $type, public readonly string $label, private readonly array $options = [], ) {} public static function text(string $name, string $label, int $max = 200, string $default = '', ?string $placeholder = null): self { return new self($name, 'text', $label, ['max' => $max, 'default' => $default, 'placeholder' => $placeholder]); } public static function textarea(string $name, string $label, int $max = 2000, string $default = ''): self { return new self($name, 'textarea', $label, ['max' => $max, 'default' => $default]); } /** Formatierter Text – im Editor direkt im Block bearbeitet (TipTap). */ public static function html(string $name, string $label): self { return new self($name, 'html', $label, ['default' => '']); } /** * @param array|Closure(): array $choices */ public static function select(string $name, string $label, array|Closure $choices, ?string $default = null): self { return new self($name, 'select', $label, ['choices' => $choices, 'default' => $default]); } public static function toggle(string $name, string $label, bool $default = false): self { return new self($name, 'toggle', $label, ['default' => $default]); } public static function number(string $name, string $label, int $min, int $max, int $default): self { return new self($name, 'number', $label, ['min' => $min, 'max' => $max, 'default' => $default]); } public static function url(string $name, string $label, string $default = ''): self { return new self($name, 'url', $label, ['default' => $default]); } /** Hochgeladenes Bild bzw. Bild aus der Mediathek (Pfad im Upload-Speicher). */ public static function image(string $name, string $label): self { return new self($name, 'image', $label, ['default' => null]); } /** Personen aus dem Verzeichnis (IDs). */ public static function people(string $name, string $label, int $max = 24): self { return new self($name, 'people', $label, ['max' => $max, 'default' => []]); } /** Einfache Tabelle: Zeilen aus Textzellen. */ public static function table(string $name, string $label): self { return new self($name, 'table', $label, ['default' => [['', ''], ['', '']]]); } /** Symbol aus dem Symbolsatz der Schnellzugriffe. */ public static function icon(string $name, string $label, string $default = 'link'): self { return new self($name, 'icon', $label, ['default' => $default, 'choices' => Icons::choices()]); } /** Liste gleichartiger Einträge, z. B. Schaltflächen oder Galeriebilder. */ public static function repeater(string $name, string $label, array $fields, int $max = 20, string $itemLabel = 'Eintrag'): self { return new self($name, 'repeater', $label, ['fields' => $fields, 'max' => $max, 'default' => [], 'itemLabel' => $itemLabel]); } /** Beliebige Liste von IDs, deren Einträge der Block selbst verwaltet (z. B. Dateien). */ public static function ids(string $name, string $label, string $kind, int $max = 50): self { return new self($name, 'ids', $label, ['kind' => $kind, 'max' => $max, 'default' => []]); } public function default(): mixed { if ($this->type === 'repeater') { return []; } return $this->options['default'] ?? null; } public function help(string $help): self { return new self($this->name, $this->type, $this->label, [...$this->options, 'help' => $help]); } /** * @return array Beschreibung für das Editor-Formular */ public function toArray(): array { $options = $this->options; if (isset($options['choices'])) { $choices = $options['choices'] instanceof Closure ? ($options['choices'])() : $options['choices']; $options['choices'] = collect($choices)->map(fn ($label, $value) => ['value' => (string) $value, 'label' => $label])->values()->all(); } if (isset($options['fields'])) { $options['fields'] = array_map(fn (Field $field) => $field->toArray(), $options['fields']); } return ['name' => $this->name, 'type' => $this->type, 'label' => $this->label, ...$options]; } public function sanitize(mixed $value): mixed { return match ($this->type) { 'text', 'textarea' => mb_substr(trim(is_scalar($value) ? (string) $value : ''), 0, $this->options['max']), 'html' => app(RichText::class)->sanitize(is_string($value) ? mb_substr($value, 0, 200_000) : ''), 'select' => $this->sanitizeChoice($value), 'toggle' => (bool) $value, 'number' => max($this->options['min'], min($this->options['max'], is_numeric($value) ? (int) $value : $this->options['default'])), 'url' => self::safeUrl($value), 'image' => ImagePaths::isCandidate($value) ? $value : null, 'people', 'ids' => array_values(array_slice(array_unique(array_filter(array_map('intval', is_array($value) ? $value : []))), 0, $this->options['max'])), 'table' => $this->sanitizeTable($value), 'icon' => Icons::has($value) ? $value : $this->options['default'], 'repeater' => $this->sanitizeRepeater($value), default => null, }; } public static function safeUrl(mixed $value): string { $url = trim(is_string($value) ? $value : ''); if ($url === '' || mb_strlen($url) > 2000) { return ''; } // Relative Adressen im Portal, Anker oder http(s)/mailto/tel – nichts anderes (kein javascript:). if (preg_match('#^(/(?!/)|\#)#', $url) || preg_match('#^(https?://|mailto:|tel:)#i', $url)) { return $url; } return preg_match('#^[a-z0-9.-]+\.[a-z]{2,}(/|$)#i', $url) ? 'https://'.$url : ''; } private function sanitizeChoice(mixed $value): ?string { $choices = $this->options['choices'] instanceof Closure ? ($this->options['choices'])() : $this->options['choices']; $value = is_scalar($value) ? (string) $value : null; return $value !== null && array_key_exists($value, $choices) ? $value : $this->options['default']; } /** * @return list> */ private function sanitizeTable(mixed $value): array { $rows = array_slice(is_array($value) ? array_values($value) : [], 0, 100); $width = min(10, max(1, ...array_map(fn ($row) => is_array($row) ? count($row) : 1, $rows ?: [[]]))); return array_map(function ($row) use ($width) { $cells = array_slice(is_array($row) ? array_values($row) : [], 0, $width); return array_pad(array_map(fn ($cell) => mb_substr(is_scalar($cell) ? (string) $cell : '', 0, 1000), $cells), $width, ''); }, $rows); } /** * @return list> */ private function sanitizeRepeater(mixed $value): array { $items = array_slice(is_array($value) ? array_values($value) : [], 0, $this->options['max']); return array_map(function ($item) { $item = is_array($item) ? $item : []; $clean = []; foreach ($this->options['fields'] as $field) { $clean[$field->name] = $field->sanitize($item[$field->name] ?? $field->default()); } return $clean; }, $items); } }