'date', ]; } protected static function booted(): void { static::creating(function (self $text): void { $text->body_sha256 = hash('sha256', $text->body); }); static::updating(fn () => throw new \LogicException('legal_texts ist unveränderlich — neue Fassung als neue Zeile anlegen.')); static::deleting(fn () => throw new \LogicException('legal_texts ist unveränderlich — Fassungen werden nie gelöscht.')); } /** * Die heute gültige Fassung eines Typs für die gegebene AGB-Fassung. * Fällt auf die jüngste gültige Fassung ohne AGB-Bindung zurück, damit * ein Text ohne Paragrafenverweis für jede AGB-Fassung taugt. */ public static function currentFor(string $type, ?string $agbVersion, string $locale = 'de'): ?self { $query = static::query() ->where('type', $type) ->where('locale', $locale) ->whereNotNull('effective_from') ->where('effective_from', '<=', today()) ->orderByDesc('effective_from') ->orderByDesc('id'); if ($agbVersion !== null) { $bound = (clone $query)->where('bound_agb_version', $agbVersion)->first(); if ($bound !== null) { return $bound; } } return (clone $query)->whereNull('bound_agb_version')->first(); } public static function findByKey(string $key): ?self { return static::query()->where('key', $key)->first(); } /** * Ob eine Kennung heute abgegeben werden darf (aktive Fassung des Typs). */ public function isCurrentFor(?string $agbVersion, string $locale = 'de'): bool { return static::currentFor($this->type, $agbVersion, $locale)?->is($this) === true; } /** * Platzhalter im Wortlaut auflösen. Der gerenderte Text wird mit der * Bestätigung gespeichert, der Rohtext bleibt hier unverändert. * * @param array $variables */ public function render(array $variables, bool $short = false): string { $template = $short ? ($this->short_body ?? $this->body) : $this->body; return strtr($template, collect($variables) ->mapWithKeys(fn (string $value, string $name): array => ['{'.$name.'}' => $value]) ->all()); } public function effectiveFromFormatted(): ?string { return $this->effective_from ? Carbon::parse($this->effective_from)->locale('de')->isoFormat('D. MMMM YYYY') : null; } }