Skip to content

Commit

Permalink
halfway fix for xhtml, v5 will require better templating and typing s…
Browse files Browse the repository at this point in the history
…ytsem
  • Loading branch information
dcarbone committed Jan 23, 2025
1 parent 56313c9 commit 2979ac3
Show file tree
Hide file tree
Showing 23 changed files with 136 additions and 151 deletions.
14 changes: 8 additions & 6 deletions files/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@

// raw type
const PHPFHIR_XHTML_TYPE_NAME = 'XHTML';
const PHPFHIR_XHTML_TYPE_DESCRIPTION = 'XHTML type used in special cases';

// FHIR XML NS
const PHPFHIR_FHIR_XMLNS = 'https://hl7.org/fhir';
Expand Down Expand Up @@ -92,16 +91,11 @@
const PHPFHIR_CLASSNAME_VALIDATOR = 'Validator';

// Core interface names
const PHPFHIR_INTERFACE_TYPE = 'TypeInterface';
const PHPFHIR_INTERFACE_CONTAINED_TYPE = 'ContainedTypeInterface';
const PHPFHIR_INTERFACE_COMMENT_CONTAINER = 'CommentContainerInterface';
const PHPFHIR_INTERFACE_PRIMITIVE_TYPE = 'PrimitiveTypeInterface';
const PHPFHIR_INTERFACE_VERSION = 'VersionInterface';
const PHPFHIR_INTERFACE_VERSION_CONFIG = 'VersionConfigInterface';
const PHPFHIR_INTERFACE_VERSION_TYPE_MAP = 'VersionTypeMapInterface';

// Core traits
const PHPFHIR_TRAIT_COMMENT_CONTAINER = 'CommentContainerTrait';
const PHPFHIR_TRAIT_SOURCE_XMLNS = 'SourceXMLNamespaceTrait';

// Core enums
Expand All @@ -112,6 +106,14 @@
const PHPFHIR_EXCEPTION_CLIENT_ERROR = 'ClientErrorException';
const PHPFHIR_EXCEPTION_CLIENT_UNEXPECTED_RESPONSE_CODE = 'UnexpectedResponseCodeException';

// Core types entities
const PHPFHIR_TYPES_INTERFACE_TYPE = 'TypeInterface';
const PHPFHIR_TYPES_INTERFACE_CONTAINED_TYPE = 'ContainedTypeInterface';
const PHPFHIR_TYPES_INTERFACE_COMMENT_CONTAINER = 'CommentContainerInterface';
const PHPFHIR_TYPES_INTERFACE_PRIMITIVE_TYPE = 'PrimitiveTypeInterface';
const PHPFHIR_TYPES_TRAIT_COMMENT_CONTAINER = 'CommentContainerTrait';


// Core encoding entities
const PHPFHIR_ENCODING_CLASSNAME_XML_WRITER = 'XMLWriter';
const PHPFHIR_ENCODING_ENUM_VALUE_XML_LOCATION = 'ValueXMLLocationEnum';
Expand Down
7 changes: 6 additions & 1 deletion src/Utilities/ImportUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public static function buildVersionTypeImports(Version $version, Type $type): vo
// immediately add self
$imports->addImport($type->getFullyQualifiedNamespace(false), $type->getClassName());

// xhtml type has too many special cases, do better.
if ($type->getFHIRName() === PHPFHIR_XHTML_TYPE_NAME) {
return;
}

$typeKind = $type->getKind();

if (!$type->isAbstract()) {
Expand All @@ -54,7 +59,7 @@ public static function buildVersionTypeImports(Version $version, Type $type): vo
PHPFHIR_ENCODING_ENUM_VALUE_XML_LOCATION,
PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG,
PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG,
PHPFHIR_INTERFACE_TYPE,
PHPFHIR_TYPES_INTERFACE_TYPE,
);
}

