*/ private readonly array $manifest; /** @var array> */ private readonly array $manifestByKey; /** @var list */ private readonly array $pages; /** @var array */ private readonly array $groupLabels; /** @var array */ private readonly array $groupIds; public function __construct(array $manifest) { $this->manifest = UserCopyManifest::validate($manifest); $manifestByKey = []; $groupIds = []; foreach ($this->manifest['entries'] as $entry) { $manifestByKey[$entry['key']] = $entry; $groupIds[(string) $entry['editor_group']] = true; } $this->manifestByKey = $manifestByKey; ksort($groupIds, SORT_STRING); $this->groupIds = $groupIds; $pages = []; $pageLabels = []; foreach ($this->manifest['pages'] as $pageKey => $definition) { $pageLabels[$pageKey] = (string) $definition['label']; $pages[] = [ 'id' => $pageKey, 'label' => (string) $definition['label'], ]; } $this->pages = $pages; $groupLabels = []; foreach (array_keys($groupIds) as $groupId) { $groupLabels[$groupId] = $pageLabels[$groupId] ?? ($groupId === 'common.shared' ? '公共框架' : $groupId); } $this->groupLabels = $groupLabels; } /** * @param array $query * @param list $entryRows Active ledger rows in copy-key order. * @return array{ * pages:list, * groups:list, * selected_page:string, * channel:string, * selected_group:string, * entry_rows:list, * metadata_by_key:array> * } */ public function project(array $query, array $entryRows): array { $selectedPage = isset($query['page']) && is_string($query['page']) ? trim($query['page']) : ''; if ($selectedPage === '') { $selectedPage = (string) ($this->pages[0]['id'] ?? ''); } if (!isset($this->manifest['pages'][$selectedPage])) { throw new UserCopyCatalogException('Unknown user copy page.'); } $channel = 'legacy'; if (array_key_exists('channel', $query)) { if (!is_string($query['channel'])) { throw new UserCopyAdminException('文案编辑通道无效。'); } $candidate = trim($query['channel']); if ($candidate !== '') { $channel = $candidate; } } if (!in_array( $channel, ['legacy', 'page', 'shared', 'advanced', 'all'], true )) { throw new UserCopyAdminException('文案编辑通道无效。'); } $selectedGroup = ''; if (array_key_exists('group', $query)) { if (!is_string($query['group'])) { throw new UserCopyAdminException('文案编辑分组无效。'); } $selectedGroup = trim($query['group']); if ($selectedGroup !== '' && !isset($this->groupIds[$selectedGroup])) { throw new UserCopyAdminException('文案编辑分组无效。'); } } $selectedScopes = array_fill_keys( $this->manifest['pages'][$selectedPage]['scopes'], true ); $channelRows = []; $channelGroupIds = []; foreach ($entryRows as $entry) { $copyKey = (string) $entry->copy_key; $manifestEntry = $this->manifestByKey[$copyKey] ?? null; $inSelectedPage = isset($selectedScopes[(string) $entry->scope]); $matchesChannel = match ($channel) { 'legacy' => $inSelectedPage, 'page' => is_array($manifestEntry) && $manifestEntry['editor_surface'] === 'page' && $manifestEntry['editor_group'] === $selectedPage, 'shared' => is_array($manifestEntry) && $manifestEntry['editor_surface'] === 'shared', 'advanced' => is_array($manifestEntry) && in_array( $manifestEntry['editor_surface'], ['dialog', 'feedback', 'task', 'accessibility'], true ), 'all' => is_array($manifestEntry), }; if (!$matchesChannel) { continue; } $channelRows[] = $entry; if (is_array($manifestEntry)) { $channelGroupIds[(string) $manifestEntry['editor_group']] = true; } } ksort($channelGroupIds, SORT_STRING); $groups = []; foreach (array_keys($channelGroupIds) as $groupId) { $groups[] = [ 'id' => $groupId, 'label' => $this->groupLabels[$groupId] ?? $groupId, ]; } $projectedRows = []; foreach ($channelRows as $entry) { $manifestEntry = $this->manifestByKey[(string) $entry->copy_key] ?? null; if ($selectedGroup !== '' && (!is_array($manifestEntry) || $manifestEntry['editor_group'] !== $selectedGroup)) { continue; } $projectedRows[] = $entry; } return [ 'pages' => $this->pages, 'groups' => $groups, 'selected_page' => $selectedPage, 'channel' => $channel, 'selected_group' => $selectedGroup, 'entry_rows' => $projectedRows, 'metadata_by_key' => $this->manifestByKey, ]; } }