forked from laysense/laysite
120 lines
5.8 KiB
PHP
120 lines
5.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Content;
|
|
|
|
use RuntimeException;
|
|
|
|
final class PageDraft
|
|
{
|
|
public const RESERVED = ['admin', 'assets', 'home', 'account', 'mcp', 'palette'];
|
|
|
|
public static function slug(mixed $value, array $reserved = self::RESERVED, bool $includeLayouts = false): string
|
|
{
|
|
if ($includeLayouts && is_string($value) && preg_match('/^layout--(?:header|footer)-[1-9][0-9]{0,18}$/D', $value)) {
|
|
return $value;
|
|
}
|
|
if (!is_string($value) || strlen($value) > 70 || !preg_match('/^[a-z0-9]+(?:-[a-z0-9]+)*$/D', $value)
|
|
|| in_array($value, $reserved, true)) {
|
|
throw new RuntimeException('网址标识无效或已保留。');
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
|
|
public static function content(array $input, array $page): array
|
|
{
|
|
$name = self::text($input['name'] ?? $page['name'] ?? '', 100, true);
|
|
$page += ['name' => $name, 'title' => $name, 'heading' => $name, 'description' => '', 'lead' => '',
|
|
'accent' => '', 'image' => '', 'alt' => '', 'image_scale' => 0, 'alignments' => [], 'visibility' => [],
|
|
'actions' => [], 'quicklinks' => [], 'theme' => '', 'sections' => []];
|
|
$page['name'] = $name;
|
|
foreach (['title' => 160, 'heading' => 110, 'description' => 300, 'lead' => 600, 'accent' => 110, 'image' => 1000, 'alt' => 200] as $field => $limit) {
|
|
$page[$field] = self::text($input[$field] ?? $page[$field], $limit, in_array($field, ['title', 'heading'], true));
|
|
}
|
|
if (mb_strlen($page['heading'] . $page['accent']) > 110 || ($page['image'] !== '' && !Html::url($page['image'], true))) {
|
|
throw new RuntimeException('首屏标题过长或图片地址不正确。');
|
|
}
|
|
foreach (['header', 'footer', 'palette'] as $kind) {
|
|
$key = $kind . '_id';
|
|
$page[$key] = array_key_exists($key, $input) ? DesignLibrary::selection($kind, $input[$key]) : ($page[$key] ?? null);
|
|
}
|
|
$actions = Buttons::validate($input['actions'] ?? $page['actions']);
|
|
if (array_key_exists('actions', $input) && $actions !== Buttons::validate($page['actions'])) {
|
|
$page['presentation'] = CanvasStyle::remapSlots($page['presentation'] ?? [], 'action-', []);
|
|
}
|
|
$page['actions'] = $actions;
|
|
if (array_key_exists('visibility', $input)) {
|
|
if (!is_array($input['visibility'])) {
|
|
throw new RuntimeException('页面显示选项无效。');
|
|
}
|
|
foreach ($input['visibility'] as $area => $visible) {
|
|
if (!in_array($area, ['hero', 'header', 'footer'], true) || !is_bool($visible)) {
|
|
throw new RuntimeException('页面显示选项无效。');
|
|
}
|
|
$page['visibility'][$area] = $visible;
|
|
}
|
|
}
|
|
$page['status'] = 'draft';
|
|
if (array_key_exists('sections', $input)) {
|
|
$oldKeys = array_column($page['sections'], 'key');
|
|
$page['sections'] = self::sections($input['sections']);
|
|
if (!array_key_exists('actions', $input)) {
|
|
$removed = array_map(static fn (string $key): string => '#' . $key, array_diff($oldKeys, array_column($page['sections'], 'key')));
|
|
$kept = array_filter($page['actions'], static fn (array $button): bool => !in_array($button[0], $removed, true));
|
|
$page['presentation'] = CanvasStyle::remapSlots($page['presentation'] ?? [], 'action-', array_keys($kept));
|
|
$page['actions'] = array_values($kept);
|
|
}
|
|
$page['presentation'] = CanvasStyle::remapSlots($page['presentation'] ?? [], 'nav-', []);
|
|
$page['quicklinks'] = array_map(static fn (array $section): array => ['#' . $section['key'], $section['title']], array_slice($page['sections'], 0, 20));
|
|
}
|
|
|
|
return $page;
|
|
}
|
|
|
|
private static function sections(mixed $items): array
|
|
{
|
|
if (!is_array($items) || !array_is_list($items) || count($items) > 50) {
|
|
throw new RuntimeException('每页最多可提交 50 个区块。');
|
|
}
|
|
$sections = [];
|
|
foreach ($items as $item) {
|
|
if (!is_array($item)) {
|
|
throw new RuntimeException('区块必须是对象。');
|
|
}
|
|
$key = self::slug($item['key'] ?? null, ['home', 'contact', 'main-content', 'primary-navigation', 'preview']);
|
|
if (isset($sections[$key])) {
|
|
throw new RuntimeException('区块锚点重复。');
|
|
}
|
|
$preset = null;
|
|
if (isset($item['preset'])) {
|
|
$preset = is_string($item['preset']) ? BlockTemplates::find($item['preset']) : null;
|
|
if (!$preset) {
|
|
throw new RuntimeException('区块模板不存在。');
|
|
}
|
|
}
|
|
$style = $item['style'] ?? $preset[4] ?? 'plain';
|
|
if (!in_array($style, ['plain', 'tinted', 'layout'], true)) {
|
|
throw new RuntimeException('区块样式无效。');
|
|
}
|
|
$sections[$key] = [
|
|
'key' => $key, 'title' => self::text($item['title'] ?? $preset[0] ?? '', 100, true),
|
|
'lead' => self::text($item['lead'] ?? $preset[2] ?? '', 500),
|
|
'body' => Html::sanitize(self::text($item['body'] ?? $preset[1] ?? '', 120000)),
|
|
'style' => $style, 'feed' => $preset[3] ?? null, 'version' => null,
|
|
];
|
|
}
|
|
return array_values($sections);
|
|
}
|
|
|
|
private static function text(mixed $value, int $limit, bool $required = false): string
|
|
{
|
|
if (!is_string($value) || !mb_check_encoding($value, 'UTF-8') || mb_strlen($value) > $limit || ($required && trim($value) === '')) {
|
|
throw new RuntimeException('请检查页面内容及字段长度。');
|
|
}
|
|
|
|
return trim($value);
|
|
}
|
|
}
|