romeldev / php-fhir-r5
HL7 FHIR R5 PHP library, PHP 7.4+ compatible. Generated from dcarbone/php-fhir and downgraded with Rector.
Requires
- php: >=7.4
- ext-ctype: *
- ext-curl: *
- ext-dom: *
- ext-json: *
- ext-libxml: *
- ext-simplexml: *
- ext-xmlreader: *
- ext-xmlwriter: *
- composer/semver: ^3.4
- psr/log: ^1.1 || ^2.0 || ^3.0
- symfony/polyfill-php80: ^1.27
Requires (Dev)
- phpunit/phpunit: ^9.6
README
HL7 FHIR R5 PHP library, compatible with PHP 7.4+.
This package is a downgraded build of dcarbone/php-fhir (which targets PHP 8.1+), produced with Rector so it can be consumed by legacy applications — typically Laravel projects still on PHP 7.4.
It provides fully-typed PHP models for every FHIR R5 resource and data type, JSON & XML (de)serialization, validation rules, and a ready-to-use FHIR REST client.
Requirements
- PHP 7.4 or newer
- PHP extensions:
ctype,curl,dom,json,libxml,simplexml,xmlreader,xmlwriter
Installation
composer require romeldev/php-fhir-r5
Usage
All classes live under the Romeldev\Fhir namespace. FHIR types for the R5 version are nested under Romeldev\Fhir\Versions\R5\Types.
Parse a FHIR JSON resource
use Romeldev\Fhir\Versions\R5\Version; use Romeldev\Fhir\Versions\R5\Types\FHIRBase\FHIRResource\FHIRDomainResource\FHIRPatient; $version = new Version(); $json = file_get_contents('/path/to/patient.json'); $decoded = json_decode($json); $patient = FHIRPatient::jsonUnserialize( $decoded, $version->getConfig()->getUnserializeConfig() ); foreach ($patient->getName() as $name) { echo $name->getFamily()->getValue() . "\n"; }
Serialize a resource back to JSON
$encoded = json_encode($patient);
Parse FHIR XML
$xml = simplexml_load_file('/path/to/patient.xml'); $patient = FHIRPatient::xmlUnserialize( $xml, $version->getConfig()->getUnserializeConfig() );
Use the REST client
use Romeldev\Fhir\Versions\R5\VersionClient; use Romeldev\Fhir\Client\Client; use Romeldev\Fhir\Encoding\SerializeFormatEnum; use Romeldev\Fhir\Versions\R5\VersionResourceTypeEnum; $client = new VersionClient( new Client('https://my-fhir-server.example.com/fhir'), $version ); $response = $client->read( VersionResourceTypeEnum::PATIENT, 'example-patient-id', SerializeFormatEnum::JSON ); if ($response->getCode() === 200) { $patient = FHIRPatient::jsonUnserialize( json_decode($response->getResp()), $version->getConfig()->getUnserializeConfig() ); }
Namespace layout
Romeldev\Fhir\
├── Client\ → HTTP client, response objects, enums (method, sort)
├── Encoding\ → JSON/XML (un)serialization configs, XMLWriter helper
├── Types\ → Shared interfaces (ElementTypeInterface, etc.)
├── Validation\ → Validator + rule classes
└── Versions\R5\
├── Version → entry point for the R5 version
├── VersionClient → R5-specific REST client
├── VersionConfig → config used by (un)serialize
├── VersionResourceTypeEnum
└── Types\ → every FHIR R5 resource and data type
Compatibility notes
- No PHP 8+ features at runtime. Enums, union types,
match, named arguments andmixedhave been transformed to their PHP 7.4 equivalents (classes with constants, PHPDoc@return,switch, positional args, untyped). - Constants instead of enums. What was
enum Foo: stringin the original is nowclass Foo { public const XML = 'xml'; ... }. UseFoo::XMLas before — the API surface is unchanged. Type hints that originally referenced these enums (e.g.SerializeFormatEnum $format) have been rewritten tostringsince the constants are now string literals. #[Attribute]markers on classes are preserved as#[...]lines — PHP 7.4 parses them as single-line comments, so they are inert.- Warnings-free under PHP 7.4
php -l(includingXMLWritersubclass signature compatibility). - Test suite passes on PHP 7.4 — 5368 tests / 19743 assertions, using PHPUnit 9.6 and
symfony/polyfill-php80(forpreg_last_error_msg()).
Running the test suite
composer install composer test # 5368 tests, no network required composer test:all # all tests, requires a FHIR R5 server at 127.0.0.1:8080/R5
Upstream & attribution
All generation logic, schemas and the original library architecture belong to Daniel Carbone and the dcarbone/php-fhir project. This package only:
- Generates under a different PHP namespace (
Romeldev\Fhirinstead ofDCarbone\PHPFHIRGenerated). - Transpiles the output down to PHP 7.4 with Rector.
If you can run PHP 8.1+, prefer the upstream dcarbone/php-fhir-generated directly.
License
Apache License 2.0 — same as upstream. See LICENSE.