Skip to content

Commit

Permalink
refactoring property creation and overloading
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Jan 7, 2025
1 parent b66edd8 commit 64911a6
Show file tree
Hide file tree
Showing 23 changed files with 269 additions and 281 deletions.
2 changes: 1 addition & 1 deletion src/Builder/TypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private static function buildPrimitiveType(Config $config,
$type = self::buildDefaultType($config, $version, $fhirName, $sxe, $sourceFilename);
$value = new Property($type, $sxe, $sourceFilename);
$value->setName(PHPFHIR_VALUE_PROPERTY_NAME);
$type->getProperties()->addProperty($value);
$type->getProperties()->addOrReturnProperty($value);
return $type;
}

Expand Down
12 changes: 5 additions & 7 deletions src/Utilities/ImportUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace DCarbone\PHPFHIR\Utilities;

/*
* Copyright 2016-2025 Daniel Carbone (daniel.p.carbone@gmail.com)
* Copyright 2025 Daniel Carbone (daniel.p.carbone@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -75,7 +75,7 @@ public static function buildVersionTypeImports(Type $type): void
);

if (($type->isCommentContainer() && !$type->hasCommentContainerParent()) ||
$type->hasLocalPropertiesWithValidations() ||
$type->hasPropertiesWithValidations() ||
($typeKind->isOneOf(TypeKindEnum::PRIMITIVE) && !$type->hasPrimitiveParent())) {
$imports->addCoreFileImportsByName(PHPFHIR_CLASSNAME_CONSTANTS);
}
Expand All @@ -89,17 +89,15 @@ public static function buildVersionTypeImports(Type $type): void
return;
}

$imports->addCoreFileImportsByName(PHPFHIR_CLASSNAME_VALIDATOR);

if ($parentType = $type->getParentType()) {
$imports->addImport(
$parentType->getFullyQualifiedNamespace(false),
$parentType->getClassName(),
);
}

if ($type->hasLocalPropertiesWithValidations()) {
$imports->addCoreFileImportsByName(PHPFHIR_CLASSNAME_VALIDATOR);
}

if ($restrictionBaseType = $type->getRestrictionBaseFHIRType()) {
$imports->addImport(
$restrictionBaseType->getFullyQualifiedNamespace(false),
Expand All @@ -115,7 +113,7 @@ public static function buildVersionTypeImports(Type $type): void

$ptk = $propertyType->getKind();

if ($property->isOverloaded() && !$ptk->isOneOf(TypeKindEnum::PRIMITIVE, TypeKindEnum::LIST)) {
if ($property->getOverloadedProperty() && !$ptk->isOneOf(TypeKindEnum::PRIMITIVE, TypeKindEnum::LIST)) {
continue;
}

Expand Down
29 changes: 18 additions & 11 deletions src/Version/Definition/Decorator/AnyElementTypeDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
use DCarbone\PHPFHIR\Utilities\ExceptionUtils;

/**
* Class AnyElementTypeDecorator
* @package DCarbone\PHPFHIR\Version\Definition\Decorator
* This decorator is seemingly only used within DSTU1. It creates a new property and sets it to the XHTML type.
*/
abstract class AnyElementTypeDecorator
{
Expand All @@ -39,28 +38,36 @@ abstract class AnyElementTypeDecorator
*/
public static function decorate(Config $config, Types $types, Type $type, \SimpleXMLElement $any): void
{
$property = new Property($type, $any, $type->getSourceFilename());

$property->setValueFHIRTypeName('string-primitive');
$namespace = '';
$minOccurs = '';

// parse through attributes
foreach ($any->attributes() as $attribute) {
switch ($attrName = $attribute->getName()) {
switch ($attribute->getName()) {
case AttributeNameEnum::NAMESPACE->value:
$property->setNamespace((string)$attribute);
$namespace = (string)$attribute;
break;
case AttributeNameEnum::MIN_OCCURS->value:
$property->setMinOccurs(intval((string)$attribute));
break;
case AttributeNameEnum::MAX_OCCURS->value:
$property->setMaxOccurs((string)$attribute);
$minOccurs = (string)$attribute;
break;

default:
throw ExceptionUtils::createUnexpectedAttributeException($type, $any, $attribute);
}
}

$property = $type
->getProperties()
->addOrReturnProperty(new Property(PHPFHIR_VALUE_PROPERTY_NAME, $type, $any, $type->getSourceFilename()));

$property->setValueFHIRTypeName(PHPFHIR_XHTML_TYPE_NAME);
if ('' !== $namespace) {
$property->setNamespace($namespace);
}
if ('' !== $minOccurs) {
$property->setMaxOccurs(intval($minOccurs));
}

// parse through child elements
foreach ($any->children('xs', true) as $child) {
switch ($child->getName()) {
Expand Down
58 changes: 43 additions & 15 deletions src/Version/Definition/Decorator/AttributeElementTypeDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,36 +39,67 @@ abstract class AttributeElementTypeDecorator
* @param \DCarbone\PHPFHIR\Version\Definition\Type $type
* @param \SimpleXMLElement $attributeElement
*/
public static function decorate(
Config $config,
Types $types,
Type $type,
\SimpleXMLElement $attributeElement
): void {
// create property object
$property = new Property($type, $attributeElement, $type->getSourceFilename());
public static function decorate(Config $config,
Types $types,
Type $type,
\SimpleXMLElement $attributeElement): void
{
$name = '';
$fhirTypeName = '';
$use = '';
$fixed = '';

// parse through attributes
foreach ($attributeElement->attributes() as $attribute) {
switch ($attribute->getName()) {
case AttributeNameEnum::NAME->value:
$property->setName((string)$attribute);
$name = (string)$attribute;
break;
case AttributeNameEnum::TYPE->value:
$property->setValueFHIRTypeName((string)$attribute);
$fhirTypeName = (string)$attribute;
break;
case AttributeNameEnum::USE->value:
$property->setUse(PropertyUseEnum::from((string)$attribute));
$use = (string)$attribute;
break;
case AttributeNameEnum::FIXED->value:
$property->setFixed((string)$attribute);
$fixed = (string)$attribute;
break;

default:
throw ExceptionUtils::createUnexpectedAttributeException($type, $attributeElement, $attribute);
}
}

if ('' === $name) {
throw new \DomainException(sprintf(
'Unable to determine name of property for type %s: %s',
$type->getFHIRName(),
$attributeElement->asXML()
));
}

// either create new or get existing property definition on type.
$property = $type
->getProperties()
->addOrReturnProperty(
new Property(
memberOf: $type,
sxe: $attributeElement,
sourceFilename: $type->getSourceFilename(),
name: $name,
),
);

if ('' !== $fhirTypeName) {
$property->setValueFHIRTypeName($fhirTypeName);
}
if ('' !== $use) {
$property->setUse(PropertyUseEnum::from($use));
}
if ('' !== $fixed) {
$property->setFixed($fixed);
}

// parse through child elements
foreach ($attributeElement->children('xs', true) as $child) {
switch ($child->getName()) {
Expand All @@ -80,8 +111,5 @@ public static function decorate(
throw ExceptionUtils::createUnexpectedElementException($type, $attributeElement, $child);
}
}

// add property to type
$type->getProperties()->addProperty($property);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,60 +36,74 @@ abstract class ChoiceElementElementPropertyDecorator
* @param \DCarbone\PHPFHIR\Version\Definition\Types $types
* @param \DCarbone\PHPFHIR\Version\Definition\Type $type
* @param \SimpleXMLElement $element
* @param string|null $minOccurs
* @param string|null $maxOccurs
* @param string $minOccurs
* @param string $maxOccurs
* @param \SimpleXMLElement|null $annotationElement
*/
public static function decorate(
Config $config,
Types $types,
Type $type,
\SimpleXMLElement $element,
null|string $minOccurs,
null|string $maxOccurs,
null|\SimpleXMLElement $annotationElement = null
): void {
$property = new Property($type, $element, $type->getSourceFilename());
public static function decorate(Config $config,
Types $types,
Type $type,
\SimpleXMLElement $element,
string $minOccurs,
string $maxOccurs,
null|\SimpleXMLElement $annotationElement = null): void
{

if (null !== $minOccurs) {
$property->setMinOccurs(intval($minOccurs));
}
if (null !== $maxOccurs) {
$property->setMaxOccurs($maxOccurs);
}
if (null !== $annotationElement) {
AnnotationElementPropertyTypeDecorator::decorate(
$config,
$types,
$type,
$property,
$annotationElement
);
}
$name = '';
$ref = '';
$fhirTypeName = '';

foreach ($element->attributes() as $attribute) {
switch ($attribute->getName()) {
case AttributeNameEnum::REF->value:
$property->setRef((string)$attribute);
$ref = (string)$attribute;
break;
case AttributeNameEnum::NAME->value:
$property->setName((string)$attribute);
$name = (string)$attribute;
break;
case AttributeNameEnum::TYPE->value:
$property->setValueFHIRTypeName((string)$attribute);
$fhirTypeName = (string)$attribute;
break;
case AttributeNameEnum::MIN_OCCURS->value:
$property->setMinOccurs(intval((string)$attribute));
$minOccurs = (string)$attribute;
break;
case AttributeNameEnum::MAX_OCCURS->value:
$property->setMaxOccurs((string)$attribute);
$maxOccurs = (string)$attribute;
break;

default:
throw ExceptionUtils::createUnexpectedAttributeException($type, $element, $attribute);
}
}

if ('' === $name) {
throw new \DomainException(sprintf(
'Unable to determine name of property for type %s: %s',
$type->getFHIRName(),
$annotationElement->asXML()
));
}

$property = $type
->getProperties()
->addOrReturnProperty(new Property($name, $type, $element, $type->getSourceFilename()));

if (null !== $minOccurs) {
$property->setMinOccurs(intval($minOccurs));
}
if (null !== $maxOccurs) {
$property->setMaxOccurs($maxOccurs);
}
if (null !== $annotationElement) {
AnnotationElementPropertyTypeDecorator::decorate(
$config,
$types,
$type,
$property,
$annotationElement
);
}

if (null === $property->getName()) {
if ('' === ($ref = $property->getRef())) {
throw new \DomainException(
Expand Down Expand Up @@ -118,6 +132,6 @@ public static function decorate(
}
}

$type->getProperties()->addProperty($property);
$type->getProperties()->addOrReturnProperty($property);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ abstract class ChoiceElementTypeDecorator
*/
public static function decorate(Config $config, Types $types, Type $type, \SimpleXMLElement $choice): void
{
$minOccurs = null;
$maxOccurs = null;
$minOccurs = '';
$maxOccurs = '';
foreach ($choice->attributes() as $attribute) {
switch ($attribute->getName()) {
case AttributeNameEnum::MIN_OCCURS->value:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,6 @@ public static function decorate(Config $config, Types $types, Type $type, Simple
}
}

$type->getProperties()->addProperty($property);
$type->getProperties()->addOrReturnProperty($property);
}
}
Loading

0 comments on commit 64911a6

Please sign in to comment.