Skip to content

Generated Code

Daniel Carbone edited this page Apr 21, 2026 · 2 revisions

Generated Code

After running the generator you will have a self-contained PHP library under your configured libraryPath. This page describes its layout and the main entities you'll work with.

Output Directory Structure

Using the default namespace DCarbone\PHPFHIRGenerated and two versions (R4 + R5), the generated tree looks like this:

output/src/DCarbone/PHPFHIRGenerated/
├── Autoloader.php                 ← register with spl_autoload_register or Composer
├── Constants.php                  ← library-wide constants
├── FHIRVersion.php                ← version metadata class
├── Client/                        ← FHIR REST client
│   ├── Client.php
│   ├── ClientInterface.php
│   ├── Config.php
│   ├── Request.php
│   ├── Response.php
│   ├── ResponseHeaders.php
│   ├── HTTPMethodEnum.php
│   ├── SortDirectionEnum.php
│   └── *Exception.php
├── Encoding/                      ← serialization helpers
│   ├── SerializeConfig.php
│   ├── UnserializeConfig.php
│   ├── SerializeFormatEnum.php
│   ├── ResourceParser.php
│   ├── XMLWriter.php
│   ├── ValueXMLLocationEnum.php
│   └── *Trait.php
├── Types/                         ← shared type interfaces & traits
│   ├── TypeInterface.php
│   ├── PrimitiveTypeInterface.php
│   ├── ElementTypeInterface.php
│   ├── ResourceTypeInterface.php
│   ├── ContainedTypeInterface.php
│   ├── CommentContainerInterface.php
│   ├── ...Trait.php
│   └── ...
├── Validation/                    ← validation framework
│   ├── Validator.php
│   ├── RuleInterface.php
│   └── Rules/
├── Mapping/                       ← version mapping helpers
└── Versions/
    ├── VersionInterface.php
    ├── VersionConfigInterface.php
    ├── VersionTypeMapInterface.php
    ├── VersionConfig.php
    ├── R4/
    │   ├── Autoloader.php
    │   ├── Version.php
    │   ├── VersionConstants.php
    │   ├── VersionTypeMap.php
    │   ├── VersionClient.php
    │   ├── VersionResourceTypeEnum.php
    │   ├── VersionContainedTypeInterface.php
    │   ├── VersionResourceTypeInterface.php
    │   └── Types/
    │       ├── FHIRPatient.php
    │       ├── FHIRObservation.php
    │       └── ... (hundreds of type classes)
    └── R5/
        └── ... (same structure)

Core Entities (Shared)

These entities are version-independent and live directly under the library namespace.

Autoloader

Autoloader.php registers a custom spl_autoload_register handler for all generated classes. If you use Composer's autoloader for the generated code you may not need this, but it's available for standalone use.

Client

A minimal FHIR REST client built on ext-curl. Key classes:

Class Purpose
Client Sends HTTP requests to a FHIR server.
Config Client configuration (base URL, auth, timeouts).
Request / Response Value objects wrapping HTTP request/response data.
ResponseHeaders Parsed HTTP response headers.
HTTPMethodEnum GET, POST, PUT, DELETE, etc.
SortDirectionEnum ASC / DESC for search sorting.

Encoding

Handles JSON ↔ PHP ↔ XML conversion.

Class Purpose
SerializeConfig Options for serializing types to JSON/XML.
UnserializeConfig Options for parsing JSON/XML into types.
ResourceParser Detects the resourceType field and delegates to the correct type class.
XMLWriter Extended XMLWriter with FHIR-aware helpers.
SerializeFormatEnum JSON / XML.
ValueXMLLocationEnum Tracks whether a primitive value is rendered as an XML attribute or element.

Types (Interfaces & Traits)

Every generated FHIR type implements one or more of these interfaces:

Interface Meaning
TypeInterface Root interface for all types.
PrimitiveTypeInterface Wraps a PHP scalar value (string, int, bool, float).
ElementTypeInterface A FHIR Element (complex type with properties).
PrimitiveContainerTypeInterface An Element whose primary purpose is to carry a primitive value.
ValueContainerTypeInterface Has a value property.
ResourceTypeInterface A FHIR Resource (has an id, meta, etc.).
ContainedTypeInterface A Resource that can appear inside a contained array.
ResourceContainerTypeInterface The polymorphic "contained resource" wrapper.
ResourceIDTypeInterface A type that serves as a resource ID.
CommentContainerInterface Supports XML comment preservation.

