Skip to content

Commit 8a9ce48

Browse files
authored
Merge pull request #157 from dcarbone/feature/version-describing-types
Version-aware resource types and TypeMap improvements
2 parents 4db6d5c + 93ff0c1 commit 8a9ce48

File tree

19 files changed

+347
-158
lines changed

19 files changed

+347
-158
lines changed

src/Utilities/ImportUtils.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static function buildPropertyValidationRuleImports(Type $type, Property $
4343
{
4444
$imports = $type->getImports();
4545

46-
foreach($property->buildValidationMap($type) as $ruleClass => $_) {
46+
foreach ($property->buildValidationMap($type) as $ruleClass => $_) {
4747
$imports->addCoreFileImportsByName($ruleClass);
4848
}
4949
}
@@ -190,7 +190,13 @@ public static function buildVersionTypeImports(Version $version, Type $type): vo
190190
}
191191

192192
if ($sourceMeta->isDSTU1()) {
193-
$imports->addCoreFileImportsByName(PHPFHIR_TYPES_INTERFACE_RESOURCE_TYPE);
193+
$imports->addCoreFileImportsByName(
194+
PHPFHIR_TYPES_INTERFACE_RESOURCE_TYPE,
195+
);
196+
$imports->addVersionCoreFileImportsByName(
197+
$version,
198+
PHPFHIR_VERSION_CLASSNAME_VERSION,
199+
);
194200
}
195201

196202
if ($type->getKind()->isResourceContainer($type->getVersion())) {

src/Version/SourceMetadata.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,29 +157,45 @@ public function getFullPHPFHIRCopyrightComment(): string
157157
/**
158158
* @return string
159159
*/
160-
public function getFHIRGenerationDate(): string
160+
public function getSourceGenerationDate(): string
161161
{
162162
$this->compile();
163163
return $this->_fhirGenerationDate;
164164
}
165165

166166
/**
167-
* @param bool $trimmed
167+
* @param bool $trimmed If true, trims off the 'v' prefix before returning.
168168
* @return string
169169
*/
170-
public function getFHIRVersionString(bool $trimmed): string
170+
public function getSemanticVersion(bool $trimmed): string
171171
{
172172
$this->compile();
173173
return $trimmed ? trim($this->_fhirVersion, 'v') : $this->_fhirVersion;
174174
}
175175

176+
/**
177+
* Return the shortenend representation of the FHIR semantic version containing only Manjor.Minor values.
178+
*
179+
* @return string
180+
*/
181+
public function getShortVersion(): string
182+
{
183+
$this->compile();
184+
$v = $this->getsemanticVersion(false);
185+
return match (substr_count($v, '.')) {
186+
1 => $v,
187+
2 => substr($v, 0, strrpos($v, '.')),
188+
default => implode('.', array_chunk(explode('.', $v), 2)[0])
189+
};
190+
}
191+
176192
/**
177193
* Returns true if the upstream source was generated from, or is based on, DSTU1.
178194
*
179195
* @return bool
180196
*/
181197
public function isDSTU1(): bool
182198
{
183-
return Semver::satisfies($this->getFHIRVersionString(false), '<= ' . self::_DSTU1_VERSION_MAX);
199+
return Semver::satisfies($this->getSemanticVersion(false), '<= ' . self::_DSTU1_VERSION_MAX);
184200
}
185201
}

template/core/client/class_client.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,12 @@ public function exec(<?php echo $clientRequestClass; ?> $request): <?php echo $c
150150
$rc->err = $err;
151151
$rc->errno = $errno;
152152

153-
if ($parseResponseHeaders) {
154-
$rc->resp = substr($resp, $rc->headers->getLength());
155-
} else {
156-
$rc->resp = $resp;
153+
if (0 === $errno) {
154+
if ($parseResponseHeaders) {
155+
$rc->resp = substr($resp, $rc->headers->getLength());
156+
} else {
157+
$rc->resp = $resp;
158+
}
157159
}
158160

159161
return $rc;

template/core/encoding/class_resource_parser.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public static function parseArray(<?php echo $versionInterface; ?> $version, arr
8888
}
8989
if (isset($input[<?php echo $constantsClass; ?>::JSON_FIELD_RESOURCE_TYPE])) {
9090
/** @var <?php echo $resourceTypeInterface->getFullyQualifiedName(true); ?> $className */
91-
$className = $version->getTypeMap()::getTypeClassName($input[<?php echo $constantsClass; ?>::JSON_FIELD_RESOURCE_TYPE]);
91+
$className = $version->getTypeMap()::getTypeClassname($input[<?php echo $constantsClass; ?>::JSON_FIELD_RESOURCE_TYPE]);
9292
if (null === $className) {
9393
throw new \UnexpectedValueException(sprintf(
9494
'Provided input has "%s" value of "%s", but it does not map to any known type. Other keys: ["%s"]',
@@ -129,7 +129,7 @@ public static function parseSimpleXMLElement(<?php echo $versionInterface; ?> $v
129129
{
130130
$elementName = $input->getName();
131131
/** @var <?php echo $resourceTypeInterface->getFullyQualifiedName(true); ?> $fhirType */
132-
$fhirType = $version->getTypeMap()::getTypeClassName($elementName);
132+
$fhirType = $version->getTypeMap()::getTypeClassname($elementName);
133133
if (null === $fhirType) {
134134
throw new \UnexpectedValueException(sprintf(
135135
'Unable to locate FHIR type for root XML element "%s". Input seen: %s',

template/core/types/interface_resource_type.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,27 @@
4444
interface <?php echo $coreFile; ?> extends <?php echo $typeInterface; ?>
4545

4646
{
47+
/**
48+
* Returns the name of the version this type was generated from.
49+
*
50+
* @return string
51+
*/
52+
public function _getFHIRVersionName(): string;
53+
54+
/**
55+
* Returns the semver of the version of FHIR this type was generated from.
56+
*
57+
* @return string
58+
*/
59+
public function _getFHIRSemanticVersion(): string;
60+
61+
/**
62+
* Returns the shortened Major.Minor representation of the FHIR semantic version this type was generated from.
63+
*
64+
* @return string
65+
*/
66+
public function _getFHIRShortVersion(): string;
67+
4768
/**
4869
* Returns the root XMLNS value found in the source. Null indicates no "xmlns" was found. Only defined when
4970
* unserializing XML, and only used when serializing XML.

template/core/versions/interface_version.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,21 @@ public function getName(): string;
5757
*
5858
* @return string
5959
*/
60-
public function getSourceVersion(): string;
60+
public function getFHIRSemanticVersion(): string;
61+
62+
/*
63+
* Must return the shortened Major.Minor representation of the source's semantic version.
64+
*
65+
* @return string
66+
*/
67+
public function getFHIRShortVersion(): string;
6168

6269
/**
6370
* Must return the date this FHIR version's source was generated
6471
*
6572
* @return string
6673
*/
67-
public function getSourceGenerationDate(): string;
74+
public function getFHIRGenerationDate(): string;
6875

6976
/**
7077
* Must return config for this version

template/core/versions/interface_version_type_map.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,6 @@
4343
interface <?php echo PHPFHIR_INTERFACE_VERSION_TYPE_MAP; ?>
4444

4545
{
46-
/**
47-
* Must return the fully qualified class name for FHIR Type name. Must return null if type not found.
48-
*
49-
* @param string $typeName
50-
* @return string|null
51-
*/
52-
public static function getTypeClassName(string $typeName): null|string;
53-
5446
/**
5547
* Must return the full internal class map
5648
*
@@ -66,30 +58,40 @@ public static function getMap(): array;
6658
public static function getContainableTypes(): array;
6759

6860
/**
69-
* @param string $typeName Name of FHIR object reference by a version's container type
61+
* Must return the fully qualified class name for FHIR Type name. Must return null if type not found.
62+
*
63+
* @param string|\stdClass|\SimpleXMLElement $input Must expect either name of type, or unserialized JSON or XML.
64+
* @return string|null
65+
*/
66+
public static function getTypeClassname(string|\stdClass|\SimpleXMLElement $input): null|string;
67+
68+
/**
69+
* Must attempt to return the fully qualified classname of a contained type from the provided input, if it
70+
* if it represents one.
71+
*
72+
* @param string|\stdClass|\SimpleXMLElement $input Expects either name of type or unserialized JSON or XML.
7073
* @return string|null Name of class as string or null if type is not contained in map
7174
*/
72-
public static function getContainedTypeClassName(string $typeName): null|string;
75+
public static function getContainedTypeClassname(string|\stdClass|\SimpleXMLElement $input): null|string;
7376

7477
/**
7578
* Must attempt to determine if the provided value is or describes a containable resource type
7679
*
77-
* @param string|array|\SimpleXMLElement|<?php echo $typeInterface->getFullyQualifiedName(true); ?> $type
80+
* @param string|\stdClass|\SimpleXMLElement|<?php echo $typeInterface->getFullyQualifiedName(true); ?> $input
7881
* @return bool
79-
* @throws \InvalidArgumentException
8082
*/
81-
public static function isContainableResource(string|array|\SimpleXMLElement|<?php echo PHPFHIR_TYPES_INTERFACE_TYPE; ?> $type): bool;
83+
public static function isContainableType(string|\stdClass|\SimpleXMLElement|TypeInterface $input): bool;
8284

8385
/**
8486
* @param \SimpleXMLElement $node Parent element containing inline resource
8587
* @return string Fully qualified class name of contained resource type
8688
*/
87-
public static function getContainedTypeClassNameFromXML(\SimpleXMLElement $node): string;
89+
public static function mustGetContainedTypeClassnameFromXML(\SimpleXMLElement $node): string;
8890

8991
/**
9092
* @param \stdClass $json
9193
* @return string Fully qualified class name of contained resource type
9294
*/
93-
public static function getContainedTypeClassNameFromJSON(\stdClass $json): string;
95+
public static function mustGetContainedTypeClassnameFromJSON(\stdClass $json): string;
9496
}
9597
<?php return ob_get_clean();

template/tests/core/types/class_mock_resource_type.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,21 @@ class <?php echo $coreFile; ?> implements <?php echo $resourceTypeInterface; ?>,
7979
private const _FHIR_VALIDATION_RULES = [];
8080

8181
protected string $_name;
82+
protected string $_versionName;
83+
protected string $_semanticVersion;
8284

8385
private array $_valueXMLLocations = [];
8486

8587
public function __construct(string $name,
8688
array $fields = [],
8789
array $validationRuleMap = [],
88-
array $fhirComments = [])
90+
array $fhirComments = [],
91+
string $versionName = 'mock',
92+
string $semanticVersion = 'v0.0.0')
8993
{
9094
$this->_name = $name;
95+
$this->_versionName = $versionName;
96+
$this->_semanticVersion = $semanticVersion;
9197
$this->_setFHIRComments($fhirComments);
9298
foreach($validationRuleMap as $field => $rules) {
9399
$this->_setFieldValidationRules($field, $rules);
@@ -100,6 +106,26 @@ public function _getFHIRTypeName(): string
100106
return $this->_name;
101107
}
102108

109+
public function _getFHIRVersionName(): string
110+
{
111+
return $this->_versionName;
112+
}
113+
114+
public function _getFHIRSemanticVersion(): string
115+
{
116+
return $this->_semanticVersion;
117+
}
118+
119+
public function _getFHIRShortVersion(): string
120+
{
121+
$v = ltrim($this->_semanticVersion, 'v');
122+
return match (substr_count($v, '.')) {
123+
1 => $v,
124+
2 => substr($v, 0, strrpos($v, '.')),
125+
default => implode('.', array_chunk(explode('.', $v), 2)[0])
126+
};
127+
}
128+
103129
public static function xmlUnserialize(\SimpleXMLElement|string $element,
104130
null|<?php echo $unserializeConfig; ?> $config = null,
105131
null|<?php echo $resourceTypeInterface; ?> $type = null): <?php echo $resourceTypeInterface; ?>

0 commit comments

Comments
 (0)