$input * @return array */ public function build(array $input): array { $scan = is_array($input['scan'] ?? null) ? $input['scan'] : []; $coverage = (new AIFQ_SR_Coverage_Projector())->normalize_results($scan['coverage_results'] ?? []); $next = is_array($scan['next'] ?? null) ? $scan['next'] : []; $evidence = is_array($next['evidence_inventory'] ?? null) ? array_values(array_filter($next['evidence_inventory'], 'is_array')) : []; $human = is_array($input['external_human'] ?? null) ? $input['external_human'] : []; $changes = is_array($input['meaningful_changes'] ?? null) ? $input['meaningful_changes'] : []; $live_context = is_array($input['live_context'] ?? null) ? $input['live_context'] : []; $groups = [ 'checks' => $this->checks($coverage), 'evidence' => $this->evidence($evidence), 'situations' => $this->situations($human), 'context' => $this->context($scan, $live_context), 'changes' => $this->changes($changes), ]; $all = []; foreach (['checks', 'evidence', 'situations', 'context', 'changes'] as $group) { foreach ($groups[$group] as $row) { $all[] = $row; } } return [ 'review' => [ 'created_at' => max(0, (int) ($scan['created_at'] ?? 0)), 'coverage_schema_version' => (int) ($scan['coverage_schema_version'] ?? 0), 'next_schema_version' => (int) ($next['schema_version'] ?? 0), ], 'groups' => $groups, 'all' => $all, 'counts' => array_map('count', $groups), ]; } /** @return array> */ private function checks(array $coverage): array { $rows = []; foreach ($coverage as $entry) { $applicability = (string) ($entry['applicability'] ?? ''); $knowledge = (string) ($entry['knowledge_state'] ?? ''); $outcome = (string) ($entry['outcome'] ?? ''); $state = $outcome; if ($applicability === AIFQ_SR_Coverage_Projector::NOT_APPLICABLE) { $state = 'not_applicable'; } elseif ($knowledge === AIFQ_SR_Coverage_Projector::KNOWLEDGE_NOT_RUN) { $state = 'not_run'; } elseif (in_array($knowledge, [AIFQ_SR_Coverage_Projector::KNOWLEDGE_UNOBSERVABLE, AIFQ_SR_Coverage_Projector::KNOWLEDGE_ERROR], true)) { $state = 'not_verified'; } elseif ($state === '') { $state = 'unknown'; } $rows[] = $this->row( 'check', (string) ($entry['coverage_id'] ?? ''), (string) ($entry['label'] ?? ''), $this->check_value($entry), $state, (string) ($entry['area_id'] ?? ''), $knowledge, (string) ($entry['source_ref'] ?? ''), (string) ($entry['source_kind'] ?? ''), max(0, (int) ($entry['observed_at'] ?? 0)), 'review_snapshot', $entry ); } return $rows; } private function check_value(array $entry): string { if (($entry['applicability'] ?? '') === AIFQ_SR_Coverage_Projector::NOT_APPLICABLE) { return __('Not applicable', 'aifq-site-review'); } if (($entry['knowledge_state'] ?? '') === AIFQ_SR_Coverage_Projector::KNOWLEDGE_NOT_RUN) { return __('Not run', 'aifq-site-review'); } if (($entry['knowledge_state'] ?? '') !== AIFQ_SR_Coverage_Projector::KNOWLEDGE_EVALUATED) { return __('Not verified', 'aifq-site-review'); } return ucfirst(str_replace('_', ' ', (string) ($entry['outcome'] ?? ''))); } /** @return array> */ private function evidence(array $inventory): array { $rows = []; foreach ($inventory as $item) { $rows[] = $this->row( 'evidence', (string) ($item['id'] ?? ''), (string) ($item['key'] ?? __('Evidence', 'aifq-site-review')), (string) ($item['state'] ?? ''), (string) ($item['state'] ?? 'unknown'), 'evidence', (string) ($item['state'] ?? ''), (string) ($item['provider_id'] ?? ''), (string) ($item['perspective'] ?? ''), max(0, (int) ($item['observed_at'] ?? 0)), 'review_snapshot', $item ); } return $rows; } /** @return array> */ private function situations(array $human): array { $rows = []; $items = is_array($human['summary']['situations'] ?? null) ? $human['summary']['situations'] : []; foreach ($items as $item) { if (!is_array($item)) { continue; } $identity = is_array($item['identity'] ?? null) ? $item['identity'] : []; $current = is_array($item['current'] ?? null) ? $item['current'] : []; $presentation = is_array($item['presentation'] ?? null) ? $item['presentation'] : []; $id = (string) ($identity['situation_id'] ?? ''); $title = (string) ($presentation['status_label'] ?? ''); if ($title === '') { $title = ucwords(str_replace('_', ' ', $id)); } $rows[] = $this->row( 'situation', $id, $title, (string) ($current['health_claim'] ?? $current['state'] ?? ''), (string) ($current['placement'] ?? $item['attention']['placement'] ?? $current['knowledge_state'] ?? 'context'), 'situations', (string) ($current['knowledge_state'] ?? ''), 'canonical_situation', 'derived_from_canonical_evidence', 0, 'current_projection', $item ); } return $rows; } /** @return array> */ private function context(array $scan, array $live_context): array { $rows = []; foreach ((array) ($scan['context_items'] ?? []) as $item) { if (!is_array($item)) { continue; } $rows[] = $this->row( 'context', (string) ($item['key'] ?? ''), (string) ($item['title'] ?? __('Context', 'aifq-site-review')), (string) ($item['description'] ?? ''), 'context', (string) ($item['category'] ?? 'context'), 'evaluated', 'review_context', 'local_runtime', max(0, (int) ($scan['created_at'] ?? 0)), 'review_snapshot', $item ); } foreach ($live_context as $index => $item) { if (!is_array($item)) { continue; } $label = (string) ($item['label'] ?? ''); $rows[] = $this->row( 'context', 'live.' . sanitize_key($label !== '' ? $label : (string) $index), $label, (string) ($item['value'] ?? ''), 'context', 'live_runtime', 'live', 'wordpress_runtime', 'local_runtime', time(), 'live', $item ); } return $rows; } /** @return array> */ private function changes(array $changes): array { if (empty($changes['is_comparable'])) { return []; } $rows = []; foreach ((array) ($changes['new'] ?? []) as $index => $item) { if (!is_array($item)) continue; $finding = is_array($item['finding'] ?? null) ? $item['finding'] : []; $rows[] = $this->change_row('new', $item, $finding, $index); } foreach ((array) ($changes['resolved'] ?? []) as $index => $item) { if (!is_array($item)) continue; $finding = is_array($item['finding'] ?? null) ? $item['finding'] : []; $rows[] = $this->change_row('no_longer_flagged', $item, $finding, $index); } foreach ((array) ($changes['now_assessable'] ?? []) as $index => $item) { if (!is_array($item)) continue; $id = (string) ($item['capability_id'] ?? ''); $rows[] = $this->row( 'change', 'now_assessable.' . sanitize_key($id) . '.' . $index, ucwords(str_replace('_', ' ', $id)), __('Can now be checked', 'aifq-site-review'), 'changed', 'changes', (string) ($item['current_evaluability'] ?? ''), 'meaningful_changes', 'review_history', max(0, (int) ($changes['current_created_at'] ?? 0)), 'review_comparison', $item ); } return $rows; } private function change_row(string $value, array $item, array $finding, int $index): array { $key = (string) ($item['finding_key'] ?? $finding['key'] ?? ''); $title = (string) ($finding['title'] ?? $key); return $this->row( 'change', $value . '.' . sanitize_key($key) . '.' . $index, $title, $value === 'new' ? __('New', 'aifq-site-review') : __('No longer flagged', 'aifq-site-review'), 'changed', (string) ($finding['category_id'] ?? 'changes'), 'evaluated', 'meaningful_changes', 'review_history', 0, 'review_comparison', $item ); } private function row( string $type, string $id, string $title, string $value, string $state, string $area, string $knowledge, string $source, string $perspective, int $observed_at, string $scope, array $raw ): array { return compact('type', 'id', 'title', 'value', 'state', 'area', 'knowledge', 'source', 'perspective', 'observed_at', 'scope', 'raw'); } }