Expand Down
27 changes: 17 additions & 10 deletions src/Utilities/TypeHintUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,14 @@ public static function buildBaseHintParts(Version $version, Type $type, bool $fu
// fetch type's kind
$tk = $type->getKind();

if ($tk->isOneOf(TypeKindEnum::PRIMITIVE, TypeKindEnum::LIST)) {
if ($type->isPrimitiveOrListType()) {
$hintTypes = $type->getPrimitiveType()->getPHPReceiveValueTypeHints();
} else if ($tk === TypeKindEnum::PRIMITIVE_CONTAINER) {
} else if ($type->isPrimitiveContainer()) {
$ptp = $type->getProperties()->getProperty(PHPFHIR_VALUE_PROPERTY_NAME)->getValueFHIRType();
$hintTypes = $ptp->getPrimitiveType()->getPHPReceiveValueTypeHints();
array_merge($hintTypes, self::buildBaseHintParts($version, $ptp, $fullyQualified));
} else if ($tk->isResourceContainer($version)) {
$containerType = $version->getDefinition()->getTypes()->getContainerType();

$hintTypes = match ($fullyQualified) {
true => [
$containerType->getFullyQualifiedClassName(true),
Expand All @@ -143,6 +142,16 @@ public static function buildBaseHintParts(Version $version, Type $type, bool $fu
PHPFHIR_VERSION_INTERFACE_VERSION_CONTAINED_TYPE,
],
};
} else if ($type->getKind() === TypeKindEnum::PHPFHIR_XHTML) {
$hintTypes = [
'string',
'\\SimpleXMLElement',
'\\DOMNode',
match ($fullyQualified) {
true => $type->getFullyQualifiedClassName(true),
false => $type->getClassName(),
},
];
} else {
$hintTypes = [
match ($fullyQualified) {
Expand Down Expand Up @@ -232,6 +241,7 @@ public static function propertyGetterDocHint(Version $version,
* @param bool $nullable
* @param bool $ignoreCollection
* @return string
* @throws \Exception
*/
public static function buildSetterParameterDocHint(Version $version,
Property $property,
Expand All @@ -250,13 +260,12 @@ public static function buildSetterParameterDocHint(Version $version,

$hintTypes = self::buildBaseHintParts($version, $pt, true);

$ptk = $pt->getKind();

if ($property->isValueProperty() || $ptk->isOneOf(TypeKindEnum::LIST, TypeKindEnum::PRIMITIVE)) {
if ($property->isValueProperty() || $pt->isPrimitiveOrListType()) {
$hintTypes[] = $pt->getFullyQualifiedClassName(true);
}

if ($ptk === TypeKindEnum::PRIMITIVE_CONTAINER) {
if ($pt->isPrimitiveContainer()) {
$vp = $pt->getProperties()->getProperty(PHPFHIR_VALUE_PROPERTY_NAME);
array_push(
$hintTypes,
Expand Down Expand Up @@ -300,13 +309,11 @@ public static function buildSetterParameterHint(Version $version,
} else {
$hintTypes = self::buildBaseHintParts($version, $pt, false);

$ptk = $pt->getKind();

if ($property->isValueProperty() || $ptk->isOneOf(TypeKindEnum::LIST, TypeKindEnum::PRIMITIVE)) {
if ($property->isValueProperty() || $pt->isPrimitiveOrListType()) {
$hintTypes[] = $pt->getClassName();
}

if ($ptk === TypeKindEnum::PRIMITIVE_CONTAINER) {
if ($pt->isPrimitiveContainer()) {
$vp = $pt->getProperties()->getProperty(PHPFHIR_VALUE_PROPERTY_NAME);
array_push(
$hintTypes,
Expand Down
16 changes: 8 additions & 8 deletions src/Version/Definition/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -823,21 +823,21 @@ public function getDirectlyImplementedInterfaces(): array

if (null === $parentType) {
if ($this->isCommentContainer()) {
$interfaces[PHPFHIR_INTERFACE_COMMENT_CONTAINER] = $coreFiles
->getCoreFileByEntityName(PHPFHIR_INTERFACE_COMMENT_CONTAINER)
$interfaces[PHPFHIR_TYPES_INTERFACE_COMMENT_CONTAINER] = $coreFiles
->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_COMMENT_CONTAINER)
->getFullyQualifiedNamespace(false);
}
if ($this->isContainedType()) {
$interfaces[PHPFHIR_VERSION_INTERFACE_VERSION_CONTAINED_TYPE] = $versionCoreFiles
->getCoreFileByEntityName(PHPFHIR_VERSION_INTERFACE_VERSION_CONTAINED_TYPE)
->getFullyQualifiedNamespace(false);
} else if ($this->getKind() === TypeKindEnum::PRIMITIVE) {
$interfaces[PHPFHIR_INTERFACE_PRIMITIVE_TYPE] = $coreFiles
->getCoreFileByEntityName(PHPFHIR_INTERFACE_PRIMITIVE_TYPE)
$interfaces[PHPFHIR_TYPES_INTERFACE_PRIMITIVE_TYPE] = $coreFiles
->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_PRIMITIVE_TYPE)
->getFullyQualifiedNamespace(false);
} else {
$interfaces[PHPFHIR_INTERFACE_TYPE] = $coreFiles
->getCoreFileByEntityName(PHPFHIR_INTERFACE_TYPE)
$interfaces[PHPFHIR_TYPES_INTERFACE_TYPE] = $coreFiles
->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_TYPE)
->getFullyQualifiedNamespace(false);
}
} elseif ($this->isContainedType() && !$parentType->isContainedType()) {
Expand All @@ -862,8 +862,8 @@ public function getDirectlyUsedTraits(): array
// if this type has no parent(s), try to add all traits

if ($this->isCommentContainer()) {
$traits[PHPFHIR_TRAIT_COMMENT_CONTAINER] = $coreFiles
->getCoreFileByEntityName(PHPFHIR_TRAIT_COMMENT_CONTAINER)
$traits[PHPFHIR_TYPES_TRAIT_COMMENT_CONTAINER] = $coreFiles
->getCoreFileByEntityName(PHPFHIR_TYPES_TRAIT_COMMENT_CONTAINER)
->getFullyQualifiedNamespace(false);
}

Expand Down
2 changes: 0 additions & 2 deletions src/Version/Definition/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ public function __construct(Version $version)
// TODO(dcarbone): this sucks.
$xt = new Type($version, PHPFHIR_XHTML_TYPE_NAME);
$xt->setKind(TypeKindEnum::PHPFHIR_XHTML);
$xt->addDocumentationFragment(PHPFHIR_XHTML_TYPE_DESCRIPTION);

$this->addType($xt);
}

Expand Down
35 changes: 24 additions & 11 deletions template/core/class_response_parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,24 @@
* limitations under the License.
*/

use DCarbone\PHPFHIR\Utilities\ImportUtils;

/** @var \DCarbone\PHPFHIR\Config $config */
/** @var \DCarbone\PHPFHIR\CoreFile $coreFile */

$coreFiles = $config->getCoreFiles();

$versionInterface = $coreFiles->getCoreFileByEntityName(PHPFHIR_INTERFACE_VERSION);
$typeInterface = $coreFiles->getCoreFileByEntityName(PHPFHIR_INTERFACE_TYPE);
$typeInterface = $coreFiles->getCoreFileByEntityName(PHPFHIR_TYPES_INTERFACE_TYPE);
$constantsClass = $coreFiles->getCoreFileByEntityName(PHPFHIR_CLASSNAME_CONSTANTS);

$imports = $coreFile->getImports();

$imports->addCoreFileImports(
$versionInterface,
$typeInterface,
$constantsClass,
);

ob_start();
echo '<?php ';?>declare(strict_types=1);
Expand All @@ -31,6 +42,8 @@

<?php echo $config->getBasePHPFHIRCopyrightComment(true); ?>

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

class <?php echo PHPFHIR_CLASSNAME_RESPONSE_PARSER; ?>

{
Expand All @@ -47,7 +60,7 @@ class <?php echo PHPFHIR_CLASSNAME_RESPONSE_PARSER; ?>
* @throws \Exception
*/
public static function parse(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $version,
null|string|array|\stdClass|\SimpleXMLElement|\DOMDocument $input): null|<?php echo PHPFHIR_INTERFACE_TYPE; ?>
null|string|array|\stdClass|\SimpleXMLElement|\DOMDocument $input): null|<?php echo PHPFHIR_TYPES_INTERFACE_TYPE; ?>

{
if (null === $input) {
Expand All @@ -68,7 +81,7 @@ public static function parse(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $version,

*/
public static function parseArray(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $version,
array $input): null|<?php echo PHPFHIR_INTERFACE_TYPE; ?>
array $input): null|<?php echo PHPFHIR_TYPES_INTERFACE_TYPE; ?>

{
if ([] === $input) {
Expand Down Expand Up @@ -100,7 +113,7 @@ public static function parseArray(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $vers

*/
public static function parseStdClass(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $version,
\stdClass $input): null|<?php echo PHPFHIR_INTERFACE_TYPE; ?>
\stdClass $input): null|<?php echo PHPFHIR_TYPES_INTERFACE_TYPE; ?>

{
return static::parseArray($version, (array)$input);
Expand All @@ -113,11 +126,11 @@ public static function parseStdClass(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $v

*/
public static function parseSimpleXMLElement(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $version,
\SimpleXMLElement $input): null|<?php echo PHPFHIR_INTERFACE_TYPE; ?>
\SimpleXMLElement $input): null|<?php echo PHPFHIR_TYPES_INTERFACE_TYPE; ?>

{
$elementName = $input->getName();
/** @var <?php echo $config->getFullyQualifiedName(true, PHPFHIR_INTERFACE_TYPE); ?> $fhirType */
/** @var <?php echo $config->getFullyQualifiedName(true, PHPFHIR_TYPES_INTERFACE_TYPE); ?> $fhirType */
$fhirType = $version->getTypeMap()::getTypeClassName($elementName);
if (null === $fhirType) {
throw new \UnexpectedValueException(sprintf(
Expand All @@ -136,7 +149,7 @@ public static function parseSimpleXMLElement(<?php echo PHPFHIR_INTERFACE_VERSIO

*/
public static function parseDOMDocument(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $version,
\DOMDocument $input): null|<?php echo PHPFHIR_INTERFACE_TYPE; ?>
\DOMDocument $input): null|<?php echo PHPFHIR_TYPES_INTERFACE_TYPE; ?>

{
return static::parseSimpleXMLElement($version, simplexml_import_dom($input));
Expand All @@ -149,7 +162,7 @@ public static function parseDOMDocument(<?php echo PHPFHIR_INTERFACE_VERSION; ?>

*/
public static function parseObject(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $version,
\stdClass|\SimpleXMLElement|\DOMDocument $input): null|<?php echo PHPFHIR_INTERFACE_TYPE; ?>
\stdClass|\SimpleXMLElement|\DOMDocument $input): null|<?php echo PHPFHIR_TYPES_INTERFACE_TYPE; ?>

{
if ($input instanceof \stdClass) {
Expand All @@ -169,7 +182,7 @@ public static function parseObject(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $ver
* @throws \Exception
*/
public static function parseXML(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $version,
string $input): null|<?php echo PHPFHIR_INTERFACE_TYPE; ?>
string $input): null|<?php echo PHPFHIR_TYPES_INTERFACE_TYPE; ?>

{
return static::parseSimpleXMLElement(
Expand All @@ -184,7 +197,7 @@ public static function parseXML(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $versio

*/
public static function parseJSON(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $version,
string $input): null|<?php echo PHPFHIR_INTERFACE_TYPE; ?>
string $input): null|<?php echo PHPFHIR_TYPES_INTERFACE_TYPE; ?>

{
$decoded = json_decode($input, true, $version->getConfig()->getUnserializeConfig()->getJSONDecodeMaxDepth());
Expand All @@ -208,7 +221,7 @@ public static function parseJSON(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $versi
* @throws \Exception
*/
public static function parseString(<?php echo PHPFHIR_INTERFACE_VERSION; ?> $version,
string $input): null|<?php echo PHPFHIR_INTERFACE_TYPE; ?>
string $input): null|<?php echo PHPFHIR_TYPES_INTERFACE_TYPE; ?>

{
$input = trim($input);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

<?php echo $config->getBasePHPFHIRCopyrightComment(true); ?>

interface <?php echo PHPFHIR_INTERFACE_COMMENT_CONTAINER; ?>
interface <?php echo PHPFHIR_TYPES_INTERFACE_COMMENT_CONTAINER; ?>

{
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
<?php echo $config->getBasePHPFHIRCopyrightComment(true); ?>

/**
* Interface <?php echo PHPFHIR_INTERFACE_CONTAINED_TYPE; ?>
* Interface <?php echo PHPFHIR_TYPES_INTERFACE_CONTAINED_TYPE; ?>

*
* This is a meta interface that must never be directly implemented by a class. It exists purely to ensure type safety
* throughout the base package.
*/
interface <?php echo PHPFHIR_INTERFACE_CONTAINED_TYPE; ?> extends <?php echo PHPFHIR_INTERFACE_TYPE; ?>
interface <?php echo PHPFHIR_TYPES_INTERFACE_CONTAINED_TYPE; ?> extends <?php echo PHPFHIR_TYPES_INTERFACE_TYPE; ?>

{
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

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

interface <?php echo PHPFHIR_INTERFACE_PRIMITIVE_TYPE; ?> extends \JsonSerializable
interface <?php echo PHPFHIR_TYPES_INTERFACE_PRIMITIVE_TYPE; ?> extends \JsonSerializable
{
/**
* Returns the FHIR name represented by this Type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

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

interface <?php echo PHPFHIR_INTERFACE_TYPE; ?> extends \JsonSerializable
interface <?php echo PHPFHIR_TYPES_INTERFACE_TYPE; ?> extends \JsonSerializable
{
/**
* Returns the FHIR name represented by this Type
Expand Down Expand Up @@ -84,7 +84,7 @@ public function _getValidationErrors(): array;
* @return static
*/
public static function xmlUnserialize(string|\SimpleXMLElement $element,
null|<?php echo PHPFHIR_INTERFACE_TYPE; ?> $type = null,
null|<?php echo PHPFHIR_TYPES_INTERFACE_TYPE; ?> $type = null,
null|<?php echo PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG ?> $config = null): self;

/**
Expand All @@ -103,7 +103,7 @@ public function xmlSerialize(null|<?php echo PHPFHIR_ENCODING_CLASSNAME_XML_WRIT
* @return static
*/
public static function jsonUnserialize(string|array|\stdClass $json,
null|<?php echo PHPFHIR_INTERFACE_TYPE; ?> $type = null,
null|<?php echo PHPFHIR_TYPES_INTERFACE_TYPE; ?> $type = null,
null|<?php echo PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG; ?> $config = null): self;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

<?php echo $config->getBasePHPFHIRCopyrightComment(true); ?>

trait <?php echo PHPFHIR_TRAIT_COMMENT_CONTAINER; ?>
trait <?php echo PHPFHIR_TYPES_TRAIT_COMMENT_CONTAINER; ?>

{
/** @var array */
Expand Down
Loading

0 comments on commit 2979ac3

Please sign in to comment.