You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Services/UserCopy/UserCopyAuthoringProjection...

184 lines
6.3 KiB
PHP

<?php
namespace App\Services\UserCopy;
/**
* Pure authoring classification for the administrator copy workspace.
*
* Database reads, locale state, publication hydration and coverage belong to
* the workspace reader. This module only applies the Manifest's authoring
* contract to an already ordered list of active ledger entries.
*/
final class UserCopyAuthoringProjection
{
/** @var array<string,mixed> */
private readonly array $manifest;
/** @var array<string,array<string,mixed>> */
private readonly array $manifestByKey;
/** @var list<array{id:string,label:string}> */
private readonly array $pages;
/** @var array<string,string> */
private readonly array $groupLabels;
/** @var array<string,true> */
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<string,mixed> $query
* @param list<object> $entryRows Active ledger rows in copy-key order.
* @return array{
* pages:list<array{id:string,label:string}>,
* groups:list<array{id:string,label:string}>,
* selected_page:string,
* channel:string,
* selected_group:string,
* entry_rows:list<object>,
* metadata_by_key:array<string,array<string,mixed>>
* }
*/
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,
];
}
}