|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Ardenexal\FHIRTools\Component\Validation; |
| 6 | + |
| 7 | +use Ardenexal\FHIRTools\Component\Metadata\Attribute\Validation\FHIRPathInvariant; |
| 8 | +use Ardenexal\FHIRTools\Component\Metadata\Attribute\Validation\FHIRValueSetBinding; |
| 9 | +use Ardenexal\FHIRTools\Component\Models\R4\DataType\IssueSeverityType as R4IssueSeverityType; |
| 10 | +use Ardenexal\FHIRTools\Component\Models\R4\DataType\IssueTypeType as R4IssueTypeType; |
| 11 | +use Ardenexal\FHIRTools\Component\Models\R4\Resource\OperationOutcome\OperationOutcomeIssue as R4Issue; |
| 12 | +use Ardenexal\FHIRTools\Component\Models\R4\Resource\OperationOutcomeResource as R4Outcome; |
| 13 | +use Ardenexal\FHIRTools\Component\Models\R4B\DataType\IssueSeverityType as R4BIssueSeverityType; |
| 14 | +use Ardenexal\FHIRTools\Component\Models\R4B\DataType\IssueTypeType as R4BIssueTypeType; |
| 15 | +use Ardenexal\FHIRTools\Component\Models\R4B\Resource\OperationOutcome\OperationOutcomeIssue as R4BIssue; |
| 16 | +use Ardenexal\FHIRTools\Component\Models\R4B\Resource\OperationOutcomeResource as R4BOutcome; |
| 17 | +use Ardenexal\FHIRTools\Component\Models\R5\DataType\IssueSeverityType as R5IssueSeverityType; |
| 18 | +use Ardenexal\FHIRTools\Component\Models\R5\DataType\IssueTypeType as R5IssueTypeType; |
| 19 | +use Ardenexal\FHIRTools\Component\Models\R5\Resource\OperationOutcome\OperationOutcomeIssue as R5Issue; |
| 20 | +use Ardenexal\FHIRTools\Component\Models\R5\Resource\OperationOutcomeResource as R5Outcome; |
| 21 | + |
| 22 | +/** |
| 23 | + * Converts a FHIRValidationReport into a version-appropriate OperationOutcomeResource. |
| 24 | + * |
| 25 | + * The generated IssueType enum only contains top-level group codes. Sub-codes such as |
| 26 | + * 'invariant' and 'value' are not enum cases but are valid codes in the FHIR issue-type |
| 27 | + * value set; they are passed as raw strings via CodePrimitive which accepts any string. |
| 28 | + */ |
| 29 | +final class FHIRValidationReportMapper |
| 30 | +{ |
| 31 | + /** |
| 32 | + * @param string $fhirVersion 'R4' | 'R4B' | 'R5' |
| 33 | + * |
| 34 | + * @return object OperationOutcomeResource for the requested FHIR version |
| 35 | + */ |
| 36 | + public function toOperationOutcome( |
| 37 | + FHIRValidationReport $report, |
| 38 | + string $fhirVersion = 'R4', |
| 39 | + ): object { |
| 40 | + return match ($fhirVersion) { |
| 41 | + 'R4' => $this->buildR4($report->violations), |
| 42 | + 'R4B' => $this->buildR4B($report->violations), |
| 43 | + 'R5' => $this->buildR5($report->violations), |
| 44 | + default => throw new \InvalidArgumentException(sprintf('Unsupported FHIR version "%s". Supported values: R4, R4B, R5.', $fhirVersion)), |
| 45 | + }; |
| 46 | + } |
| 47 | + |
| 48 | + /** @param list<FHIRValidationViolation> $violations */ |
| 49 | + private function buildR4(array $violations): R4Outcome |
| 50 | + { |
| 51 | + return new R4Outcome( |
| 52 | + issue: $violations !== [] |
| 53 | + ? array_map(fn (FHIRValidationViolation $v): R4Issue => new R4Issue( |
| 54 | + severity: new R4IssueSeverityType($this->mapSeverity($v->severity)), |
| 55 | + code: new R4IssueTypeType($this->mapIssueType($v)), |
| 56 | + diagnostics: $v->message, |
| 57 | + expression: $v->path !== '' ? [$v->path] : [], |
| 58 | + ), $violations) |
| 59 | + : [new R4Issue( |
| 60 | + severity: new R4IssueSeverityType('information'), |
| 61 | + code: new R4IssueTypeType('informational'), |
| 62 | + diagnostics: 'No issues found — resource is valid.', |
| 63 | + )], |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + /** @param list<FHIRValidationViolation> $violations */ |
| 68 | + private function buildR4B(array $violations): R4BOutcome |
| 69 | + { |
| 70 | + return new R4BOutcome( |
| 71 | + issue: $violations !== [] |
| 72 | + ? array_map(fn (FHIRValidationViolation $v): R4BIssue => new R4BIssue( |
| 73 | + severity: new R4BIssueSeverityType($this->mapSeverity($v->severity)), |
| 74 | + code: new R4BIssueTypeType($this->mapIssueType($v)), |
| 75 | + diagnostics: $v->message, |
| 76 | + expression: $v->path !== '' ? [$v->path] : [], |
| 77 | + ), $violations) |
| 78 | + : [new R4BIssue( |
| 79 | + severity: new R4BIssueSeverityType('information'), |
| 80 | + code: new R4BIssueTypeType('informational'), |
| 81 | + diagnostics: 'No issues found — resource is valid.', |
| 82 | + )], |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + /** @param list<FHIRValidationViolation> $violations */ |
| 87 | + private function buildR5(array $violations): R5Outcome |
| 88 | + { |
| 89 | + return new R5Outcome( |
| 90 | + issue: $violations !== [] |
| 91 | + ? array_map(fn (FHIRValidationViolation $v): R5Issue => new R5Issue( |
| 92 | + severity: new R5IssueSeverityType($this->mapSeverity($v->severity)), |
| 93 | + code: new R5IssueTypeType($this->mapIssueType($v)), |
| 94 | + diagnostics: $v->message, |
| 95 | + expression: $v->path !== '' ? [$v->path] : [], |
| 96 | + ), $violations) |
| 97 | + : [new R5Issue( |
| 98 | + severity: new R5IssueSeverityType('information'), |
| 99 | + code: new R5IssueTypeType('informational'), |
| 100 | + diagnostics: 'No issues found — resource is valid.', |
| 101 | + )], |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + private function mapSeverity(string $severity): string |
| 106 | + { |
| 107 | + return match ($severity) { |
| 108 | + 'error' => 'error', |
| 109 | + 'warning' => 'warning', |
| 110 | + default => 'information', |
| 111 | + }; |
| 112 | + } |
| 113 | + |
| 114 | + private function mapIssueType(FHIRValidationViolation $violation): string |
| 115 | + { |
| 116 | + if ($violation->code === FHIRViolationCode::EVAL_ERROR |
| 117 | + || $violation->code === FHIRViolationCode::UNCHECKED_BINDING |
| 118 | + || $violation->constraintClass === FHIRValidationService::class) { |
| 119 | + return 'not-supported'; |
| 120 | + } |
| 121 | + |
| 122 | + if ($violation->code === FHIRViolationCode::INFO) { |
| 123 | + return 'informational'; |
| 124 | + } |
| 125 | + |
| 126 | + return match ($violation->constraintClass) { |
| 127 | + FHIRPathInvariant::class => 'invariant', |
| 128 | + FHIRValueSetBinding::class => 'value', |
| 129 | + default => 'invalid', |
| 130 | + }; |
| 131 | + } |
| 132 | +} |
0 commit comments