legacyAdapter->buildSnapshotFromInput($input); if ($input->persistedReportId === null) { throw new RuntimeException('persistedReportId is required for report comparison.'); } $actual = $this->reportRepository->findSnapshot($input->persistedReportId); if ($actual === null) { throw new RuntimeException('Saved report snapshot was not found for comparison.'); } $diff = $this->buildDiff($expected->toComparableArray(), $actual->toComparableArray()); $status = empty($diff) ? 'matched' : 'diff'; return new ReportComparisonResult( reportType: $input->reportType, path: 'new', status: $status, diff: $diff, departmentId: $input->departmentId, userId: $input->userId, periodStart: $input->periodStart->format('Y-m-d H:i:s'), periodEnd: $input->periodEnd->format('Y-m-d H:i:s'), reportId: $input->persistedReportId, durationMs: round((microtime(true) - $startedAt) * 1000, 2), ); } /** * @param array $expected * @param array $actual * @return array */ private function buildDiff(array $expected, array $actual): array { $diff = []; foreach ($expected as $key => $value) { $actualValue = $actual[$key] ?? null; if ($actualValue !== $value) { $diff[$key] = [ 'expected' => $value, 'actual' => $actualValue, ]; } } return $diff; } }