Installation
composer require ardenexal/fhir-serialization
Ensure your project uses PHP 8.0+ (check composer.json constraints).
First Use Case: Parsing FHIR JSON
use Ardenexal\FhirSerialization\FhirSerializer;
$serializer = new FhirSerializer();
$fhirData = $serializer->parse('{
"resourceType": "Patient",
"id": "example",
"name": [{"family": "Doe"}]
}');
FhirSerializer (core parser)FhirResource (base resource model)FhirBundle (for handling FHIR Bundles)Where to Look First
tests/ for real-world examples (e.g., PatientResourceTest).src/FhirSerializer.php for core logic.Deserialization
$patient = $serializer->parse($jsonString, PatientResource::class);
// Access fields:
$patient->getId(); // "example"
$patient->getName()[0]->getFamily(); // "Doe"
Validation
try {
$serializer->validate($jsonString);
} catch (FhirValidationException $e) {
// Handle errors (e.g., missing required fields)
}
Bundle Processing
$bundle = $serializer->parse($bundleJson, FhirBundle::class);
foreach ($bundle->getEntry() as $entry) {
$resource = $entry->getResource();
// Process each resource (Patient, Observation, etc.)
}
Laravel Service Provider Bind the serializer to the container for dependency injection:
$this->app->singleton(FhirSerializer::class, function ($app) {
return new FhirSerializer();
});
Use in controllers:
public function __construct(private FhirSerializer $serializer) {}
Request Handling Parse FHIR payloads from API requests:
public function store(Request $request) {
$fhirData = $this->serializer->parse($request->json()->all());
// Process $fhirData
}
Testing
Use FhirSerializer in feature tests to validate FHIR responses:
$response = $this->postJson('/api/fhir', $fhirJson);
$response->assertJson($this->serializer->parse($fhirJson));
Strict FHIR Compliance
resourceType) throws FhirValidationException.Nested Resources
Observation with reference fields) require chained parsing:
$observation = $serializer->parse($json, ObservationResource::class);
$patientRef = $observation->getSubject(); // FhirReference
$patient = $serializer->parse($patientRef->getReference(), PatientResource::class);
Performance
Enable Verbose Validation Set the serializer to throw detailed errors:
$serializer = new FhirSerializer();
$serializer->setValidationMode(FhirSerializer::VALIDATION_VERBOSE);
Log FHIR Errors
Catch FhirValidationException and log the $e->getErrors() array for debugging:
catch (FhirValidationException $e) {
\Log::error('FHIR Validation Errors', $e->getErrors());
}
Custom Resource Classes
Extend FhirResource to add domain-specific logic:
class CustomPatientResource extends FhirResource {
public function isActive() {
return $this->getActive() === true;
}
}
Override Serialization
Implement JsonSerializable in custom resources to modify output:
class CustomBundle extends FhirBundle implements JsonSerializable {
public function jsonSerialize() {
$data = parent::jsonSerialize();
$data['meta'] = ['lastUpdated' => now()->toAtomString()];
return $data;
}
}
Profile Validation
Use the underlying FHIR\R4\Validator for profile-based validation (advanced):
$validator = $serializer->getValidator();
$validator->validate($fhirData, 'http://hl7.org/fhir/StructureDefinition/Patient');
Patient vs patient). Always use uppercase as per the spec.How can I help you explore Laravel packages today?