Skip to content

Commit

Permalink
attempting to improve upon what I was trying to do with the "value co…
Browse files Browse the repository at this point in the history
…ntainer" concept
  • Loading branch information
dcarbone committed Feb 15, 2020
1 parent 0372c04 commit 9d5d58b
Show file tree
Hide file tree
Showing 18 changed files with 131 additions and 241 deletions.
10 changes: 10 additions & 0 deletions files/funcs.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,14 @@ function build_raw_type(VersionConfig $config)
$rt->setKind(new TypeKindEnum(TypeKindEnum::RAW));
$rt->addDocumentationFragment(PHPFHIR_RAW_TYPE_DESCRIPTION);
return $rt;
}

/**
* @param \DCarbone\PHPFHIR\Definition\Type $type
*/
function type_debug(Type $type)
{
echo "\n\n\n";
var_dump($type->getConfig()->getVersion()->getName(), $type->getFHIRName());
echo "\n\n\n";
}
3 changes: 0 additions & 3 deletions src/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,6 @@ public function buildDefinition()
$log->info('Setting contained type flags');
TypeDecorator::setContainedTypeFlag($this->config, $this->types);

$log->info('Setting value container flags');
TypeDecorator::setValueContainerFlag($this->config, $this->types);

$log->info('Setting comment container flags');
TypeDecorator::setCommentContainerFlag($this->config, $this->types);

Expand Down
52 changes: 15 additions & 37 deletions src/Definition/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,6 @@ class Type
/** @var bool */
private $containedType = false;
/** @var bool */
private $valueContainer = false;
/** @var bool */
private $commentContainer = false;

/** @var \DCarbone\PHPFHIR\Definition\TypeImports */
Expand Down Expand Up @@ -452,6 +450,19 @@ public function hasPrimitiveParent()
return false;
}

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

/**
* TODO: super hacky.
*
Expand Down Expand Up @@ -674,37 +685,6 @@ public function setContainedType($containedType)
return $this;
}

/**
* @return bool
*/
public function isValueContainer()
{
return $this->valueContainer;
}

/**
* @param bool $valueContainer
* @return \DCarbone\PHPFHIR\Definition\Type
*/
public function setValueContainer($valueContainer)
{
$this->valueContainer = (bool)$valueContainer;
return $this;
}

/**
* @return bool
*/
public function hasValueContainerParent()
{
foreach ($this->getParentTypes() as $parent) {
if ($parent->isValueContainer()) {
return true;
}
}
return false;
}

/**
* @return array
*/
Expand All @@ -722,10 +702,8 @@ public function getDirectlyImplementedInterfaces()
} else {
$interfaces[] = PHPFHIR_INTERFACE_TYPE;
}
} else {
if ($this->isContainedType() && !$parentType->isContainedType()) {
$interfaces[] = PHPFHIR_INTERFACE_CONTAINED_TYPE;
}
} elseif ($this->isContainedType() && !$parentType->isContainedType()) {
$interfaces[] = PHPFHIR_INTERFACE_CONTAINED_TYPE;
}

return $interfaces;
Expand Down
13 changes: 0 additions & 13 deletions src/Definition/TypeDecorationValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,6 @@ public static function validateDecoration(VersionConfig $config, Types $types)
} else {
$seenClasses[$cname] = true;
}

