Skip to content

Commit

Permalink
lots of cleanup, some stupid bug somewhere
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Jan 5, 2025
1 parent 04c2fb3 commit 0c24ac3
Show file tree
Hide file tree
Showing 57 changed files with 398 additions and 495 deletions.
36 changes: 18 additions & 18 deletions files/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,8 @@
const PHPFHIR_CLASSNAME_FACTORY_VERSION_CONFIG = 'FactoryVersionConfig';
const PHPFHIR_CLASSNAME_FACTORY = 'Factory';
const PHPFHIR_CLASSNAME_VERSION_CONFIG = 'VersionConfig';
const PHPFHIR_CLASSNAME_UNSERIALIZE_CONFIG = 'UnserializeConfig';
const PHPFHIR_CLASSNAME_SERIALIZE_CONFIG = 'SerializeConfig';
const PHPFHIR_CLASSNAME_RESPONSE_PARSER = 'ResponseParser';
const PHPFHIR_CLASSNAME_CONSTANTS = 'Constants';
const PHPFHIR_CLASSNAME_XML_WRITER = 'XMLWriter';
const PHPFHIR_CLASSNAME_VALIDATOR = 'Validator';

// Core interface names
Expand All @@ -104,26 +101,32 @@
const PHPFHIR_ENUM_FACTORY_VERSION_CONFIG_KEY = 'FactoryVersionConfigKey';
const PHPFHIR_ENUM_FACTORY_CONFIG_KEY = 'FactoryConfigKeyEnum';
const PHPFHIR_ENUM_VERSION_CONFIG_KEY = 'VersionConfigKeyEnum';
const PHPFHIR_ENUM_UNSERIALIZE_CONFIG_KEY = 'UnserializeConfigKeyEnum';
const PHPFHIR_ENUM_SERIALIZE_CONFIG_KEY = 'SerializeConfigKeyEnum';
const PHPFHIR_ENUM_VERSION = 'VersionEnum';
const PHPFHIR_ENUM_XML_LOCATION = 'XMLLocationEnum';

// Core exceptions
const PHPFHIR_EXCEPTION_CLIENT_ABSTRACT_CLIENT = 'AbstractClientException';
const PHPFHIR_EXCEPTION_CLIENT_ERROR = 'ClientErrorException';
const PHPFHIR_EXCEPTION_CLIENT_UNEXPECTED_RESPONSE_CODE = 'UnexpectedResponseCodeException';

// Core encoding entities
const PHPFHIR_ENCODING_CLASSNAME_XML_WRITER = 'XMLWriter';
const PHPFHIR_ENCODING_ENUM_XML_LOCATION = 'XMLLocationEnum';
const PHPFHIR_ENCODING_ENUM_SERIALIZE_CONFIG_KEY = 'SerializeConfigKeyEnum';
const PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG = 'SerializeConfig';
const PHPFHIR_ENCODING_ENUM_UNSERIALIZE_CONFIG_KEY = 'UnserializeConfigKeyEnum';
const PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG = 'UnserializeConfig';


// Core client entities
const PHPFHIR_INTERFACE_CLIENT_CLIENT = 'ClientInterface';
const PHPFHIR_CLASSNAME_CLIENT_CLIENT = 'Client';
const PHPFHIR_CLASSNAME_CLIENT_CONFIG = 'Config';
const PHPFHIR_CLASSNAME_CLIENT_REQUEST = 'Request';
const PHPFHIR_CLASSNAME_CLIENT_RESPONSE = 'Response';
const PHPFHIR_CLASSNAME_CLIENT_RESPONSE_HEADERS = 'ResponseHeaders';
const PHPFHIR_ENUM_CLIENT_HTTP_METHOD = 'HTTPMethodEnum';
const PHPFHIR_ENUM_CLIENT_SORT = 'SortDirectionEnum';
const PHPFHIR_ENUM_CLIENT_RESPONSE_FORMAT = 'ResponseFormatEnum';
const PHPFHIR_CLIENT_INTERFACE_CLIENT = 'ClientInterface';
const PHPFHIR_CLIENT_CLASSNAME_CLIENT = 'Client';
const PHPFHIR_CLIENT_CLASSNAME_CONFIG = 'Config';
const PHPFHIR_CLIENT_CLASSNAME_REQUEST = 'Request';
const PHPFHIR_CLIENT_CLASSNAME_RESPONSE = 'Response';
const PHPFHIR_CLIENT_CLASSNAME_RESPONSE_HEADERS = 'ResponseHeaders';
const PHPFHIR_CLIENT_ENUM_HTTP_METHOD = 'HTTPMethodEnum';
const PHPFHIR_CLIENT_ENUM_SORT_DIRECTION = 'SortDirectionEnum';
const PHPFHIR_CLIENT_ENUM_RESPONSE_FORMAT = 'ResponseFormatEnum';

