Skip to content

Commit

Permalink
working through some ideas
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Jul 19, 2024
1 parent c668ec3 commit 751bc78
Show file tree
Hide file tree
Showing 21 changed files with 364 additions and 113 deletions.
26 changes: 17 additions & 9 deletions files/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,23 @@
// Core interfaces, traits, and classes
const PHPFHIR_TEMPLATE_CORE_DIR = PHPFHIR_TEMPLATE_DIR . DIRECTORY_SEPARATOR . 'core';

// Type rendering
const PHPFHIR_TEMPLATE_TYPES_DIR = PHPFHIR_TEMPLATE_DIR . DIRECTORY_SEPARATOR . 'types';
const PHPFHIR_TEMPLATE_TYPES_PROPERTIES_DIR = PHPFHIR_TEMPLATE_TYPES_DIR . DIRECTORY_SEPARATOR . 'properties';
const PHPFHIR_TEMPLATE_TYPES_METHODS_DIR = PHPFHIR_TEMPLATE_TYPES_DIR . DIRECTORY_SEPARATOR . 'methods';
const PHPFHIR_TEMPLATE_TYPES_CONSTRUCTORS_DIR = PHPFHIR_TEMPLATE_TYPES_METHODS_DIR . DIRECTORY_SEPARATOR . 'constructors';
const PHPFHIR_TEMPLATE_TYPES_SERIALIZATION_DIR = PHPFHIR_TEMPLATE_TYPES_DIR . DIRECTORY_SEPARATOR . 'serialization';
const PHPFHIR_TEMPLATE_TYPES_VALIDATION_DIR = PHPFHIR_TEMPLATE_TYPES_DIR . DIRECTORY_SEPARATOR . 'validation';
// Version interfaces, traits, and enums
const PHHPFHIR_TEMPLATE_VERSIONS_DIR = PHPFHIR_TEMPLATE_DIR . DIRECTORY_SEPARATOR . 'versions';
const PHPFHIR_TEMPLATE_VERSIONS_CORE_DIR = PHHPFHIR_TEMPLATE_VERSIONS_DIR . DIRECTORY_SEPARATOR . 'core';

// Version type rendering
const PHPFHIR_TEMPLATE_VERSION_TYPES_DIR = PHHPFHIR_TEMPLATE_VERSIONS_DIR . DIRECTORY_SEPARATOR . 'types';
const PHPFHIR_TEMPLATE_VERSION_TYPES_PROPERTIES_DIR = PHPFHIR_TEMPLATE_VERSION_TYPES_DIR . DIRECTORY_SEPARATOR . 'properties';
const PHPFHIR_TEMPLATE_VERSION_TYPES_METHODS_DIR = PHPFHIR_TEMPLATE_VERSION_TYPES_DIR . DIRECTORY_SEPARATOR . 'methods';
const PHPFHIR_TEMPLATE_VERSION_TYPES_CONSTRUCTORS_DIR = PHPFHIR_TEMPLATE_VERSION_TYPES_METHODS_DIR . DIRECTORY_SEPARATOR . 'constructors';
const PHPFHIR_TEMPLATE_VERSION_TYPES_SERIALIZATION_DIR = PHPFHIR_TEMPLATE_VERSION_TYPES_DIR . DIRECTORY_SEPARATOR . 'serialization';
const PHPFHIR_TEMPLATE_VERSION_TYPES_VALIDATION_DIR = PHPFHIR_TEMPLATE_VERSION_TYPES_DIR . DIRECTORY_SEPARATOR . 'validation';


// Fhir type test templates
const PHPFHIR_TEMPLATE_TYPE_TESTS_DIR = PHPFHIR_TEMPLATE_TYPES_DIR . DIRECTORY_SEPARATOR . 'tests';
const PHPFHIR_TEMPLATE_VERSION_TYPE_TESTS_DIR = PHPFHIR_TEMPLATE_VERSION_TYPES_DIR . DIRECTORY_SEPARATOR . 'tests';

// Core class names
const PHPFHIR_CLASSNAME_VERSION = 'PHPFHIRVersion';
const PHPFHIR_CLASSNAME_VERSIONS = 'PHPFHIRVersions';
const PHPFHIR_CLASSNAME_AUTOLOADER = 'PHPFHIRAutoloader';
const PHPFHIR_CLASSNAME_CONFIG = 'PHPFHIRConfig';
Expand All @@ -92,6 +95,9 @@
const PHPFHIR_CLASSNAME_DEBUG_CLIENT = 'PHPFHIRDebugClient';