if ($type->isValueContainer()) {
$valueFound = false;
foreach ($type->getProperties()->getIterator() as $property) {
if ($property->isValueProperty()) {
$valueFound = true;
break;
}
}
if (!$valueFound) {
throw ExceptionUtils::createValuePropertyNotFoundException($type);
}
}
}
}
}
93 changes: 39 additions & 54 deletions src/Definition/TypeDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
*/
abstract class TypeDecorator
{
/** @var array */
private static $_dstu1Primitives = ['ResourceType', 'xmlIdRef', 'ResourceNamesPlusBinary'];

/**
* @param \DCarbone\PHPFHIR\Config\VersionConfig $config
* @param \DCarbone\PHPFHIR\Definition\Types $types
Expand Down Expand Up @@ -250,7 +253,9 @@ public static function determinePrimitiveTypes(VersionConfig $config, Types $typ
{
$logger = $config->getLogger();
foreach ($types->getIterator() as $type) {
if ($type->getKind()->isPrimitive()) {
if (in_array($type->getFHIRName(), self::$_dstu1Primitives, true)) {
$ptn = 'string';
} elseif ($type->getKind()->isPrimitive()) {
$ptn = $type->getFHIRName();
} elseif ($type->hasPrimitiveParent()) {
$ptn = $type->getParentType()->getFHIRName();
Expand Down Expand Up @@ -341,37 +346,56 @@ private static function determineParsedTypeKind(VersionConfig $config, Types $ty
if (false !== strpos($fhirName, PHPFHIR_PRIMITIVE_SUFFIX)) {
self::setTypeKind($config, $types, $type, TypeKindEnum::PRIMITIVE);
} elseif (false !== strpos($fhirName, PHPFHIR_LIST_SUFFIX)) {
// for all intents and purposes, a List type is a multiple choice primitive type
self::setTypeKind($config, $types, $type, TypeKindEnum::_LIST);
} elseif (false !== strpos($fhirName, '.') && TypeKindEnum::RESOURCE_INLINE !== $fhirName) {
// This block indicates the type is only present as the child of a Resource. Its name may (and in many
// cases does) conflict with a top level Element or Resource. Because of this, they are treated differently
// and must be marked as such.
self::setTypeKind($config, $types, $type, TypeKindEnum::RESOURCE_COMPONENT);
} elseif ($types->getTypeByName("{$fhirName}-primitive")) {
self::setTypeKind($config, $types, $type, TypeKindEnum::PRIMITIVE_CONTAINER);
} elseif (null !== ($rootType = $type->getRootType()) && $rootType !== $type) {
// this entire block is only hit when generating from DSTU1 sources.

// DSTU1 is weird.

if (null === $rootType->getKind()) {
// ensure root type has kind
// this is due to the out-of-order loading that is made possible by looking at all xml files, rather
// than just fhir-all or something.
self::determineParsedTypeKind($config, $types, $rootType);
}

// These are set to primitive through automagic
// TODO: maybe set to GENERIC?
if (in_array($fhirName, self::$_dstu1Primitives, true)) {
$type->setKind(new TypeKindEnum(TypeKindEnum::PRIMITIVE));
return;
}

$rootTypeKind = $rootType->getKind();
if ($rootTypeKind->isPrimitive()) {
self::setTypeKind($config, $types, $type, (string)TypeKindEnum::GENERIC);
} else {
$set = false;
if ($rootTypeKind->isElement() && [] !== ($parentTypes = $type->getParentTypes())) {
foreach ($parentTypes as $parentType) {
if ('Resource' === $parentType->getFHIRName()) {
$set = true;
self::setTypeKind($config, $types, $type, TypeKindEnum::RESOURCE);
}

// this final block is necessary as in DSTU1 all Resources extend Elements, so we cannot just use the upper-
// most parent to determine type as then they would all just be elements.
$set = false;
if ($rootTypeKind->isElement() && [] !== ($parentTypes = $type->getParentTypes())) {
foreach ($parentTypes as $parentType) {
if ('Resource' === $parentType->getFHIRName()) {
$set = true;
self::setTypeKind($config, $types, $type, TypeKindEnum::RESOURCE);
}
}
if (!$set) {
self::setTypeKind($config, $types, $type, (string)$rootTypeKind);
}
}

if (!$set) {
self::setTypeKind($config, $types, $type, (string)$rootTypeKind);
}
} elseif (TypeKindEnum::isKnownRoot($fhirName)) {
self::setTypeKind($config, $types, $type, $fhirName);
} else {
self::setTypeKind($config, $types, $type, TypeKindEnum::GENERIC);
// this case is only applicable to the DSTU1 type "Binary"
self::setTypeKind($config, $types, $type, TypeKindEnum::RAW);
}
}

Expand Down Expand Up @@ -437,45 +461,6 @@ public static function setContainedTypeFlag(VersionConfig $config, Types $types)
}
}

/**
* @param \DCarbone\PHPFHIR\Config\VersionConfig $config
* @param \DCarbone\PHPFHIR\Definition\Types $types
*/
public static function setValueContainerFlag(VersionConfig $config, Types $types)
{
static $skip = [
TypeKindEnum::PRIMITIVE,
TypeKindEnum::RAW,
TypeKindEnum::QUANTITY,
];

foreach ($types->getIterator() as $type) {
// TODO: handle valueString, valueQuantity, etc. types?
// skip primitive types and their child types
if ($type->getKind()->isOneOf($skip) || $type->hasPrimitiveParent()) {
continue;
}

$properties = $type->getProperties();

// only target types with a single field on them with the name "value"
if (1 !== count($properties) || !$properties->hasProperty(PHPFHIR_VALUE_PROPERTY_NAME)) {
continue;
}

$property = $properties->getProperty(PHPFHIR_VALUE_PROPERTY_NAME);
$propertyType = $property->getValueFHIRType();

// only target types where the "value" field is itself typed
if (null === $propertyType) {
continue;
}

$type->setValueContainer(true);
}
}

/**
* @param \DCarbone\PHPFHIR\Config\VersionConfig $config
* @param \DCarbone\PHPFHIR\Definition\Types $types
Expand Down
33 changes: 18 additions & 15 deletions template/methods/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,14 @@

/** @var \DCarbone\PHPFHIR\Definition\Type $type */
/** @var \DCarbone\PHPFHIR\Definition\Type|null $parentType */
/** @var null|bool $skipTypeName */
/** @var null|bool $skipGetXMLNamespace */
/** @var null|bool $skipGetXMLDefinition */

$xmlName = NameUtils::getTypeXMLElementName($type);
$skipTypeName = isset($skipTypeName) ? (bool)$skipTypeName : false;
$skipGetXMLNamespace = isset($skipGetXMLNamespace) ? (bool)$skipGetXMLNamespace : false;
$skipGetXMLDefinition = isset($skipGetXMLDefinition) ? (bool)$skipGetXMLDefinition : false;

ob_start(); ?>
/**
Expand All @@ -30,41 +36,38 @@
public function _getFHIRTypeName()
{
return self::FHIR_TYPE_NAME;
}<?php if (null === $parentType) : ?>

}
<?php if (null === $parentType) : ?><?php if (!$skipGetXMLNamespace) : ?>

/**
* @return string|null
* @return string
*/
public function _getFHIRXMLNamespace()
{
return '' === $this->_xmlns ? null : $this->_xmlns;
return return $this->_xmlns;
}
<?php endif; ?>

/**
<?php if (!$skipGetXMLDefinition) : ?> /**
* @param null|string $xmlNamespace
* @return static
*/
public function _setFHIRXMLNamespace($xmlNamespace)
{
if (null === $xmlNamespace || is_string($xmlNamespace)) {
$this->_xmlns = (string)$xmlNamespace;
return $this;
}
throw new \InvalidArgumentException(sprintf(
'$xmlNamespace must be a null or string value, %s seen.',
gettype($xmlNamespace)
));
}<?php endif; ?>
$this->_xmlns = trim((string)$xmlNamespace);
return $this;
}
<?php endif; ?>