// Version class names
const PHPFHIR_CLASSNAME_VERSION = 'Version';
Expand Down Expand Up @@ -153,9 +156,6 @@

// Static test type, namespace, and class values.
const PHPFHIR_TESTS_NAMESPACE_BASE = 'Tests';
const PHPFHIR_TESTS_NAMESPACE_UNIT = PHPFHIR_TESTS_NAMESPACE_BASE . '\\Unit';
const PHPFHIR_TESTS_NAMESPACE_INTEGRATION = PHPFHIR_TESTS_NAMESPACE_BASE . '\\Integration';
const PHPFHIR_TESTS_NAMESPACE_VALIDATION = PHPFHIR_TESTS_NAMESPACE_BASE . '\\Validation';
const PHPFHIR_TEST_CLASSNAME_CONSTANTS = PHPFHIR_CLASSNAME_CONSTANTS . 'Test';
const PHPFHIR_TEST_CLASSNAME_AUTOLOADER = PHPFHIR_CLASSNAME_AUTOLOADER . 'Test';
const PHPFHIR_TEST_CLASSNAME_TYPE_MAP = PHPFHIR_CLASSNAME_VERSION_TYPE_MAP . 'Test';
Expand Down
7 changes: 4 additions & 3 deletions src/Builder/Imports.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

use DCarbone\PHPFHIR\Config;
use DCarbone\PHPFHIR\Utilities\ExceptionUtils;
use DCarbone\PHPFHIR\Version;
use DCarbone\PHPFHIR\Version\Definition\Type;

Expand Down Expand Up @@ -147,17 +148,17 @@ public function requiredImportCount(): int

/**
* @param \DCarbone\PHPFHIR\Version\Definition\Type $type
* @return \DCarbone\PHPFHIR\Builder\Import|null
* @return \DCarbone\PHPFHIR\Builder\Import
*/
public function getImportByType(Type $type): ?Import
public function getImportByType(Type $type): Import
{
$fqn = $type->getFullyQualifiedClassName(false);
foreach ($this->_imports as $import) {
if ($import->getFullyQualifiedName(false) === $fqn) {
return $import;
}
}
return null;
throw ExceptionUtils::createMissingExpectedImportException($type->getFullyQualifiedClassName(false));
}