Shared traits (ValueContainerTrait, CommentContainerTrait, SourceXMLNamespaceTrait) provide default implementations.

Validation

Class Purpose
Validator Runs a set of rules against a type instance.
RuleInterface Contract for individual validation rules.
Built-in rules MinOccursRule, MaxOccursRule, ValueMinLengthRule, ValueMaxLengthRule, ValueOneOfRule, ValuePatternMatchRule

Per-Version Entities

Each configured FHIR version gets its own sub-namespace (e.g. Versions\R4).

Version

Version.php implements VersionInterface and provides the version's name, FHIR semantic version, and access to its VersionTypeMap and VersionConfig.

Key constants and methods:

Constant / Method Type Description
NAME string The version name (e.g. 'R4').
FHIR_SEMANTIC_VERSION string Full version string as reported by the source XSD (e.g. 'v6.0.0-ballot4').
FHIR_SHORT_VERSION string Major.Minor form (e.g. '6.0').
FHIR_VERSION_INTEGER int Integer derived from the base version. Pre-release and GA share the same value.
FHIR_PRE_RELEASE null|string Pre-release suffix (e.g. 'ballot4'), or null for GA releases.
FHIR_GENERATION_DATE string Date the FHIR source schemas were generated.
static getFHIRVersion() FHIRVersion Singleton FHIRVersion value object for this version.
getFHIRSemanticVersion() string Returns FHIR_SEMANTIC_VERSION.
getFHIRShortVersion() string Returns FHIR_SHORT_VERSION.
getFHIRVersionInteger() int Returns FHIR_VERSION_INTEGER.
getFHIRPreRelease() null|string Returns FHIR_PRE_RELEASE.
isFHIRPreRelease() bool true when FHIR_PRE_RELEASE is not null.
getFHIRGenerationDate() string Returns FHIR_GENERATION_DATE.

The FHIRVersion value object (shared across all versions) mirrors the same API and additionally exposes the R6_MIN_VERSION_INTEGER constant alongside the existing DSTU2_MIN_VERSION_INTEGER through R5_MIN_VERSION_INTEGER constants for use in version-range comparisons.

VersionConstants

Contains string constants for every type name in the version, plus the FHIR semantic version and generation date.

VersionTypeMap

Maps FHIR type names (strings) to their PHP class names. Used by ResourceParser to resolve resourceType → class during unserialization.

VersionClient

A version-scoped FHIR client that automatically wires up the correct type map for parsing responses.

VersionResourceTypeEnum

A PHP enum listing every resource type name in the version.

Type Classes

Each FHIR type becomes a PHP class under Versions\<Name>\Types\. Type classes vary by kind:

Kind Template Notes
Primitive class_primitive.php Wraps a PHP scalar; has getValue() / setValue().
Resource Container class_resource_container.php The polymorphic contained wrapper.
XHTML class_xhtml.php Special handling for the narrative div element.
Default (Element, Resource, BackboneElement, etc.) class_default.php Standard class with typed properties, getters, setters, and JSON/XML (de)serialization.

Every type class supports:

  • Construction from an associative array.
  • jsonSerialize() (implements \JsonSerializable).
  • xmlSerialize() / static xmlUnserialize().
  • Validation via the shared Validator.

Generated Tests

When testsPath is configured, the generator also produces PHPUnit test classes:

output/tests/Tests/DCarbone/PHPFHIRGenerated/
├── TestAutoloader.php
├── Mock/                          ← Mock types for core tests
│   ├── MockResourceType.php
│   ├── MockContainedResourceType.php
│   └── ...
├── ConstantsTest.php              ← Core constant tests
├── Encoding/                      ← Encoding config tests
├── Client/                        ← Client config tests
└── Versions/
    └── R4/
        ├── VersionConstantsTest.php
        └── Types/
            ├── FHIRPatientTest.php
            └── ...

See Testing for how to run them.


Next: Testing →

Clone this wiki locally