Skip to content

Commit d9d0383

Browse files
authored
Merge pull request #161 from dcarbone/feature/v4-fhir-version-everywhere
moving the _getFHIRVersion func to the base Type interface
2 parents e6ee71e + 4a252ab commit d9d0383

16 files changed

+209
-83
lines changed

files/constants.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@
165165
const PHPFHIR_TEST_CLIENT_CLASSNAME_CONFIG = PHPFHIR_CLIENT_CLASSNAME_CONFIG . 'Test';
166166
const PHPFHIR_TEST_CLIENT_CLASSNAME_CLIENT = PHPFHIR_CLIENT_CLASSNAME_CLIENT . 'Test';
167167

168+
const PHPFHIR_TEST_CLASSNAME_ABSTRACT_MOCK_TYPE = 'AbstractMockType';
168169
const PHPFHIR_TEST_TRAIT_MOCK_TYPE_FIELDS = 'MockTypeFieldsTrait';
169170
const PHPFHIR_TEST_CLASSNAME_MOCK_STRING_PRIMITIVE_TYPE = 'MockStringPrimitiveType';
170171
const PHPFHIR_TEST_CLASSNAME_MOCK_ELEMENT_TYPE = 'MockElementType';

src/Utilities/ImportUtils.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,17 @@ public static function buildVersionTypeImports(Version $version, Type $type): vo
132132

133133
$logger->debug(sprintf('Compiling imports for Type "%s"...', $type->getFHIRName()));
134134

135-
$sourceMeta = $version->getSourceMetadata();
136-
137135
$imports = $type->getImports();
138136

139137
// immediately add self
140138
$imports->addImport($type->getFullyQualifiedNamespace(false), $type->getClassName());
141139

140+
// needed by any type without a parent to return the FHIR version of the type.
141+
if (!$type->hasParent()) {
142+
$imports->addCoreFileImportsByName(PHPFHIR_CLASSNAME_FHIR_VERSION)
143+
->addVersionCoreFileImportsByName($version, PHPFHIR_VERSION_CLASSNAME_VERSION);
144+
}
145+
142146
// few types are handled different. this is lazy and I hate it, but here we are.
143147
if ($type->getFHIRName() === PHPFHIR_XHTML_TYPE_NAME || $type->getKind()->isResourceContainer($version)) {
144148
return;

template/core/types/interface_resource_type.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,13 @@
2828
$serializeConfigClass = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG);
2929
$unserializeConfigClass = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG);
3030
$xmlWriterClass = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_CLASSNAME_XML_WRITER);
31-
$fhirVersion = $coreFiles->getCoreFileByEntityName(PHPFHIR_CLASSNAME_FHIR_VERSION);
3231
$resourceIDInterface = $coreFiles->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_RESOURCE_ID_TYPE);
3332

3433
$imports->addCoreFileImports(
3534
$typeInterface,
3635
$serializeConfigClass,
3736
$unserializeConfigClass,
3837
$xmlWriterClass,
39-
$fhirVersion,
4038
$resourceIDInterface,
4139
);
4240

@@ -52,14 +50,6 @@
5250
interface <?php echo $coreFile; ?> extends <?php echo $typeInterface; ?>
5351

