Repository navigation
Translation
Use DCarbone\PHPFHIRGenerated\Translation\Translator to turn a typed resource from one FHIR release into a
resource from another. Translation is structural: a field that can be copied is not necessarily valid or
clinically equivalent in the target release.
Parse JSON or XML with the source Version. The translator infers the source release from the resulting PHP
class; the second argument to translate() is the target Version. This example uses package v4.3.1:
<?php
use DCarbone\PHPFHIRGenerated\Encoding\ResourceParser;
use DCarbone\PHPFHIRGenerated\Translation\Translator;
use DCarbone\PHPFHIRGenerated\Versions\R4\Version as R4Version;
use DCarbone\PHPFHIRGenerated\Versions\R5\Version as R5Version;
require __DIR__ . '/vendor/autoload.php';
$json = '{"resourceType":"Patient","id":"patient-1","active":true}';
$patient = ResourceParser::parse(new R4Version(), $json);
$result = (new Translator())->translate($patient, new R5Version());
$r5Patient = $result->getResource(); // R5 FHIRPatient, with the same id and active flag
$notices = $result->getNotices(); // [] for this PatientIf you already have a generated resource, pass it directly; there is no need to serialize and parse it again. Translating to the same release returns the original resource, not a clone.
v4.3.1 ships a map in each direction between adjacent releases:
DSTU1 <-> DSTU2 <-> STU3 <-> R4 <-> R4B <-> R5
The translator takes the shortest available route that moves only toward the target release. The Patient above
passes through R4B; there is no bundled direct R4-to-R5 map. Fields must be representable at each step.
A direct custom translator for the root type takes precedence. If there are multiple equally short routes,
the translator raises AmbiguousTranslationRouteException rather than choosing one arbitrarily.
Pass a TranslationConfig to Translator to choose what happens when a populated source field has no target
representation. All three settings are cases of OnUnmappableFieldEnum:
| Policy | Behavior |
|---|---|
EXTENSION (default) |
For a field absent from the target, try to preserve it in an official HL7 cross-version extension. If no supported definition can carry the value, throw MissingXverDefinitionException. |
THROW |
Stop at the first unmappable value with UnmappableFieldException. |
DROP |
Omit the value and record a dropped notice. Use only when losing data is acceptable. |
For example, Account.currency exists in R5 but not R4B. With DROP, the R4B Account omits currency and
getNotices() identifies that field and the R5-to-R4B step. Each notice exposes getPath(), getKind(),
getSourceVersion(), getTargetVersion(), and getMessage(). See the
runnable Account example.
A root resource with no target representation fails under every policy: there is no resource to return.
The v4.3.1 hop maps contain no ConceptMaps or cross-version extension definitions. Compatible fields still
translate, but Coding values are not remapped and the default policy cannot preserve a removed field such as
Account.currency. The R4 XverR5R4 profile classes are not translation maps.
In a custom build, the generator can
compile HL7 cross-version (xver) packages into the hop maps. This is done at generation time, not by
downloading packages or contacting a terminology server during translation:
- A directional ConceptMap can translate a
Codingwith asystemandcodewhen it identifies one eligible equivalent target. A system with no map is copied unchanged. Maps are not inverted for the return trip. - A supported extension definition can carry a target-absent field in
extensionormodifierExtension. On the reverse hop, a recognized extension is unfolded into the native field only if its contents fit, without overwriting an existing single-valued field. Unfolding is attempted under all three policies.
For a type that needs more than field copying, implement TypeTranslatorInterface and register it with
$translator->register($fromVersion, $toVersion, 'Patient', $patientTranslator). Registration applies to that
direction and FHIR type name only. Its translate(TypeInterface $source, TranslationContext $context) method
returns a target-version type, or null when the source cannot be represented. For a root resource it must return
a resource to succeed. Use $context->translateField() for children to retain field paths and notices.
To change how the generic translator discovers or matches fields, pass a FieldMetadataFactoryInterface as the
metadataFactory: constructor argument to Translator. Its forType() method returns FieldMetadata objects
keyed by the field name to match, each describing the getter, setter, type, and whether the field is a collection.
Bare code fields such as Encounter.status are copied without terminology mapping or target-binding validation,
even in builds with ConceptMaps. The generic translator does not reconstruct renamed fields or guarantee a
semantic conversion when a field changes type. FHIR comments (fhir_comments) are not preserved.
Inspect the notices and run _getValidationErrors() on the target. Even an empty notice list and no generated
validation errors do not establish target profile conformance or clinical equivalence. Check the target
terminology, profiles, and meaning before relying on the result. See Validation.