// Core interface names
const PHPFHIR_INTERFACE_TYPE_MAP = 'PHPFHIRTypeMapInterface';
const PHPFHIR_INTERFACE_RESPONSE_PARSER = 'PPHPFHIRResponseParserInterface';
const PHPFHIR_INTERFACE_FHIR_VERSION = 'PHPFHIRVersionInterface';
const PHPFHIR_INTERFACE_TYPE = 'PHPFHIRTypeInterface';
const PHPFHIR_INTERFACE_CONTAINED_TYPE = 'PHPFHIRContainedTypeInterface';
const PHPFHIR_INTERFACE_COMMENT_CONTAINER = 'PHPFHIRCommentContainerInterface';
Expand All @@ -111,6 +117,8 @@
const PHPFHIR_ENUM_API_RESOURCE_LIST = 'PHPFHIRApiResourceListEnum';
const PHPFHIR_ENUM_API_SORT = 'PHPFHIRApiSortEnum';

// Version Co

// validation constants
const PHPFHIR_VALIDATION_ENUM = 'enum';
const PHPFHIR_VALIDATION_ENUM_NAME = 'VALIDATE_ENUM';
Expand Down
6 changes: 3 additions & 3 deletions src/Render/Templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static function renderCoreType(string $coreFilename, Config $config): str
*/
public static function renderXhtmlTypeClass(Config $config, Version $version, Types $types, Type $type): string
{
return require PHPFHIR_TEMPLATE_TYPES_DIR . DIRECTORY_SEPARATOR . 'class_xhtml.php';
return require PHPFHIR_TEMPLATE_VERSION_TYPES_DIR . DIRECTORY_SEPARATOR . 'class_xhtml.php';
}

/**
Expand All @@ -61,7 +61,7 @@ public static function renderXhtmlTypeClass(Config $config, Version $version, Ty
*/
public static function renderFhirTypeClass(Config $config, Version $version, Types $types, Type $type): string
{
return require PHPFHIR_TEMPLATE_TYPES_DIR . DIRECTORY_SEPARATOR . 'class_default.php';
return require PHPFHIR_TEMPLATE_VERSION_TYPES_DIR . DIRECTORY_SEPARATOR . 'class_default.php';
}

/**
Expand All @@ -74,6 +74,6 @@ public static function renderFhirTypeClass(Config $config, Version $version, Typ
*/
public static function renderFhirTypeClassTest(Config $config, Version $version, Types $types, Type $type, TestType $testType): string
{
return require PHPFHIR_TEMPLATE_TYPE_TESTS_DIR . DIRECTORY_SEPARATOR . $testType->value . DIRECTORY_SEPARATOR .'class.php';
return require PHPFHIR_TEMPLATE_VERSION_TYPE_TESTS_DIR . DIRECTORY_SEPARATOR . $testType->value . DIRECTORY_SEPARATOR .'class.php';
}
}
8 changes: 8 additions & 0 deletions src/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ public function getFullyQualifiedTestsName(TestType $testType, bool $leadingSlas
return $this->getFullyQualifiedName($leadingSlash, $testType->namespaceSlug(), ...$bits);
}

/**
* @return string
*/
public function getVersionClassname(): string
{
return ucfirst($this->name);
}

/**
* @return \DCarbone\PHPFHIR\Version\Definition
*/
Expand Down
47 changes: 5 additions & 42 deletions template/core/classes/class_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
echo "namespace {$namespace};\n\n";
endif;

echo VersionCopyright::getFullPHPFHIRCopyrightComment();
echo $config->getBasePHPFHIRCopyrightComment();