5452
{
55-
/**
56-
* Must return the FHIR version of this type.
57-
*
58-
* @return <?php echo $fhirVersion->getFullyQualifiedName(true); ?>
59-
60-
*/
61-
public function _getFHIRVersion(): <?php echo $fhirVersion; ?>;
62-
6353
/**
6454
* Must return the root XMLNS value found in the source. Null indicates no "xmlns" was found. Only defined when
6555
* unserializing XML, and only used when serializing XML.

template/core/types/interface_type.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
$coreFiles = $config->getCoreFiles();
2525
$imports = $coreFile->getImports();
2626

27+
$fhirVersion = $coreFiles->getCoreFileByEntityName(PHPFHIR_CLASSNAME_FHIR_VERSION);
28+
29+
$imports->addCoreFileImport($fhirVersion);
30+
2731
ob_start();
2832
echo '<?php ';?>declare(strict_types=1);
2933

@@ -42,6 +46,14 @@ interface <?php echo $coreFile; ?> extends \JsonSerializable
4246
*/
4347
public function _getFHIRTypeName(): string;
4448

49+
/**
50+
* Must return the FHIR version of this type.
51+
*
52+
* @return <?php echo $fhirVersion->getFullyQualifiedName(true); ?>
53+
54+
*/
55+
public function _getFHIRVersion(): <?php echo $fhirVersion; ?>;
56+
4557
/**
4658
* Execute any and all validation rules present on this type and all nested field types.
4759
*
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php declare(strict_types=1);
2+
3+
/*
4+
* Copyright 2025 Daniel Carbone ([email protected])
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
use DCarbone\PHPFHIR\Utilities\ImportUtils;
20+
21+
/** @var \DCarbone\PHPFHIR\Config $config */
22+
/** @var \DCarbone\PHPFHIR\CoreFile $coreFile */
23+
24+
$coreFiles = $config->getCoreFiles();
25+
$testCoreFiles = $config->getCoreTestFiles();
26+
$imports = $coreFile->getImports();
27+
28+
$typeInterface = $coreFiles->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_TYPE);
29+
$fhirVersion = $coreFiles->getCoreFileByEntityName(PHPFHIR_CLASSNAME_FHIR_VERSION);
30+
31+
$imports->addCoreFileImports(
32+
$typeInterface,
33+
$fhirVersion,
34+
);
35+
36+
ob_start();
37+
echo '<?php'; ?> declare(strict_types=1);
38+
39+
namespace <?php echo $coreFile->getFullyQualifiedNamespace(false); ?>;
40+
41+
<?php echo $config->getBasePHPFHIRCopyrightComment(true); ?>
42+
43+
<?php echo ImportUtils::compileImportStatements($imports); ?>
44+
45+
abstract class <?php echo $coreFile; ?> implements <?php echo $typeInterface; ?>
46+
47+
{
48+
public const DEFAULT_MOCK_VERSION_NAME = 'mock';
49+
public const DEFAULT_MOCK_SEMANTIC_VERSION = 'v99.99.99';
50+
51+
protected string $_name;
52+
protected <?php echo $fhirVersion; ?> $_fhirVersion;
53+
54+
public function __construct(string $name,
55+
string $versionName = self::DEFAULT_MOCK_VERSION_NAME,
56+
string $semanticVersion = self::DEFAULT_MOCK_SEMANTIC_VERSION)
57+
{
58+
$this->_name = $name;
59+
60+
$shortVersion = ltrim($semanticVersion, 'v');
61+
$shortVersion = match (substr_count($shortVersion, '.')) {
62+
1 => $shortVersion,
63+
2 => substr($shortVersion, 0, strrpos($shortVersion, '.')),
64+
default => implode('.', array_chunk(explode('.', $shortVersion), 2)[0])
65+
};
66+
67+
$this->_fhirVersion = new <?php echo $fhirVersion; ?>(
68+
$versionName,
69+
$semanticVersion,
70+
$shortVersion,
71+
intval(sprintf("%'.-08s", str_replace(['v', '.'], '', $semanticVersion))),
72+
);
73+
}
74+
75+
public function _getFHIRTypeName(): string
76+
{
77+
return $this->_name;
78+
}
79+
80+
public function _getFHIRVersion(): <?php echo $fhirVersion; ?>
81+
82+
{
83+
return $this->_fhirVersion;
84+
}
85+
}
86+
<?php return ob_get_clean();

template/tests/core/types/class_mock_element_type.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
$unserializeConfig = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG);
3838
$serializeConfig = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG);
3939

40+
$mockAbstractTypeClass = $testCoreFiles->getCoreFileByEntityName(PHPFHIR_TEST_CLASSNAME_ABSTRACT_MOCK_TYPE);
4041
$mockTypeFieldsTrait = $testCoreFiles->getCoreFileByEntityName(PHPFHIR_TEST_TRAIT_MOCK_TYPE_FIELDS);
4142

4243
$imports->addCoreFileImports(
@@ -52,6 +53,7 @@
5253
$unserializeConfig,
5354
$serializeConfig,
5455

56+
$mockAbstractTypeClass,
5557
$mockTypeFieldsTrait,
5658
);
5759

@@ -64,7 +66,7 @@
6466

6567
<?php echo ImportUtils::compileImportStatements($imports); ?>
6668

67-
class <?php echo $coreFile; ?> implements <?php echo $elementTypeInterface; ?>, <?php echo $commentContainerInterface; ?>, \Iterator
69+
class <?php echo $coreFile; ?> extends <?php echo $mockAbstractTypeClass; ?> implements <?php echo $elementTypeInterface; ?>, <?php echo $commentContainerInterface; ?>, \Iterator
6870

