Skip to content

Commit

Permalink
more cleanup of root namespace, re-thinking xml value location implem…
Browse files Browse the repository at this point in the history
…entation
  • Loading branch information
dcarbone committed Jan 17, 2025
1 parent 6b4b748 commit 8aab701
Show file tree
Hide file tree
Showing 31 changed files with 308 additions and 275 deletions.
4 changes: 2 additions & 2 deletions files/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@

// Core encoding entities
const PHPFHIR_ENCODING_CLASSNAME_XML_WRITER = 'XMLWriter';
const PHPFHIR_ENCODING_ENUM_XML_LOCATION = 'XMLLocationEnum';
const PHPFHIR_ENCODING_TRAIT_XML_LOCATION = 'XMLLocationTrait';
const PHPFHIR_ENCODING_ENUM_VALUE_XML_LOCATION = 'ValueXMLLocationEnum';
const PHPFHIR_ENCODING_TRAIT_VALUE_XML_LOCATION = 'ValueXMLLocationTrait';
const PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG = 'SerializeConfig';
const PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG = 'UnserializeConfig';

Expand Down
6 changes: 3 additions & 3 deletions src/Utilities/ImportUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static function buildVersionTypeImports(Version $version, Type $type): vo
if (!$type->isAbstract()) {
$imports->addCoreFileImportsByName(
PHPFHIR_ENCODING_CLASSNAME_XML_WRITER,
PHPFHIR_ENCODING_ENUM_XML_LOCATION,
PHPFHIR_ENCODING_ENUM_VALUE_XML_LOCATION,
PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG,
PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG,
PHPFHIR_INTERFACE_TYPE,
Expand All @@ -74,7 +74,7 @@ public static function buildVersionTypeImports(Version $version, Type $type): vo

if (($type->isCommentContainer() && !$type->hasCommentContainerParent()) ||
$type->hasPropertiesWithValidations() ||
($typeKind->isOneOf(TypeKindEnum::PRIMITIVE) && !$type->hasPrimitiveParent())) {
($typeKind->isOneOf(TypeKindEnum::PRIMITIVE) && !$type->hasPrimitiveOrListParent())) {
$imports->addCoreFileImportsByName(PHPFHIR_CLASSNAME_CONSTANTS);
}

Expand All @@ -101,7 +101,7 @@ public static function buildVersionTypeImports(Version $version, Type $type): vo
|| $type->hasPrimitiveContainerParent()
|| $typeKind->isOneOf(TypeKindEnum::PRIMITIVE, TypeKindEnum::LIST, TypeKindEnum::PRIMITIVE_CONTAINER)) {
if (!$type->hasValueContainerParent() && !$type->hasPrimitiveContainerParent()) {
$imports->addCoreFileImportsByName(PHPFHIR_ENCODING_TRAIT_XML_LOCATION);
$imports->addCoreFileImportsByName(PHPFHIR_ENCODING_TRAIT_VALUE_XML_LOCATION);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Utilities/TypeHintUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public static function propertyGetterDocHint(Version $version,
return self::primitivePHPValueTypeHint(
$version,
$property->getMemberOf()->getPrimitiveType(),
!$property->isCollection(),
$nullable && !$property->isCollection(),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Version/Definition/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ public function getOverloadedProperty(): null|Property
*/
public function requiresXMLLocation(): bool
{
return $this->_memberOf->hasPrimitiveParent()
return $this->_memberOf->hasPrimitiveOrListParent()
|| $this->_memberOf->getKind()->isOneOf(
TypeKindEnum::PRIMITIVE_CONTAINER,
TypeKindEnum::PRIMITIVE,
Expand Down
30 changes: 22 additions & 8 deletions src/Version/Definition/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,30 +506,45 @@ public function hasParent(): bool
/**
* @return bool
*/
public function hasPrimitiveParent(): bool
public function isPrimitiveOrListType(): bool
{
return $this->getKind()->isOneOf(TypeKindEnum::PRIMITIVE, TypeKindEnum::LIST);
}

/**
* @return bool
*/
public function hasPrimitiveOrListParent(): bool
{
foreach ($this->getParentTypes() as $parentType) {
if ($parentType->getKind() === TypeKindEnum::PRIMITIVE) {
if ($parentType->isPrimitiveOrListType()) {
return true;
}
}
return false;
}

public function isPrimitiveContainer(): bool
{
return $this->getKind() === TypeKindEnum::PRIMITIVE_CONTAINER;
}

/**
* @return bool
*/
public function hasPrimitiveContainerParent(): bool
{
foreach ($this->getParentTypes() as $parentType) {
if ($parentType->getKind() === TypeKindEnum::PRIMITIVE_CONTAINER) {
if ($parentType->isPrimitiveContainer()) {
return true;
}
}
return false;
}

/**
* TODO: this is a really poor way to implement this...
*
* @return bool
*/
public function isResourceType(): bool
Expand Down Expand Up @@ -845,11 +860,10 @@ public function getDirectlyUsedTraits(): array
->getFullyQualifiedNamespace(false);
}

if (($this->isValueContainer()
|| $this->getKind()->isOneOf(TypeKindEnum::LIST, TypeKindEnum::PRIMITIVE, TypeKindEnum::PRIMITIVE_CONTAINER))
&& !($this->hasValueContainerParent() || $this->hasPrimitiveContainerParent() || $this->hasPrimitiveParent())) {
$traits[PHPFHIR_ENCODING_TRAIT_XML_LOCATION] = $coreFiles
->getCoreFileByEntityName(PHPFHIR_ENCODING_TRAIT_XML_LOCATION)
if (($this->isValueContainer() || $this->isPrimitiveOrListType() || $this->isPrimitiveContainer())
&& !($this->hasValueContainerParent() || $this->hasPrimitiveContainerParent() || $this->hasPrimitiveOrListParent())) {
$traits[PHPFHIR_ENCODING_TRAIT_VALUE_XML_LOCATION] = $coreFiles
->getCoreFileByEntityName(PHPFHIR_ENCODING_TRAIT_VALUE_XML_LOCATION)
->getFullyQualifiedNamespace(false);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Version/Definition/TypeDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public static function determinePrimitiveTypes(Config $config, Types $types): vo
if (in_array($type->getFHIRName(), self::DSTU1_PRIMITIVES, true)) {
$ptn = PrimitiveTypeEnum::STRING->value;
$logger->debug(sprintf('(DSTU1 suppport) Type "%s" determined to be DSTU1 primitive', $type->getFHIRName()));
} elseif ($type->hasPrimitiveParent()) {
} elseif ($type->hasPrimitiveOrListParent()) {
$ptn = $type->getParentType()->getFHIRName();
$logger->debug(sprintf('Type "%s" determined to have a primitive parent', $type->getFHIRName()));
} elseif ($type->getKind() === TypeKindEnum::PRIMITIVE) {
Expand Down Expand Up @@ -397,7 +397,7 @@ public static function setValueContainerFlag(Config $config, Types $types): void
// TODO: handle valueString, valueQuantity, etc. types?
// skip primitive types and their child types
if ($type->getKind()->isOneOf(...$skip) || $type->hasPrimitiveParent()) {
if ($type->getKind()->isOneOf(...$skip) || $type->hasPrimitiveOrListParent()) {
continue;
}

Expand Down Expand Up @@ -429,7 +429,7 @@ public static function setCommentContainerFlag(Config $config, Types $types): vo
static $skip = [TypeKindEnum::PRIMITIVE, TypeKindEnum::PHPFHIR_XHTML];
foreach ($types->getIterator() as $type) {
$type->setCommentContainer(
!$type->hasPrimitiveParent() && !$type->getKind()->isOneOf(...$skip)
!$type->hasPrimitiveOrListParent() && !$type->getKind()->isOneOf(...$skip)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

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

enum <?php echo PHPFHIR_ENCODING_ENUM_XML_LOCATION; ?> : string
enum <?php echo PHPFHIR_ENCODING_ENUM_VALUE_XML_LOCATION; ?> : string
{
case ATTRIBUTE = 'attribute';
case ELEMENT = 'element';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@
* limitations under the License.
*/


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

$coreFiles = $config->getCoreFiles();

$xmlLocationEnum = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_ENUM_XML_LOCATION);
$valueXMLLocationEnum = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_ENUM_VALUE_XML_LOCATION);

ob_start();

Expand All @@ -32,29 +31,29 @@

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

trait <?php echo PHPFHIR_ENCODING_TRAIT_XML_LOCATION; ?>
trait <?php echo PHPFHIR_ENCODING_TRAIT_VALUE_XML_LOCATION; ?>

{
private <?php echo PHPFHIR_ENCODING_ENUM_XML_LOCATION; ?> $_xmlLocation;
private <?php echo $valueXMLLocationEnum->getEntityName(); ?> $_valueLoc;

/**
* Set the XML location of this element's value when serializing
*
* @param <?php echo $xmlLocationEnum->getFullyQualifiedName(true); ?> $xmlLocation
* @param <?php echo $valueXMLLocationEnum->getFullyQualifiedName(true); ?> $valueXMLLocation
*/
public function _setXMLLocation(<?php echo PHPFHIR_ENCODING_ENUM_XML_LOCATION; ?> $xmlLocation): void
public function _setValueXMLLocation(<?php echo $valueXMLLocationEnum->getEntityName(); ?> $valueXMLLocation): void
{
$this->_xmlLocation = $xmlLocation;
$this->_valueLoc = $valueXMLLocation;
}

/**
* @return null|<?php echo $xmlLocationEnum->getFullyQualifiedName(true); ?>
* @return null|<?php echo $valueXMLLocationEnum->getFullyQualifiedName(true); ?>

*/
public function _getXMLLocation(): null|<?php echo PHPFHIR_ENCODING_ENUM_XML_LOCATION; ?>
public function _getValueXMLLocation(): null|<?php echo $valueXMLLocationEnum->getEntityName(); ?>

{
return $this->_xmlLocation ?? null;
return $this->_valueLoc ?? null;
}
}
<?php return ob_get_clean();
57 changes: 55 additions & 2 deletions template/core/interface_primitive_type.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,77 @@
* limitations under the License.
*/

use DCarbone\PHPFHIR\Utilities\ImportUtils;

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

$coreFiles = $config->getCoreFiles();

$valueXMLLocationEnum = $coreFiles->getCoreFileByEntityName(PHPFHIR_ENCODING_ENUM_VALUE_XML_LOCATION);

$imports = $coreFile->getImports();
$imports->addCoreFileImportsByName(
PHPFHIR_ENCODING_ENUM_VALUE_XML_LOCATION,
);

ob_start();
echo '<?php ';?>declare(strict_types=1);

namespace <?php echo $coreFile->getFullyQualifiedNamespace(false); ?>;

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

interface <?php echo PHPFHIR_INTERFACE_PRIMITIVE_TYPE; ?> extends <?php echo PHPFHIR_INTERFACE_TYPE; ?>
<?php echo ImportUtils::compileImportStatements($imports); ?>

interface <?php echo PHPFHIR_INTERFACE_PRIMITIVE_TYPE; ?>

{
/**
* Returns the FHIR name represented by this Type
*
* @return string
*/
public function _getFHIRTypeName(): string;

/**
* Must return an associative array in structure ["field" => ["rule" => {constraint}]] to be used during validation
*
* @return array
*/
public function _getValidationRules(): array;

/**
* Must return associative array where, if there are validation errors, the keys are the names of fields within the
* type that failed validation. The value must be a string message describing the manner of error
*
* @return array
*/
public function _getValidationErrors(): array;

/**
* Must return the appropriate "formatted" stringified version of this primitive type's value
*
* @return string
*/
public function getFormattedValue(): string;
public function _getFormattedValue(): string;

/**
* Set the XML location of this element's value when serializing
*
* @param <?php echo $valueXMLLocationEnum->getFullyQualifiedName(true); ?> $valueXMLLocation
*/
public function _setValueXMLLocation(<?php echo $valueXMLLocationEnum->getEntityName(); ?> $valueXMLLocation): void;

/**
* @return null|<?php echo $valueXMLLocationEnum->getFullyQualifiedName(true); ?>

*/
public function _getValueXMLLocation(): null|<?php echo $valueXMLLocationEnum->getEntityName(); ?>;

/**
* @return string
*/
public function __toString(): string;
}
<?php return ob_get_clean();
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions template/versions/types/class_default.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
if ($type->hasLocalProperties()) :
echo "\n";
foreach ($type->getProperties()->getIterator() as $property) :
if ($property->getMemberOf()->hasPrimitiveParent()) {
if ($property->getMemberOf()->hasPrimitiveOrListParent()) {
continue;
}

Expand Down Expand Up @@ -165,7 +165,7 @@ public function _getResourceType(): string
);
endif;

if ($type->hasLocalProperties()) :
if (!$type->isPrimitiveOrListType() && $type->hasLocalProperties()) :
echo "\n";

echo require_with(
Expand All @@ -187,15 +187,15 @@ public function _getResourceType(): string
);
endif;

if (!$type->hasPrimitiveParent()) : ?>
if (!$type->hasPrimitiveOrListParent()) : ?>

/**
* @return string
*/
public function __toString(): string
{
<?php if ($typeKind === TypeKindEnum::PRIMITIVE) : ?>
return $this->getFormattedValue();
return $this->_getFormattedValue();
<?php elseif ($typeKind->isOneOf(TypeKindEnum::LIST, TypeKindEnum::PRIMITIVE_CONTAINER)) : ?>
return (string)$this->getValue();
<?php else : ?>
Expand Down
Loading

0 comments on commit 8aab701

Please sign in to comment.