Files
laraundfelix/app/Domain/Cms/Sections/SectionStyle.php
T
LarsandClaude Sonnet 5.5 62f3a33541
CI Feature / test (push) Successful in 8m2s
Deploy Test / test (push) Successful in 8m1s
Deploy Test / deploy (push) Successful in 51s
Richte Karten im Abschnitt an der größten Karte aus und setze Kartenlinks an den unteren Rand
Zwei Gestaltungswerte am Abschnitt (Höhe der Karten, Link in der Karte), gespeichert nur bei Abweichung
von der Vorgabe. Karten nebeneinander füllen die Zeilenhöhe (CSS); ein kleines Skript gleicht Zeilen
untereinander an. Links im Fließtext bleiben unberührt (ADR-66).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-29 17:33:27 +01:00

285 lines
11 KiB
PHP

<?php
namespace App\Domain\Cms\Sections;
use App\Domain\Cms\Widgets\Support\WebsiteImage;
/**
* Semantische Abschnittsgestaltung. Jeder Wert ist eine Aufzählung, die auf zentrale Design-Tokens
* abgebildet wird (resources/css/app.css, Abschnitt „Abschnitte“). Keine freie CSS- oder Farbangabe.
*/
final class SectionStyle
{
/** @var array<string, string> */
public const BACKGROUNDS = [
'default' => 'Standard (ohne eigene Fläche)',
'light' => 'Helle Markenfläche',
'dark' => 'Dunkle Markenfläche',
'accent' => 'Markenakzent',
'image' => 'Hintergrundbild',
];
/** @var array<string, string> */
public const WIDTHS = [
'narrow' => 'Lesebereich',
'standard' => 'Standard',
'wide' => 'Breit',
'full' => 'Vollbreite',
];
/**
* Gemeinsame Stufen der drei Abstände eines Abschnitts (Hülle Version 2):
* - spacing_top: Abstand vor dem Inhalt (oben),
* - spacing_bottom: Abstand nach dem Inhalt (unten),
* - gap: Abstand zwischen den Plätzen im Abschnittsraster, nebeneinander und gestapelt.
* Zwischen zwei Abschnitten addieren sich „unten“ des ersten und „oben“ des zweiten; es gibt keine weitere
* versteckte Vorgabe. Liegt eine eigene Fläche (Hintergrund) vor, liegen oben und unten innerhalb dieser Fläche.
* Werte: resources/css/tokens.css (--section-space-*, --section-gap-*).
*
* @var array<string, string>
*/
public const SPACINGS = [
'none' => 'Kein Abstand',
'compact' => 'Kompakt',
'normal' => 'Normal',
'large' => 'Großzügig',
];
/** @var array<string, string> */
public const DIVIDERS = [
'none' => 'Kein Trenner',
'line' => 'Dezente Linie',
'accent' => 'Markenakzent',
];
/** @var array<string, string> */
public const OVERLAYS = [
'light' => 'Helle Überlagerung (dunkle Schrift)',
'dark' => 'Dunkle Überlagerung (helle Schrift)',
'none' => 'Keine Überlagerung',
];
/** @var array<string, string> */
public const FOCUS = [
'center' => 'Mitte',
'top' => 'Oben',
'bottom' => 'Unten',
'left' => 'Links',
'right' => 'Rechts',
'top_left' => 'Oben links',
'top_right' => 'Oben rechts',
'bottom_left' => 'Unten links',
'bottom_right' => 'Unten rechts',
];
/** Fokuspunkt als Objektposition des Bildes; bestimmt den Zuschnitt auf schmalen Bildschirmen. */
public const FOCUS_POSITIONS = [
'center' => '50% 50%',
'top' => '50% 0%',
'bottom' => '50% 100%',
'left' => '0% 50%',
'right' => '100% 50%',
'top_left' => '0% 0%',
'top_right' => '100% 0%',
'bottom_left' => '0% 100%',
'bottom_right' => '100% 100%',
];
/**
* Karten im Abschnitt (ADR-66): Höhe aller Karten des Abschnitts. „Gleich hoch“ richtet alle Karten an der größten aus.
*
* @var array<string, string>
*/
public const CARD_HEIGHTS = [
'auto' => 'Nach Inhalt der Karte',
'equal' => 'Alle so hoch wie die größte Karte',
];
/**
* Karten im Abschnitt (ADR-66): Lage des Links in Karten mit eigenem Link (Schaltfläche, „Mehr erfahren“). Links im
* Fließtext einer Karte bleiben, wo sie stehen.
*
* @var array<string, string>
*/
public const CARD_LINKS = [
'text' => 'Direkt unter dem Text',
'bottom' => 'Am unteren Rand der Karte',
];
public const DEFAULTS = [
'background' => 'default',
'image' => null,
'focus' => 'center',
'overlay' => 'light',
'width' => 'standard',
'spacing_top' => 'compact',
'spacing_bottom' => 'compact',
'gap' => 'normal',
'divider' => 'none',
];
/**
* Ergänzende Gestaltungsoptionen: Sie stehen nur im gespeicherten Stand, wenn sie von der Vorgabe abweichen, damit
* Abschnitte ohne diese Optionen unverändert bleiben (Format und Vergleich mit dem letzten Stand).
*/
public const OPTIONAL = [
'cards_height' => 'auto',
'cards_links' => 'text',
];
/**
* Aufzählungsfelder und ihre zulässigen Werte.
*
* @return array<string, array<string, string>>
*/
public static function enums(): array
{
return [
'background' => self::BACKGROUNDS,
'focus' => self::FOCUS,
'overlay' => self::OVERLAYS,
'width' => self::WIDTHS,
'spacing_top' => self::SPACINGS,
'spacing_bottom' => self::SPACINGS,
'gap' => self::SPACINGS,
'divider' => self::DIVIDERS,
'cards_height' => self::CARD_HEIGHTS,
'cards_links' => self::CARD_LINKS,
];
}
/**
* Lesepfad: gespeicherte Werte; Unbekanntes fällt auf die Vorgabe zurück, damit alte oder fremde Daten
* nie eine Seite brechen.
*
* @return array{background: string, image: ?string, focus: string, overlay: string, width: string, spacing_top: string, spacing_bottom: string, gap: string, divider: string, cards_height: string, cards_links: string}
*/
public static function fromStored(mixed $style): array
{
$style = is_array($style) ? $style : [];
$result = self::DEFAULTS + self::OPTIONAL;
foreach (self::enums() as $key => $options) {
$value = $style[$key] ?? null;
if (is_string($value) && array_key_exists($value, $options)) {
$result[$key] = $value;
}
}
$result['image'] = $result['background'] === 'image' ? WebsiteImage::normalize($style['image'] ?? null) : null;
return $result;
}
/**
* Kurzbeschreibung für die Abschnittsliste im Editor.
*
* @param array<string, mixed> $style
*/
public static function summary(array $style): string
{
$style = self::fromStored($style);
$parts = [self::BACKGROUNDS[$style['background']]];
if ($style['width'] !== 'standard') {
$parts[] = self::WIDTHS[$style['width']];
}
if ($style['spacing_top'] !== self::DEFAULTS['spacing_top'] || $style['spacing_bottom'] !== self::DEFAULTS['spacing_bottom']) {
$parts[] = 'Abstand oben '.mb_strtolower(self::SPACINGS[$style['spacing_top']])
.', unten '.mb_strtolower(self::SPACINGS[$style['spacing_bottom']]);
}
if ($style['gap'] !== 'normal') {
$parts[] = 'Widget-Abstand '.mb_strtolower(self::SPACINGS[$style['gap']]);
}
if ($style['divider'] !== 'none') {
$parts[] = 'Trenner: '.self::DIVIDERS[$style['divider']];
}
if ($style['cards_height'] === 'equal') {
$parts[] = 'Karten gleich hoch';
}
if ($style['cards_links'] === 'bottom') {
$parts[] = 'Kartenlinks unten';
}
return implode(' · ', $parts);
}
/**
* Ob eine gespeicherte Gestaltung noch die Abstände der Hülle Version 1 trägt
* („Abstand oben und unten“ spacing, Abweichung „Abstand oben“ same/small/none).
*
* @param array<string, mixed> $style
*/
public static function hasLegacySpacing(array $style): bool
{
return array_key_exists('spacing', $style)
|| in_array($style['spacing_top'] ?? null, ['same', 'small'], true);
}
/**
* Übersetzt die Abstände der Hülle Version 1 in die drei Werte der Version 2, ohne die Darstellung zu ändern.
*
* Version 1 rechnete so: jeder Abschnitt oben und unten „spacing“ (Vorgabe normal); die Abweichung
* „Abstand oben“ ersetzte nur oben (small/none); und eine versteckte Regel setzte oben auf null, wenn ein Abschnitt
* ohne eigene Fläche direkt auf einen Abschnitt ohne eigene Fläche folgte (ohne Linien- oder Akzenttrenner dazwischen).
* Diese Regel wird hier als „oben: kein Abstand“ ausgeschrieben. „Gering“ hat keine eigene Stufe mehr und wird
* „Kompakt“ (dokumentierte Abweichung). Der Widget-Abstand entspricht dem bisherigen festen Rasterabstand („normal“).
*
* @param list<array<string, mixed>> $sections Abschnitte in Lesereihenfolge (mit style und slots)
* @param bool $byVersion true: jede Hülle mit v < 2 gilt als Version 1 (Lesepfad gespeicherter Revisionen);
* false: nur Gestaltungen mit alten Schlüsseln (Eingaben ohne Versionsangabe)
* @return list<array<string, mixed>> dieselben Abschnitte mit übersetzter Gestaltung
*/
public static function migrateSpacing(array $sections, bool $byVersion = false): array
{
$previous = null;
foreach ($sections as $index => $section) {
$style = is_array($section['style'] ?? null) ? $section['style'] : [];
$rendered = is_array($section['slots'] ?? null) && array_filter($section['slots'], 'is_array') !== [];
$background = is_string($style['background'] ?? null) && isset(self::BACKGROUNDS[$style['background']]) ? $style['background'] : 'default';
if ($background === 'image' && WebsiteImage::normalize($style['image'] ?? null) === null) {
$background = 'default';
}
$divider = in_array($style['divider'] ?? null, ['line', 'accent'], true) ? $style['divider'] : 'none';
$legacy = $byVersion ? (int) ($section['v'] ?? 1) < 2 : self::hasLegacySpacing($style);
if ($legacy) {
$level = in_array($style['spacing'] ?? null, ['compact', 'normal', 'large'], true) ? $style['spacing'] : 'normal';
$override = $style['spacing_top'] ?? 'same';
$collapsed = $background === 'default' && $previous !== null && $previous['background'] === 'default' && ! in_array($previous['divider'], ['line', 'accent'], true);
$style['spacing_top'] = match (true) {
$override === 'none' => 'none',
$override === 'small' => 'compact',
$collapsed => 'none',
default => $level,
};
$style['spacing_bottom'] = $level;
$style['gap'] = 'normal';
// Trenner „Zusätzlicher Abstand“ (Version 1) fügte unten noch einmal den Abschnittsabstand an.
if (($style['divider'] ?? null) === 'space') {
$style['spacing_bottom'] = ['compact' => 'normal', 'normal' => 'large', 'large' => 'large'][$level];
$style['divider'] = 'none';
}
unset($style['spacing']);
$sections[$index]['style'] = $style;
}
if ($rendered) {
$previous = ['background' => $background, 'divider' => $divider];
}
}
return $sections;
}
}