6971
{
7072
use <?php echo $typeValidationTrait; ?>,
@@ -75,28 +77,24 @@ class <?php echo $coreFile; ?> implements <?php echo $elementTypeInterface; ?>,
7577

7678
private const _FHIR_VALIDATION_RULES = [];
7779

78-
protected string $_name;
79-
8080
private array $_valueXMLLocations = [];
8181

8282
public function __construct(string $name,
8383
array $fields = [],
8484
array $validationRuleMap = [],
85-
array $fhirComments = [])
85+
array $fhirComments = [],
86+
string $versionName = self::DEFAULT_MOCK_VERSION_NAME,
87+
string $semanticVersion = self::DEFAULT_MOCK_SEMANTIC_VERSION)
8688
{
87-
$this->_name = $name;
89+
parent::__construct($name, $versionName, $semanticVersion);
90+
8891
$this->_setFHIRComments($fhirComments);
8992
foreach($validationRuleMap as $field => $rules) {
9093
$this->_setFieldValidationRules($field, $rules);
9194
}
9295
$this->_processFields($fields);
9396
}
9497

95-
public function _getFHIRTypeName(): string
96-
{
97-
return $this->_name;
98-
}
99-
10098
public static function xmlUnserialize(\SimpleXMLElement $element,
10199
<?php echo $unserializeConfig; ?> $config,
102100
null|<?php echo $elementTypeInterface; ?> $type = null): self

template/tests/core/types/class_mock_primitive_container_type.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ class <?php echo $coreFile; ?> extends <?php echo $mockElementTypeClass; ?> impl
7272
<?php echo $xmlSerializationOptionsTrait; ?>,
7373
<?php echo $mockTypeFieldsTrait; ?>;
7474

75-
7675
private const _FHIR_VALIDATION_RULES = [];
7776

7877
private array $_valueXMLLocations = [];
@@ -81,7 +80,9 @@ public function __construct(string $name,
8180
array $fields = [],
8281
array $validationRuleMap = [],
8382
array $fhirComments = [],
84-
mixed $value = null)
83+
mixed $value = null,
84+
string $versionName = self::DEFAULT_MOCK_VERSION_NAME,
85+
string $semanticVersion = self::DEFAULT_MOCK_SEMANTIC_VERSION)
8586
{
8687
if (!isset($fields['value'])
8788
|| !isset($fields['value']['class'])
@@ -94,7 +95,7 @@ public function __construct(string $name,
9495
if (null !== $value) {
9596
$fields['value']['value'] = $value;
9697
}
97-
parent::__construct($name, $fields, $validationRuleMap, $fhirComments);
98+
parent::__construct($name, $fields, $validationRuleMap, $fhirComments, $versionName, $semanticVersion);
9899
}
99100

100101
public function _nonValueFieldDefined(): bool

template/tests/core/types/class_mock_resource_id_type.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,12 @@
5555
class <?php echo $coreFile; ?> extends <?php echo $mockPrimitiveContainerClass; ?> implements <?php echo $resourceIDTypeInterface; ?>
5656

5757
{
58-
5958
public function __construct(string|<?php echo $mockStringpPrimitiveClass; ?> $value,
6059
array $fields = [],
6160
array $validationRuleMap = [],
62-
array $fhirComments = [])
61+
array $fhirComments = [],
62+
string $versionName = self::DEFAULT_MOCK_VERSION_NAME,
63+
string $semanticVersion = self::DEFAULT_MOCK_SEMANTIC_VERSION)
6364
{
6465
parent::__construct(
6566
name: 'id',
@@ -76,6 +77,8 @@ public function __construct(string|<?php echo $mockStringpPrimitiveClass; ?> $va
7677
],
7778
] + $validationRuleMap,
7879
fhirComments: $fhirComments,
80+
versionName: $versionName,
81+
semanticVersion: $semanticVersion,
7982
);
8083
}
8184
}

template/tests/core/types/class_mock_resource_type.php

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
$unserializeConfig = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG);
4141
$serializeConfig = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG);
4242

43+
$mockAbstractTypeClass = $testCoreFiles->getCoreFileByEntityName(PHPFHIR_TEST_CLASSNAME_ABSTRACT_MOCK_TYPE);
4344
$mockTypeFieldsTrait = $testCoreFiles->getCoreFileByEntityName(PHPFHIR_TEST_TRAIT_MOCK_TYPE_FIELDS);
4445
$mockStringpPrimitiveClass = $testCoreFiles->getCoreFileByEntityName(PHPFHIR_TEST_CLASSNAME_MOCK_STRING_PRIMITIVE_TYPE);
4546
$mockResourceIDClass = $testCoreFiles->getCoreFileByEntityName(PHPFHIR_TEST_CLASSNAME_MOCK_RESOURCE_ID_TYPE);
@@ -60,6 +61,7 @@
6061
$unserializeConfig,
6162
$serializeConfig,
6263

