Skip to content

Commit

Permalink
enabling contained types to be set through a container instance
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Jan 13, 2025
1 parent 8a1a4da commit 1edd285
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 94 deletions.
2 changes: 1 addition & 1 deletion src/Builder/TypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private static function buildDefaultType(Config $config,
SimpleXMLElement $sxe,
string $sourceFilename): Type
{
return new Type($config, $version, $fhirName, $sxe, $sourceFilename);
return new Type($version, $fhirName, $sxe, $sourceFilename);
}

/**
Expand Down
11 changes: 9 additions & 2 deletions src/Utilities/ImportUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use DCarbone\PHPFHIR\Builder\Imports;
use DCarbone\PHPFHIR\Enum\TypeKindEnum;
use DCarbone\PHPFHIR\Version;
use DCarbone\PHPFHIR\Version\Definition\Type;

class ImportUtils
Expand All @@ -38,7 +39,7 @@ public static function compileImportStatements(Imports $imports): string
return implode("\n", $stmts) . "\n";
}

public static function buildVersionTypeImports(Type $type): void
public static function buildVersionTypeImports(Version $version, Type $type): void
{
$imports = $type->getImports();

Expand Down Expand Up @@ -123,7 +124,13 @@ public static function buildVersionTypeImports(Type $type): void
// continue;
// }

if ($ptk->isOneOf(TypeKindEnum::RESOURCE_CONTAINER, TypeKindEnum::RESOURCE_INLINE)) {
if ($ptk->isResourceContainer($type->getVersion())) {
$containerType = $version->getDefinition()->getTypes()->getContainerType();
$imports->addImport(
$containerType->getFullyQualifiedNamespace(false),
$containerType->getClassName(),
);

$imports->addCoreFileImportsByName(PHPFHIR_CLASSNAME_CONSTANTS);
$imports->addVersionCoreFileImportsByName($type->getVersion(), PHPFHIR_VERSION_INTERFACE_VERSION_CONTAINED_TYPE);
$imports->addVersionCoreFileImportsByName($type->getVersion(), PHPFHIR_VERSION_CLASSNAME_VERSION_TYPE_MAP);
Expand Down
19 changes: 13 additions & 6 deletions src/Utilities/TypeHintUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public static function typeSetterTypeHint(Version $version, Type $type, bool $nu
* @param \DCarbone\PHPFHIR\Version\Definition\Type $type
* @param bool $fullyQualified
* @return array
* @throws \Exception
*/
public static function buildBaseHintParts(Version $version, Type $type, bool $fullyQualified): array
{
Expand All @@ -162,12 +163,18 @@ public static function buildBaseHintParts(Version $version, Type $type, bool $fu
$hintTypes = $ptp->getPrimitiveType()->getPHPReceiveValueTypeHints();
array_merge($hintTypes, self::buildBaseHintParts($version, $ptp, $fullyQualified));
} else if ($tk->isResourceContainer($version)) {
$hintTypes = [
match ($fullyQualified) {
true => $version->getFullyQualifiedName(true, PHPFHIR_VERSION_INTERFACE_VERSION_CONTAINED_TYPE),
false => PHPFHIR_VERSION_INTERFACE_VERSION_CONTAINED_TYPE,
},
];
$containerType = $version->getDefinition()->getTypes()->getContainerType();

$hintTypes = match ($fullyQualified) {
true => [
$containerType->getFullyQualifiedClassName(true),
$version->getFullyQualifiedName(true, PHPFHIR_VERSION_INTERFACE_VERSION_CONTAINED_TYPE),
],
false => [
$containerType->getClassName(),
PHPFHIR_VERSION_INTERFACE_VERSION_CONTAINED_TYPE,
],
};
} else {
$hintTypes = [
match ($fullyQualified) {
Expand Down
2 changes: 1 addition & 1 deletion src/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public function getEnumImportName(): string
public function getDefinition(): Definition
{
if (!isset($this->_definition)) {
$this->_definition = new Definition($this->_config, $this);
$this->_definition = new Definition($this);
}
return $this->_definition;
}
Expand Down
69 changes: 34 additions & 35 deletions src/Version/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,24 @@
class Definition
{
/** @var \DCarbone\PHPFHIR\Config */
private Config $config;
/** @var \DCarbone\PHPFHIR\Version */
private Version $version;
private Config $_config;
/** @var \DCarbone\PHPFHIR\Version */
private Version $_version;

/** @var \DCarbone\PHPFHIR\Version\Definition\Types|null */
private null|Types $types = null;
private null|Types $_types = null;

/** @var \DCarbone\PHPFHIR\Builder */
private Builder $builder;
private Builder $_builder;

/**
* Definition constructor.
* @param \DCarbone\PHPFHIR\Config $config
* @param \DCarbone\PHPFHIR\Version $version
*/
public function __construct(Config $config, Version $version)
public function __construct(Version $version)
{
$this->config = $config;
$this->version = $version;
$this->_config = $version->getConfig();
$this->_version = $version;
}

/**
Expand All @@ -62,7 +61,7 @@ public function __construct(Config $config, Version $version)
public function __debugInfo()
{
return [
'types' => $this->types,
'types' => $this->_types,
];
}

Expand All @@ -76,60 +75,60 @@ public function buildDefinition(): void
return;
}

$log = $this->config->getLogger();
$log = $this->_config->getLogger();

$log->startBreak('Extracting defined types');

$log->info('Parsing types');
$this->types = TypeExtractor::parseTypes($this->config, $this->version);
$this->_types = TypeExtractor::parseTypes($this->_config, $this->_version);

$log->info('Finding restriction base types');
TypeDecorator::findRestrictionBaseTypes($this->version, $this->types);
TypeDecorator::findRestrictionBaseTypes($this->_version, $this->_types);

$log->info('Finding parent types');
TypeDecorator::findParentTypes($this->config, $this->types);
TypeDecorator::findParentTypes($this->_config, $this->_types);

$log->info('Finding component types');
TypeDecorator::findComponentOfTypes($this->config, $this->types);
TypeDecorator::findComponentOfTypes($this->_config, $this->_types);

// TODO: order of operations issue here, ideally this would be first...
$log->info('Determining type kinds');
TypeDecorator::determineParsedTypeKinds($this->config, $this->version, $this->types);
TypeDecorator::determineParsedTypeKinds($this->_config, $this->_version, $this->_types);

$log->info('Determining Primitive Type kinds');
TypeDecorator::determinePrimitiveTypes($this->config, $this->types);
TypeDecorator::determinePrimitiveTypes($this->_config, $this->_types);

$log->info('Finding properties without names');
TypeDecorator::findNamelessProperties($this->config, $this->types);
TypeDecorator::findNamelessProperties($this->_config, $this->_types);

$log->info('Finding property types');
TypePropertyDecorator::findPropertyTypes($this->config, $this->types);
TypePropertyDecorator::findPropertyTypes($this->_config, $this->_types);

$log->info('Finding overloaded properties in child types');
TypePropertyDecorator::findOverloadedProperties($this->config, $this->types);
TypePropertyDecorator::findOverloadedProperties($this->_config, $this->_types);

$log->info('Manually setting some property names');
TypePropertyDecorator::setMissingPropertyNames($this->config, $this->types);
TypePropertyDecorator::setMissingPropertyNames($this->_config, $this->_types);

$log->info('Parsing union memberOf Types');
TypeDecorator::parseUnionMemberTypes($this->config, $this->types);
TypeDecorator::parseUnionMemberTypes($this->_config, $this->_types);

$log->info('Setting contained type flags');
TypeDecorator::setContainedTypeFlag($this->config, $this->version, $this->types);
TypeDecorator::setContainedTypeFlag($this->_config, $this->_version, $this->_types);

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

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

$log->info('Compiling type imports');
TypeDecorator::buildTypeImports($this->config, $this->types);
TypeDecorator::buildTypeImports($this->_version, $this->_types);

$log->info('Performing some sanity checking');
TypeDecorationValidator::validateDecoration($this->config, $this->version, $this->types);
TypeDecorationValidator::validateDecoration($this->_config, $this->_version, $this->_types);

$log->info(count($this->types) . ' types extracted.');
$log->info(count($this->_types) . ' types extracted.');
$log->endBreak('Extracting defined types');
}

Expand All @@ -138,15 +137,15 @@ public function buildDefinition(): void
*/
public function getConfig(): Config
{
return $this->config;
return $this->_config;
}

/**
* @return \DCarbone\PHPFHIR\Version
*/
public function getVersion(): Version
{
return $this->version;
return $this->_version;
}

/**
Expand All @@ -158,7 +157,7 @@ public function getTypes(): Types
if (!$this->isDefined()) {
$this->buildDefinition();
}
return $this->types;
return $this->_types;
}

/**
Expand All @@ -167,17 +166,17 @@ public function getTypes(): Types
*/
public function isDefined(): bool
{
return isset($this->types);
return isset($this->_types);
}

/**
* @return \DCarbone\PHPFHIR\Builder
*/
public function getBuilder(): Builder
{
if (!isset($this->builder)) {
$this->builder = new Builder($this->config);
if (!isset($this->_builder)) {
$this->_builder = new Builder($this->_config);
}
return $this->builder;
return $this->_builder;
}
}
12 changes: 3 additions & 9 deletions src/Version/Definition/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ class Type
use DocumentationTrait,
SourceTrait;

/** @var \DCarbone\PHPFHIR\Config */
private Config $_config;

/** @var \DCarbone\PHPFHIR\Version */
private Version $_version;

Expand Down Expand Up @@ -104,22 +101,19 @@ class Type

/**
* Type constructor.
* @param \DCarbone\PHPFHIR\Config $config
* @param \DCarbone\PHPFHIR\Version $version
* @param string $fhirName
* @param \SimpleXMLElement|null $sourceSXE
* @param string $sourceFilename
*/
public function __construct(Config $config,
Version $version,
public function __construct(Version $version,
string $fhirName,
null|SimpleXMLElement $sourceSXE = null,
string $sourceFilename = '')
{
if ('' === ($fhirName = trim($fhirName))) {
throw new DomainException('$fhirName must be defined');
}
$this->_config = $config;
$this->_version = $version;
$this->_fhirName = $fhirName;
$this->sourceSXE = $sourceSXE;
Expand All @@ -143,7 +137,7 @@ public function __debugInfo()
*/
public function getConfig(): Config
{
return $this->_config;
return $this->_version->getConfig();
}

/**
Expand All @@ -170,7 +164,7 @@ public function getImports(): Imports
// TODO: implement "extraction done" mechanic
if (!isset($this->_imports)) {
$this->_imports = new Imports(
$this->_config,
$this->_version->getConfig(),
$this->getFullyQualifiedNamespace(false),
$this->getClassName(),
);
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 @@ -479,13 +479,13 @@ public static function parseUnionMemberTypes(Config $config, Types $types): void
}

/**
* @param \DCarbone\PHPFHIR\Config $config
* @param \DCarbone\PHPFHIR\Version $version
* @param \DCarbone\PHPFHIR\Version\Definition\Types $types
*/
public static function buildTypeImports(Config $config, Types $types): void
public static function buildTypeImports(Version $version, Types $types): void
{
foreach ($types->getIterator() as $type) {
ImportUtils::buildVersionTypeImports($type);
ImportUtils::buildVersionTypeImports($version, $type);
}
}
}
2 changes: 1 addition & 1 deletion src/Version/Definition/TypeExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ protected static function extractTypesFromXSD(Config $config, Version $version,
*/
public static function parseTypes(Config $config, Version $version): Types
{
$types = new Types($config, $version);
$types = new Types($version);
$logger = $config->getLogger();

// first, parse all .xsd files without the "fhir-" prefix
Expand Down
Loading

0 comments on commit 1edd285

Please sign in to comment.