• Stars
    star
    104
  • Rank 329,567 (Top 7 %)
  • Language
    PHP
  • License
    Apache License 2.0
  • Created about 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Tools for consuming data from a FHIR server with PHP

php-fhir

Tools for creating PHP classes from the HL7 FHIR Specification

If you're looking to use the classes generated by this library, you may want the php-fhir-generated repo instead.

Index

  1. Install as Standalone Generator
  2. Install as Library
  3. Version Table
  4. Basic Usage
  5. Serializatoin
  6. Testing

Install as Standalone Generator

If you wish to use this package as a standalone generator:

  1. Check out the desired branch or tag
  2. Execute composer install from root of project directory
  3. Execute ./bin/generate.sh
  4. Answer all prompts
    • If no custom configuration file is defined, definitions will be downloaded to ./input and classes will be generated under ./output
    • You can execute ./bin/generate.sh --help for details on how to utilize this script
    • You can configure various aspects of this script by altering the values in ./bin/config.php

This script will download configured major versions of FHIR into the input folder and generate classes for every version in the output folder.

Install as Library

If you wish to use the generator as part of a project, you can include it as a composer dependency:

composer require dcarbone/php-fhir

From there, you can reference the Example block for a quick example on how to configure and execute the generator.

Version Table

PHPFHIR Version PHP Versions FHIR Versions
v2 5.4-7.4 DSTU1, DSTU2, STU3, R4 (<v4.3.0)
v3 7.4-8.1 DSTU1, DSTU2, STU3, R4

Basic Usage

The first step is to determine the version of the FHIR spec your implementation supports. Once done, download the appropriate class definition XSDs from http://hl7.org/fhir/directory.html.

Uncompress the XSD's and place them in a directory that is readable by PHP's runtime user.

Next comes the fun:

Class Generation

The class generator utility included with this library is designed to parse the XSD's provided by the FHIR group into PHP classes, complete with markup and type hinting.

There are 2 important things to note with this section:

  1. Your exact implementation will probably vary, don't hesitate to ask if you need some help
  2. The class generation should be run ONCE per FHIR version. Once the classes have been generated they should only ever be re-generated if your server switches to a new FHIR spec

Generation Example

require __DIR__.'/vendor/autoload.php';

$schemaPath = 'schema/path';

// first, build new configuration class
$config = new \DCarbone\PHPFHIR\Config([
    // The path to look look for and optionally download source XSD files to
    'schemaPath'  => __DIR__ . '/../input',

    // The path to place generated type class files
    'classesPath' => __DIR__ . '/../output',

    // If true, will use a noop null logger
    'silent'      => false,

    // If true, will skip generation of test classes
    'skipTests'   => false,

    // Map of versions and configurations to generate
    // Each entry in this map will grab the latest revision of that particular version.  If you wish to use a specific
    // version, please see http://www.hl7.org/fhir/directory.cfml
    'versions'    => [
        'DSTU1' => [
            // Source URL
            'url'       => 'http://hl7.org/fhir/DSTU1/fhir-all-xsd.zip',
            // Namespace to prefix the generated classes with
            'namespace' => '\\HL7\\FHIR\\DSTU1',
        ],
        'DSTU2' => [
            'url'       => 'http://hl7.org/fhir/DSTU2/fhir-all-xsd.zip',
            'namespace' => '\\HL7\\FHIR\\DSTU2',
        ],
        'STU3'  => [
            'url'       => 'http://hl7.org/fhir/STU3/definitions.xml.zip',
            'namespace' => '\\HL7\\FHIR\\STU3',
        ],
        'R4'    => [
            'url'       => 'http://www.hl7.org/fhir/fhir-codegen-xsd.zip',
            'namespace' => '\\HL7\\FHIR\\R4',
        ],
        'Build' => [
            'url'       => 'http://build.fhir.org/fhir-all-xsd.zip',
            'namespace' => '\\HL7\\FHIR\\Build',
        ],
    ],
]);

// next, build definition class
$version_config = new \DCarbone\PHPFHIR\Config\VersionConfig($config, $config->getVersion('R4'));
$definition = new \DCarbone\PHPFHIR\Definition($version_config);
$definition->buildDefinition();

$builder = new \DCarbone\PHPFHIR\Builder($config, $definition);
$builder->build();

Using the above code will generate class files under the included output directory, under the namespace HL7\\FHIR\\{version}

If you wish the files to be placed under a different directory, pass the path in as the 2nd argument in the generator constructor.

If you wish the classes to have a different base namespace, pass the desired NS name in as the 3rd argument in the generator constructor.

Data Querying

There are a plethora of good HTTP clients you can use to get data out of a FHIR server, so I leave that up to you.