<?php endif; ?>

/**
* @return string
*/
public function _getFHIRXMLElementDefinition()
{
$xmlns = $this->_getFHIRXMLNamespace();
if (null !== $xmlns) {
if ('' !== $xmlns) {
$xmlns = " xmlns=\"{$xmlns}\"";
}
return "<<?php echo $xmlName; ?>{$xmlns}></<?php echo $xmlName; ?>>";
Expand Down
3 changes: 0 additions & 3 deletions template/methods/constructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@
case $typeKind->isPrimitiveContainer():
echo require_with(PHPFHIR_TEMPLATE_CONSTRUCTORS_DIR . '/primitive_container.php', $requireArgs);
break;
case ($type->isValueContainer() || $type->hasValueContainerParent()):
echo require_with(PHPFHIR_TEMPLATE_CONSTRUCTORS_DIR . '/value_container.php', $requireArgs);
break;

default:
echo require_with(PHPFHIR_TEMPLATE_CONSTRUCTORS_DIR . '/default.php', $requireArgs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
__DIR__ . '/property_setter_primitive.php',
['property' => $property]
);
elseif ($propertyType->isValueContainer() || $propertyType->hasValueContainerParent()) :
elseif ($propertyType->getKind()->isPrimitiveContainer() || $propertyType->hasPrimitiveContainerParent()) :
echo require_with(
__DIR__ . '/property_setter_value_container.php',
['property' => $property]
__DIR__ . '/property_setter_primitive_container.php',
['property' => $property]
);
else :
echo require_with(
Expand Down

0 comments on commit 9d5d58b

Please sign in to comment.