|
1 | 1 | # PHP FHIR Tools |
2 | 2 |
|
3 | | -This project generates PHP model classes and enums from FHIR Structure Definitions using Symfony Console and Nette PhpGenerator. It also provides a comprehensive FHIR serialization system for converting between FHIR objects and JSON/XML formats. |
| 3 | +A PHP library monorepo for working with [FHIR](https://www.hl7.org/fhir/) (Fast Healthcare Interoperability Resources). This monorepo contains multiple standalone packages that can be used independently or together: |
4 | 4 |
|
5 | | -## Requirements |
6 | | -- PHP >= 8.2 |
7 | | -- Composer |
| 5 | +- **FHIR Code Generation**: Generate PHP model classes from FHIR Structure Definitions |
| 6 | +- **FHIR Serialization**: JSON/XML serialization and deserialization |
| 7 | +- **FHIRPath**: Expression evaluator for FHIRPath 2.0 |
| 8 | +- **FHIR Models**: Pre-generated FHIR model classes (R4, R4B, R5) |
| 9 | +- **FHIR Bundle**: Symfony integration bundle |
8 | 10 |
|
9 | 11 | ## Installation |
10 | | -Clone the repository and install dependencies: |
| 12 | + |
| 13 | +### As a Library (Recommended) |
| 14 | + |
| 15 | +Install individual components in your project: |
| 16 | + |
11 | 17 | ```bash |
12 | | -composer install |
| 18 | +# For Symfony applications - includes console commands |
| 19 | +composer require ardenexal/fhir-bundle |
| 20 | + |
| 21 | +# For code generation |
| 22 | +composer require ardenexal/fhir-code-generation |
| 23 | + |
| 24 | +# For serialization |
| 25 | +composer require ardenexal/fhir-serialization |
| 26 | + |
| 27 | +# For FHIRPath evaluation |
| 28 | +composer require ardenexal/fhir-path |
| 29 | + |
| 30 | +# For pre-generated models |
| 31 | +composer require ardenexal/fhir-models |
13 | 32 | ``` |
14 | 33 |
|
15 | | -## Usage |
16 | | -Run the Symfony Console application: |
| 34 | +### For Development |
| 35 | + |
| 36 | +Clone and develop on this monorepo: |
| 37 | + |
17 | 38 | ```bash |
18 | | -php bin/console |
| 39 | +git clone https://github.com/Ardenexal/php-fhir-tools.git |
| 40 | +cd php-fhir-tools |
| 41 | +composer install |
19 | 42 | ``` |
20 | 43 |
|
21 | | -### Available Commands |
| 44 | +## Quick Start |
22 | 45 |
|
23 | | -#### `fhir:generate` |
24 | | -Generates FHIR model classes from FHIR definitions. |
| 46 | +### Using FHIR Bundle in Symfony |
25 | 47 |
|
26 | | -**Arguments:** |
27 | | -- `version` (required): Select FHIR version to generate model classes for. |
28 | | - - Suggested values: `R4`, `R4B`, `R5` |
| 48 | +If you've installed `ardenexal/fhir-bundle` in your Symfony application: |
29 | 49 |
|
30 | | -**Example:** |
31 | 50 | ```bash |
32 | | -php bin/console fhir:generate R4B |
33 | | -``` |
| 51 | +# Generate FHIR models |
| 52 | +php bin/console fhir:generate --package=hl7.fhir.r4.core -vvv |
34 | 53 |
|
35 | | -## FHIR Serialization |
| 54 | +# Evaluate FHIRPath expressions |
| 55 | +php bin/console fhir:path:evaluate "5 + 3" |
36 | 56 |
|
37 | | -This project includes a comprehensive FHIR serialization system that provides: |
| 57 | +# Against a JSON file |
| 58 | +php bin/console fhir:path:evaluate "Patient.name.given" patient.json |
38 | 59 |
|
39 | | -- **JSON and XML serialization** following official FHIR specifications |
40 | | -- **Configurable validation modes** (strict/lenient) |
41 | | -- **Flexible unknown element handling** (ignore/error/preserve) |
42 | | -- **Performance optimization options** |
43 | | -- **Debug information support** |
44 | | -- **Extension handling** with proper FHIR formatting |
| 60 | +# Output as JSON |
| 61 | +php bin/console fhir:path:evaluate "name" patient.json --format=json --pretty |
| 62 | +``` |
45 | 63 |
|
46 | | -### Quick Example |
| 64 | +### Serialize FHIR Resources |
47 | 65 |
|
48 | 66 | ```php |
49 | | -use Symfony\Component\Serializer\Serializer; |
50 | | -use Ardenexal\FHIRTools\Serialization\FHIRResourceNormalizer; |
51 | | -use Ardenexal\FHIRTools\Serialization\FHIRSerializationContext; |
| 67 | +use Ardenexal\FHIRTools\Component\Serialization\FHIRSerializationService; |
52 | 68 |
|
53 | | -// Set up FHIR-aware serializer |
54 | | -$normalizers = [new FHIRResourceNormalizer($metadataExtractor, $typeResolver)]; |
55 | | -$serializer = new Serializer($normalizers); |
| 69 | +// Serialize to JSON |
| 70 | +$json = $service->serializeToJson($patient); |
56 | 71 |
|
57 | | -// Configure serialization context |
58 | | -$context = FHIRSerializationContext::forJson() |
59 | | - ->withValidationMode(FHIRSerializationContext::VALIDATION_STRICT); |
| 72 | +// Deserialize from JSON |
| 73 | +$patient = $service->deserializeFromJson($json, FHIRPatient::class); |
60 | 74 |
|
61 | | -// Serialize FHIR resource |
62 | | -$json = $serializer->serialize($patient, 'json', $context->toSymfonyContext()); |
| 75 | +// Auto-detect format (JSON or XML) |
| 76 | +$resource = $service->deserialize($data); |
| 77 | +``` |
| 78 | + |
| 79 | +## Project Structure |
63 | 80 |
|
64 | | -// Deserialize back to FHIR object |
65 | | -$patient = $serializer->deserialize($json, FHIRPatient::class, 'json', $context->toSymfonyContext()); |
| 81 | +``` |
| 82 | +src/ |
| 83 | +├── Bundle/FHIRBundle/ # Symfony Bundle integration and console commands |
| 84 | +├── Component/ |
| 85 | +│ ├── CodeGeneration/src/ # FHIR model class generation from Structure Definitions |
| 86 | +│ ├── Serialization/src/ # FHIR JSON/XML serialization and deserialization |
| 87 | +│ ├── Models/src/ # Generated FHIR model classes (R4, R4B, R5) |
| 88 | +│ └── FHIRPath/src/ # FHIRPath 2.0 expression evaluator |
66 | 89 | ``` |
67 | 90 |
|
68 | | -For comprehensive documentation on the serialization system, see: **[FHIR Serialization Guide](docs/FHIR-Serialization-Guide.md)** |
69 | | - |
70 | | -## Documentation |
71 | | - |
72 | | -- **[FHIR Serialization Guide](docs/FHIR-Serialization-Guide.md)** - Comprehensive guide for using the FHIR serialization system |
73 | | - |
74 | | -## Composer Scripts |
75 | | -- Run static analysis: |
76 | | - ```bash |
77 | | - composer phpstan |
78 | | - ``` |
79 | | -- Run code style linter: |
80 | | - ```bash |
81 | | - composer lint |
82 | | - ``` |
83 | | -- Run tests: |
84 | | - ```bash |
85 | | - composer test |
86 | | - ``` |
87 | | -- Generate FHIR models: |
88 | | - ```bash |
89 | | - composer run generate-models-all |
90 | | - ``` |
91 | | - |
92 | | -## GitHub Actions |
93 | | - |
94 | | -### Regenerate FHIR Models |
95 | | -This repository includes a GitHub Actions workflow that allows you to manually regenerate FHIR models and commit them to the main branch. |
96 | | - |
97 | | -**To trigger the workflow:** |
98 | | -1. Go to the "Actions" tab in the GitHub repository |
99 | | -2. Select "Regenerate FHIR Models" from the workflows list |
100 | | -3. Click "Run workflow" |
101 | | -4. Optionally customize the commit message (default: "chore: regenerate FHIR models") |
102 | | -5. Click the green "Run workflow" button |
103 | | - |
104 | | -The workflow will: |
105 | | -- Set up PHP 8.3 environment with required extensions |
106 | | -- Install Composer dependencies |
107 | | -- Run `composer run generate-models-all` to generate models for R4, R4B, and R5 |
108 | | -- Automatically commit and push changes to main if models were updated |
109 | | -- Provide a summary of the operation |
| 91 | +Each component has its own README with detailed documentation: |
110 | 92 |
|
111 | | -## Project Structure |
112 | | -- `src/` - Source code |
113 | | -- `bin/console` - Symfony Console entry point |
114 | | -- `resources/definitions/` - FHIR definition files |
| 93 | +- [FHIRBundle](src/Bundle/FHIRBundle/README.md) — Symfony integration and console commands |
| 94 | +- [CodeGeneration](src/Component/CodeGeneration/README.md) — Model generation from FHIR packages |
| 95 | +- [Serialization](src/Component/Serialization/README.md) — JSON/XML serialization |
| 96 | +- [FHIRPath](src/Component/FHIRPath/README.md) — FHIRPath expression evaluation |
| 97 | +- [Models](src/Component/Models/README.md) — Generated FHIR model classes |
115 | 98 |
|
116 | | -## License |
| 99 | +## Monorepo Structure |
| 100 | + |
| 101 | +This is a library monorepo. Each component can be installed and used independently: |
| 102 | + |
| 103 | +| Package | Description | Composer Install | |
| 104 | +|---------|-------------|------------------| |
| 105 | +| `ardenexal/fhir-bundle` | Symfony Bundle with console commands | `composer require ardenexal/fhir-bundle` | |
| 106 | +| `ardenexal/fhir-code-generation` | Generate PHP from FHIR definitions | `composer require ardenexal/fhir-code-generation` | |
| 107 | +| `ardenexal/fhir-serialization` | JSON/XML serialization | `composer require ardenexal/fhir-serialization` | |
| 108 | +| `ardenexal/fhir-path` | FHIRPath expression evaluator | `composer require ardenexal/fhir-path` | |
| 109 | +| `ardenexal/fhir-models` | Pre-generated FHIR models | `composer require ardenexal/fhir-models` | |
117 | 110 |
|
118 | | -This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. |
| 111 | +## Development |
| 112 | + |
| 113 | +### Code Quality |
| 114 | + |
| 115 | +```bash |
| 116 | +# Fix code style (PSR-12 via Laravel Pint) |
| 117 | +composer lint |
| 118 | + |
| 119 | +# Static analysis (PHPStan level 8) |
| 120 | +composer phpstan |
| 121 | + |
| 122 | +# Run all tests |
| 123 | +composer test |
| 124 | + |
| 125 | +# Run specific test suites |
| 126 | +composer test-unit |
| 127 | +composer test-integration |
| 128 | +composer test-fhir |
| 129 | + |
| 130 | +# Full quality check (lint + phpstan + test) |
| 131 | +composer quality:all |
| 132 | +``` |
| 133 | + |
| 134 | +### Component-Specific Quality Checks |
| 135 | + |
| 136 | +```bash |
| 137 | +composer quality:bundle |
| 138 | +composer quality:codegen |
| 139 | +composer quality:serialization |
| 140 | +composer quality:fhir-path |
| 141 | +``` |
| 142 | + |
| 143 | +## Console Commands (via FHIR Bundle) |
| 144 | + |
| 145 | +When you install `ardenexal/fhir-bundle` in your Symfony application, you get these console commands: |
| 146 | + |
| 147 | +| Command | Description | |
| 148 | +|---------|-------------| |
| 149 | +| `fhir:generate` | Generate PHP model classes from FHIR packages | |
| 150 | +| `fhir:path:evaluate` | Evaluate a FHIRPath expression against FHIR data | |
| 151 | +| `fhir:path:validate` | Validate FHIRPath expression syntax | |
| 152 | + |
| 153 | +**Note**: These commands are only available when using the FHIR Bundle in a Symfony application. This monorepo is a library, not an application. |
| 154 | + |
| 155 | +## Contributing |
| 156 | + |
| 157 | +See [CONTRIBUTING.md](CONTRIBUTING.md) for development setup, coding standards, and contribution guidelines. |
| 158 | + |
| 159 | +## License |
119 | 160 |
|
| 161 | +This project is licensed under the MIT License — see the [LICENSE](LICENSE) file for details. |
0 commit comments