echo "\n\n"; ?>
/**
Expand All @@ -43,9 +43,6 @@
*/
class <?php echo PHPFHIR_CLASSNAME_CONFIG; ?> implements \JsonSerializable
{
/** @var bool */
private bool $registerAutoloader = false;

/** @var int */
private int $libxmlOpts;

Expand All @@ -61,47 +58,13 @@ class <?php echo PHPFHIR_CLASSNAME_CONFIG; ?> implements \JsonSerializable
*/
public function __construct(array $config = [])
{
foreach(<?php echo PHPFHIR_ENUM_CONFIG_KEY; ?>::values() as $k => $_) {
if (isset($config[$k]) || array_key_exists($k, $config)) {
$this->setKey($k, $config[$k]);
foreach(<?php echo PHPFHIR_ENUM_CONFIG_KEY; ?>::cases() as $key) {
if (isset($config[$key->value]) || array_key_exists($key->value, $config)) {
$this->{"set$key->value"}($config[$key->value);
}
}
}

/**
* Set arbitrary key on this config
*
* @param <?php echo $config->getFullyQualifiedName(true, PHPFHIR_ENUM_CONFIG_KEY); ?>|string $key
* @param mixed $value
* @return static
*/
public function setKey(<?php echo PHPFHIR_ENUM_CONFIG_KEY; ?>|string $key, mixed $value): self
{
if (!is_string($key)) {
$key = $key->value;
}
$this->{'set'.$key}($value);
return $this;
}

/**
* @param bool $registerAutoloader
* @return static
*/
public function setRegisterAutoloader(bool $registerAutoloader): self
{
$this->registerAutoloader = $registerAutoloader;
return $this;
}

/**
* @return bool
*/
public function getRegisterAutoloader(): bool
{
return $this->registerAutoloader;
}

/**
* Sets the option flags to provide to libxml when unserializing XML
*
Expand Down Expand Up @@ -171,7 +134,7 @@ public function jsonSerialize(): \stdClass
{
$out = new \stdClass();
foreach(<?php echo PHPFHIR_ENUM_CONFIG_KEY; ?>::cases() as $key) {
$out->{$k} = $this->{$key->getter()}();
$out->{$key->value} = $this->{"get$key->value"}();
}
return $out;
}
Expand Down
3 changes: 1 addition & 2 deletions template/core/enums/enum_config_key.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
echo "namespace {$namespace};\n\n";
endif;

echo VersionCopyright::getFullPHPFHIRCopyrightComment();
echo $config->getBasePHPFHIRCopyrightComment();

echo "\n\n"; ?>
/**
Expand All @@ -43,7 +43,6 @@
*/
enum <?php echo PHPFHIR_ENUM_CONFIG_KEY; ?> : string
{
case REGISTER_AUTOLOADER = 'registerAutoloader';
case LIBXML_OPTS = 'libxmlOpts';
case ROOT_XMLNS = 'rootXmlns';
case OVERRIDE_SOURCE_XMLNS = 'overrideSourceXmlns';
Expand Down
59 changes: 59 additions & 0 deletions template/core/interfaces/interface_fhir_version.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,62 @@
* limitations under the License.
*/

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

$namespace = $config->getFullyQualifiedName(false);

ob_start();

echo "<?php declare(strict_types=1);\n\n";

if ('' !== $namespace) :
echo "namespace {$namespace};\n\n";
endif;

echo $config->getBasePHPFHIRCopyrightComment();

echo "\n\n";
?>
/**
* Interface <?php echo PHPFHIR_INTERFACE_FHIR_VERSION; if ('' !== $namespace) : ?>

* @package \<?php echo $namespace; ?>
<?php endif; ?>

*/
interface <?php echo PHPFHIR_INTERFACE_FHIR_VERSION; ?>

{
/**
* Must return the "name" of this version, e.g. DSTU1, STU3, R5, etc.
* @return string
*/
public function getName(): string;

/**
* Must return the source this version was generated from
* @return string
*/
public function getSourceUrl(): string;

/**
* Must return the date this FHIR version's source was generated
* @return string
*/
public function getFhirGenerationDate(): string;

/**
* Must return config for this version
* @return <?php echo $config->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_CONFIG); ?>

*/
public function getConfig(): <?php echo PHPFHIR_CLASSNAME_CONFIG; ?>;

/**
* Must return the type map class for this version
* @return <?php echo $config->getFullyQualifiedName(true, PHPFHIR_INTERFACE_TYPE_MAP); ?>

*/
public function getTypeMap(): <?php echo PHPFHIR_INTERFACE_TYPE_MAP; ?>;
}
<?php return ob_get_clean();
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*/

/** @var \DCarbone\PHPFHIR\Config $config */
/** @var \DCarbone\PHPFHIR\Version $version */

$namespace = $config->getFullyQualifiedName(false);

Expand All @@ -29,22 +28,27 @@
echo "namespace {$namespace};\n\n";
endif;

echo $version->getCopyright()->getFullPHPFHIRCopyrightComment();
echo $config->getBasePHPFHIRCopyrightComment();

echo "\n\n";
?>
/**
* Class <?php echo PHPFHIR_CLASSNAME_VERSION; if ('' !== $namespace) : ?>
* Interface <?php echo PHPFHIR_INTERFACE_RESPONSE_PARSER; if ('' !== $namespace) : ?>

* @package \<?php echo $namespace; ?>
<?php endif; ?>

*/
class <?php echo PHPFHIR_CLASSNAME_VERSION; ?>
interface <?php echo PHPFHIR_INTERFACE_RESPONSE_PARSER; ?>

{
public const NAME = '<?php echo $version->getName(); ?>';
public const SOURCE_URL = '<?php echo $version->getSourceUrl(); ?>';
public const FHIR_GENERATION_DATE = '<?php echo $version->getCopyright()->getFHIRGenerationDate(); ?>';
/**
* Must attempt to parse the provided input into FHIR objects.
*
* @param null|string|array|\stdClass|\SimpleXMLElement|\DOMDocument $input
* @return null|<?php echo $config->getFullyQualifiedName(true, PHPFHIR_INTERFACE_TYPE); ?>

* @throws \Exception
*/
public function parse(null|string|array|\stdClass|\SimpleXMLElement|\DOMDocument $input): null|<?php echo PHPFHIR_INTERFACE_TYPE; ?>;
}
<?php return ob_get_clean();
93 changes: 93 additions & 0 deletions template/core/interfaces/interface_type_map.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php declare(strict_types=1);

/*
* Copyright 2024 Daniel Carbone ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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

$namespace = $config->getFullyQualifiedName(false);

ob_start();

echo "<?php declare(strict_types=1);\n\n";

if ('' !== $namespace) :
echo "namespace {$namespace};\n\n";
endif;

echo $config->getBasePHPFHIRCopyrightComment();

echo "\n\n";
?>
/**
* Interface <?php echo PHPFHIR_INTERFACE_TYPE_MAP; if ('' !== $namespace) : ?>

* @package \<?php echo $namespace; ?>
<?php endif; ?>

*/
interface <?php echo PHPFHIR_INTERFACE_TYPE_MAP; ?>

{
/**
* Must return the fully qualified class name for FHIR Type name. Must return null if type not found
* @param string $typeName
* @return string|null
*/
public function getTypeClass(string $typeName): null|string;

/**
* Must return the full internal class map
* @return array
*/
public function getMap(): array;

/**
* Must return the full list of containable resource types
* @return array
*/
public function getContainableTypes(): array;

/**
* @param string $typeName Name of FHIR object reference by a version's container type
* @return string|null Name of class as string or null if type is not contained in map
*/
public function getContainedTypeClassName(string $typeName): null|string;

/**
* Must attempt to determine if the provided value is or describes a containable resource type
* @param string|array|\SimpleXMLElement|<?php echo PHPFHIR_INTERFACE_TYPE; ?> $type
* @return bool
* @throws \InvalidArgumentException
*/
public function isContainableResource(string|array|\SimpleXMLElement|<?php echo PHPFHIR_INTERFACE_TYPE; ?> $type): bool;

/**
* @param \SimpleXMLElement $node Parent element containing inline resource
* @param <?php echo $config->getFullyQualifiedName(true, PHPFHIR_CLASSNAME_CONFIG); ?> $config
* @return null|<?php echo $config->getfullyQualifiedName(true, PHPFHIR_INTERFACE_CONTAINED_TYPE); ?>

*/
public function getContainedTypeFromXML(\SimpleXMLElement $node, <?php echo PHPFHIR_CLASSNAME_CONFIG; ?> $config): null|<?php echo PHPFHIR_INTERFACE_CONTAINED_TYPE; ?>;

/**
* @param array|null $data
* @return null|<?php echo $config->getfullyQualifiedName(true, PHPFHIR_INTERFACE_CONTAINED_TYPE); ?>

*/
public function getContainedTypeFromArray(null|array $data): null|<?php echo PHPFHIR_INTERFACE_CONTAINED_TYPE; ?>;
}
<?php return ob_get_clean();
Loading

0 comments on commit 751bc78

Please sign in to comment.