/**
Expand Down
32 changes: 17 additions & 15 deletions src/Builder/TypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@ abstract class TypeBuilder
* @param string $sourceFilename
* @return \DCarbone\PHPFHIR\Version\Definition\Type
*/
private static function buildDefaultType(
Config $config,
Version $version,
string $fhirName,
SimpleXMLElement $sxe,
string $sourceFilename
): Type {
private static function buildDefaultType(Config $config,
Version $version,
string $fhirName,
SimpleXMLElement $sxe,
string $sourceFilename): Type
{
return new Type($config, $version, $fhirName, $sxe, $sourceFilename);
}

Expand All @@ -52,13 +51,12 @@ private static function buildDefaultType(
* @param string $sourceFilename
* @return \DCarbone\PHPFHIR\Version\Definition\Type
*/
private static function buildPrimitiveType(
Config $config,
Version $version,
string $fhirName,
SimpleXMLElement $sxe,
string $sourceFilename
): Type {
private static function buildPrimitiveType(Config $config,
Version $version,
string $fhirName,
SimpleXMLElement $sxe,
string $sourceFilename): Type
{
$type = self::buildDefaultType($config, $version, $fhirName, $sxe, $sourceFilename);
$value = new Property($type, $sxe, $sourceFilename);
$value->setName(PHPFHIR_VALUE_PROPERTY_NAME);
Expand All @@ -74,7 +72,11 @@ private static function buildPrimitiveType(
* @param string $sourceFilename
* @return \DCarbone\PHPFHIR\Version\Definition\Type
*/
public static function build(Config $config, Version $version, string $fhirName, SimpleXMLElement $sxe, string $sourceFilename): Type
public static function build(Config $config,
Version $version,
string $fhirName,
SimpleXMLElement $sxe,
string $sourceFilename): Type
{
if (str_contains($fhirName, PHPFHIR_PRIMITIVE_SUFFIX) || str_contains($fhirName, PHPFHIR_LIST_SUFFIX)) {
return self::buildPrimitiveType($config, $version, $fhirName, $sxe, $sourceFilename);
Expand Down
14 changes: 14 additions & 0 deletions src/Utilities/ExceptionUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,4 +512,18 @@ public static function createBundleEntryPropertyNotFoundException(Type $type): \
)
);
}

/**
* @param string $expectedImport
* @return \DomainException
*/
public static function createMissingExpectedImportException(string $expectedImport): \DomainException
{
return new \DomainException(
sprintf(
'Missing expected import: %s',
$expectedImport,
)
);
}
}
33 changes: 19 additions & 14 deletions src/Utilities/ImportUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static function compileImportStatements(Imports $imports): string
public static function buildVersionTypeImports(Type $type): void
{
$imports = $type->getImports();

// immediately add self
$imports->addImport($type->getFullyQualifiedNamespace(false), $type->getClassName());

Expand All @@ -49,17 +50,15 @@ public static function buildVersionTypeImports(Type $type): void

$typeKind = $type->getKind();

$allProperties = $type->getAllPropertiesIndexedIterator();

if (!$type->isAbstract()) {
$imports->addCoreFileImportsByName(PHPFHIR_CLASSNAME_XML_WRITER);
$imports->addCoreFileImportsByName(PHPFHIR_ENUM_XML_LOCATION);
$imports->addCoreFileImportsByName(PHPFHIR_ENCODING_CLASSNAME_XML_WRITER);
$imports->addCoreFileImportsByName(PHPFHIR_ENCODING_ENUM_XML_LOCATION);
}

$imports->addVersionCoreFileImportsByName($type->getVersion(), PHPFHIR_CLASSNAME_VERSION);
$imports->addVersionCoreFileImportsByName($type->getVersion(), PHPFHIR_CLASSNAME_VERSION_CONSTANTS);
$imports->addCoreFileImportsByName(PHPFHIR_CLASSNAME_UNSERIALIZE_CONFIG);
$imports->addCoreFileImportsByName(PHPFHIR_CLASSNAME_SERIALIZE_CONFIG);
$imports->addCoreFileImportsByName(PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG);
$imports->addCoreFileImportsByName(PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG);

$imports->addCoreFileImportsByName(PHPFHIR_INTERFACE_TYPE);

Expand All @@ -78,20 +77,24 @@ public static function buildVersionTypeImports(Type $type): void
}

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

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

if ($restrictionBaseType = $type->getRestrictionBaseFHIRType()) {
$rns = $restrictionBaseType->getFullyQualifiedNamespace(false);
$imports->addImport($rns, $restrictionBaseType->getClassName());
$imports->addImport(
$restrictionBaseType->getFullyQualifiedNamespace(false),
$restrictionBaseType->getClassName(),
);
}

foreach ($allProperties as $property) {
foreach ($type->getAllPropertiesIndexedIterator() as $property) {
$propertyType = $property->getValueFHIRType();
if (null === $propertyType) {
continue;
Expand All @@ -111,12 +114,14 @@ public static function buildVersionTypeImports(Type $type): void
$imports->addVersionCoreFileImportsByName($type->getVersion(), PHPFHIR_CLASSNAME_VERSION);
} else {
if ($ptk === TypeKindEnum::PRIMITIVE_CONTAINER) {
$primType = $propertyType->getLocalProperties()->getProperty('value')->getValueFHIRType();
$primType = $propertyType->getLocalProperties()->getProperty(PHPFHIR_VALUE_PROPERTY_NAME)->getValueFHIRType();
$imports->addImport($primType->getFullyQualifiedNamespace(false), $primType->getClassName());
}

$propertyTypeNS = $propertyType->getFullyQualifiedNamespace(false);
$imports->addImport($propertyTypeNS, $propertyType->getClassName());
$imports->addImport(
$propertyType->getFullyQualifiedNamespace(false),
$propertyType->getClassName(),
);
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/Version/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use DCarbone\PHPFHIR\Builder;
use DCarbone\PHPFHIR\Config;
use DCarbone\PHPFHIR\Utilities\ImportUtils;
use DCarbone\PHPFHIR\Version;
use DCarbone\PHPFHIR\Version\Definition\TypeDecorationValidator;
use DCarbone\PHPFHIR\Version\Definition\TypeDecorator;
Expand Down Expand Up @@ -119,6 +120,9 @@ public function buildDefinition(): void
$log->info('Setting comment container flags');
TypeDecorator::setCommentContainerFlag($this->config, $this->types);

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

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

Expand Down
28 changes: 24 additions & 4 deletions src/Version/Definition/Properties.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
use DCarbone\PHPFHIR\Config;
use DCarbone\PHPFHIR\Enum\TypeKindEnum;
use DCarbone\PHPFHIR\Version;
use InvalidArgumentException;
use SplFixedArray;

/**
* Class Properties
Expand Down Expand Up @@ -90,7 +88,7 @@ public function addProperty(Property &$property): Properties
$pname = $property->getName();
$pref = $property->getRef();
if (null === $pname && null === $pref) {
throw new InvalidArgumentException(
throw new \InvalidArgumentException(
sprintf(
'Cannot add Property to Type "%s" as it has no $name or $ref defined',
$this->getType()->getFHIRName()
Expand Down Expand Up @@ -175,7 +173,18 @@ public function getAllSortedPropertiesIterator(): iterable
}

/**
* Returns an iterator containing only properties local to this type
* Returns an indexed iterator containing only properties local to this type.
*
* @return \DCarbone\PHPFHIR\Version\Definition\Property[]
*/
public function getIndexedLocalPropertiesIterator(): iterable
{
$this->_buildLocalCaches();
return \SplFixedArray::fromArray($this->_localProperties, preserveKeys: false);
}

/**
* Returns an iterator containing only properties local to this type.
*
* @return \DCarbone\PHPFHIR\Version\Definition\Property[]
*/
Expand All @@ -185,6 +194,17 @@ public function getLocalPropertiesIterator(): iterable
return new \ArrayIterator($this->_localProperties);
}

/**
* @return \Generator<\DCarbone\PHPFHIR\Version\Definition\Property>
*/
public function getLocalPropertiesGenerator(): \Generator
{
$this->_buildLocalCaches();
foreach($this->_localProperties as $p) {
yield $p;
}
}

/**
* Returns an iterator containing only properties local to this type, sorted ascending by name
*
Expand Down
18 changes: 8 additions & 10 deletions src/Version/Definition/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,6 @@ public function __construct(
$this->sourceFilename = $sourceFilename;
$this->localProperties = new Properties($config, $version, $this);
$this->enumeration = new Enumeration();
$this->imports = new Imports(
$config,
$this->getFullyQualifiedNamespace(false),
$this->getClassName(),
);
}

/**
Expand Down Expand Up @@ -174,6 +169,14 @@ public function getFHIRName(): string
*/
public function getImports(): Imports
{
// TODO: implement "extraction done" mechanic
if (!isset($this->imports)) {
$this->imports = new Imports(
$this->config,
$this->getFullyQualifiedNamespace(false),
$this->getClassName(),
);
}
return $this->imports;
}

Expand Down Expand Up @@ -298,11 +301,6 @@ public function getFullyQualifiedNamespace(bool $leadingSlash): string
return $this->getVersion()->getFullyQualifiedName($leadingSlash, $this->getTypeNamespace());
}

public function get()
{

}

/**
* @param \DCarbone\PHPFHIR\Enum\TestTypeEnum $testType
* @param bool $leadingSlash
Expand Down
8 changes: 8 additions & 0 deletions src/Version/Definition/TypeDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use DCarbone\PHPFHIR\Enum\PrimitiveTypeEnum;
use DCarbone\PHPFHIR\Enum\TypeKindEnum;
use DCarbone\PHPFHIR\Utilities\ExceptionUtils;
use DCarbone\PHPFHIR\Utilities\ImportUtils;
use DCarbone\PHPFHIR\Version;

/**
Expand Down Expand Up @@ -460,4 +461,11 @@ public static function parseUnionMemberTypes(Config $config, Types $types): void
}
}
}

public static function buildTypeImports(Config $config, Types $types): void
{
foreach ($types->getGenerator() as $type) {
ImportUtils::buildVersionTypeImports($type);
}
}
}
Loading

0 comments on commit 0c24ac3

Please sign in to comment.