64+
$mockAbstractTypeClass,
6365
$mockTypeFieldsTrait,
6466
$mockStringpPrimitiveClass,
6567
$mockResourceIDClass,
@@ -74,7 +76,7 @@
7476

7577
<?php echo ImportUtils::compileImportStatements($imports); ?>
7678

77-
class <?php echo $coreFile; ?> implements <?php echo $resourceTypeInterface; ?>, <?php echo $commentContainerInterface; ?>, \Iterator
79+
class <?php echo $coreFile; ?> extends <?php echo $mockAbstractTypeClass; ?> implements <?php echo $resourceTypeInterface; ?>, <?php echo $commentContainerInterface; ?>, \Iterator
7880

7981
{
8082
use <?php echo $typeValidationTrait; ?>,
@@ -86,35 +88,19 @@ class <?php echo $coreFile; ?> implements <?php echo $resourceTypeInterface; ?>,
8688

8789
private const _FHIR_VALIDATION_RULES = [];
8890

89-
protected string $_name;
90-
protected <?php echo $fhirVersion; ?> $_fhirVersion;
91-
9291
private array $_valueXMLLocations = [];
9392

9493
public function __construct(string $name,
9594
null|string|<?php echo $mockStringpPrimitiveClass; ?>|<?php echo $mockResourceIDClass; ?> $id = null,
9695
array $fields = [],
9796
array $validationRuleMap = [],
9897
array $fhirComments = [],
99-
string $versionName = 'mock',
100-
string $semanticVersion = 'v0.0.0')
98+
string $versionName = self::DEFAULT_MOCK_VERSION_NAME,
99+
string $semanticVersion = self::DEFAULT_MOCK_SEMANTIC_VERSION)
101100
{
102-
$this->_name = $name;
103-
$this->_setFHIRComments($fhirComments);
104-
105-
$shortVersion = ltrim($semanticVersion, 'v');
106-
$shortVersion = match (substr_count($shortVersion, '.')) {
107-
1 => $shortVersion,
108-
2 => substr($shortVersion, 0, strrpos($shortVersion, '.')),
109-
default => implode('.', array_chunk(explode('.', $shortVersion), 2)[0])
110-
};
101+
parent::__construct($name, $versionName, $semanticVersion);
111102

112-
$this->_fhirVersion = new <?php echo $fhirVersion; ?>(
113-
$versionName,
114-
$semanticVersion,
115-
$shortVersion,
116-
intval(sprintf("%'.-08s", str_replace(['v', '.'], '', $semanticVersion))),
117-
);
103+
$this->_setFHIRComments($fhirComments);
118104

119105
$fields['id'] = [
120106
'class' => <?php echo $mockResourceIDClass; ?>::class,
@@ -131,16 +117,6 @@ public function __construct(string $name,
131117
$this->_processFields($fields);
132118
}
133119

134-
public function _getFHIRTypeName(): string
135-
{
136-
return $this->_name;
137-
}
138-
139-
public function _getFHIRVersion(): <?php echo $fhirVersion; ?>
140-
{
141-
return $this->_fhirVersion;
142-
}
143-
144120
public function getId(): null|<?php echo $resourceIDTypeInterface; ?>
145121

146122
{
@@ -155,7 +131,8 @@ public static function xmlUnserialize(\SimpleXMLElement|string $element,
155131
throw new \BadMethodCallException('xmlUnserialize not yet implemented');
156132
}
157133

158-
public function xmlSerialize(null|<?php echo $xmlWriterClass; ?> $xw = null, null|<?php echo $serializeConfig; ?> $config = null): <?php echo $xmlWriterClass; ?>
134+
public function xmlSerialize(null|<?php echo $xmlWriterClass; ?> $xw = null,
135+
null|<?php echo $serializeConfig; ?> $config = null): <?php echo $xmlWriterClass; ?>
159136

160137
{
161138
if (null === $config) {
@@ -187,7 +164,9 @@ public function xmlSerialize(null|<?php echo $xmlWriterClass; ?> $xw = null, nul
187164
return $xw;
188165
}
189166

190-
public static function jsonUnserialize(string|\stdClass $json, null|<?php echo $unserializeConfig; ?> $config = null, null|<?php echo $resourceTypeInterface; ?> $type = null): <?php echo $resourceTypeInterface; ?>
167+
public static function jsonUnserialize(string|\stdClass $json,
168+
null|<?php echo $unserializeConfig; ?> $config = null,
169+
null|<?php echo $resourceTypeInterface; ?> $type = null): <?php echo $resourceTypeInterface; ?>
191170

192171
{
193172
throw new \BadMethodCallException('jsonUnserialize not yet implemented');

0 commit comments

Comments
 (0)