Response Parsing

As part of the class generation above, a response parsing class called PHPFHIRResponseParser will be created and added into the root namespace directory. It currently supports JSON and XML response types.

The parser class takes a single optional boolean argument that will determine if it should attempt to load up the generated Autoloader class. By default it will do so, but you are free to configure your own autoloader and not use the generated one if you wish.

Parsing Example

require 'path to PHPFHIRResponseParserConfig.php';
require 'path to PHPFHIRResponseParser.php';

// build config
$config = new \YourConfiguredNamespace\PHPFHIRResponseParserConfig([
    'registerAutoloader' => true, // use if you are not using Composer
    'sxeArgs' => LIBXML_COMPACT | LIBXML_NSCLEAN // choose different libxml arguments if you want, ymmv.
]);

// build parser
$parser = new \YourConfiguredNamespace\PHPFHIRResponseParser($config);

// provide input, receive output.
$object = $parser->parse($yourResponseData);

Serialization

JSON Serialization

$json = json_encode($object);

XML Serialization

// To get an instance of \DOMElement...
$element = $object->xmlSerialize();

// to get as XML string...
$xml = $element->ownerDocument->saveXML($element);

XML Serialization utilizes DOM.

Testing

As part of class generation, a directory & namespace called PHPFHIRTests is created under the root namespace and output directory.

TODO

  • Implement event or pull-based XML parsing for large responses

Suggestions and help

If you have some suggestions for how this lib could be made more useful, more applicable, easier to use, etc, please let me know.

More Repositories

1

php-consul-api

PHP client implementation for the Consul API
PHP
79
star
2

php-fhir-generated

Pre-generated classes from dcarbone/php-fhir
PHP
25
star
3

soap-plus

Simple wrapper class around PHP's SoapClient class
PHP
25
star
4

curl-plus

A simple OO wrapper around PHP's cURL implementation
PHP
22
star
5

xml-writer-plus

Simple XML Writer library for PHP 5.4+
PHP
13
star
6

terraform-plugin-framework-utils

Utilities for use with the HashiCorp Terraform Plugin Framework
Go
7
star
7

helpers

Series of little helper classes with various uses
PHP
6
star
8

collection-plus

A PHP 5.3.3+ Collection implementation that takes inspiration from multiple sources
PHP
6
star
9

php-object-merge

This is a simple library that facilitates the merging of two or more PHP stdClass object properties
PHP
5
star
10

dom-plus

A simple DOMDocument class wrapper that adds some simple improvements
PHP
4
star
11

php-fhir-resources

Resource classes for the PHP implementation of the FHIR EHR specification
PHP
3
star
12

directory-iterator-plus

A simple extension of the PHP \DirectoryIterator class
PHP
3
star
13

tns-parser

Oracle tnsnames.ora file / string parser written in PHP
PHP
3
star
14

json-writer-plus

Simple JSON Writer library for PHP 5.3+
PHP
3
star
15

ci-asset-manager

A simple asset management library for CodeIgniter 2.2
PHP
3
star
16

php-consul-api-bundle

Bundle to enable usage of dcarbone/php-consul-api inside of a Symfony 3 project
PHP
3
star
17

phpipam-api

PHP SDK for the PHPIPAM API
PHP
2
star
18

json-to-go

PHP Implementation of mholt/json-to-go
PHP
2
star
19

smart-precision-cancer-medicine

SMART Genomics: Precision Cancer Medicine
Objective-C
2
star
20

yaml-to-env-action

Shell
2
star
21

gotime

Golang-like time class(es) for PHP 7.0+
PHP
2
star
22

jobber

Little job queue thing for golang
Go
2
star
23

table-mapper

TableMapper is a class that I wrote to ease the consumption of complex HTML tables.
PHP
2
star
24

lruchal

Go
1
star
25

tfcloud-provider-push-action

Go
1
star
26

seb

Simple event bus written in go
Go
1
star
27

agentman

Help utility for managing Consul TestServer instances
Go
1
star
28

go-cbidxr

Couchbase index reconciliation tools for golang
Go
1
star
29

go-strdur

An implementation of time.Duration with prettier serialization and stuff
Go
1
star
30

php-fhir-elements

Element Classes for the PHP implementation of the FHIR EHR specification.
PHP
1
star
31

vimrc

Vim Script
1
star
32

syncthing-ignore

various syncthing .ignore files i use for various things.
1
star
33

camel

A library designed to help facilitate SharePoint CAML query construction
PHP
1
star
34

xml-primitive-types

Library containing PHP classes for XML Primitives and Derived Primitives
PHP
1
star
35

php-fhir-common

Common classes for the PHP implementation of the FHIR EHR specification
PHP
1
star