Skip to content

Commit

Permalink
moving xml location to constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Jan 9, 2025
1 parent aced606 commit 0721106
Show file tree
Hide file tree
Showing 24 changed files with 233 additions and 204 deletions.
7 changes: 1 addition & 6 deletions files/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,6 @@
const PHPFHIR_TRAIT_SOURCE_XMLNS = 'SourceXMLNamespaceTrait';

// Core enums
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_VERSION = 'VersionEnum';

// Core exceptions
Expand All @@ -111,12 +108,10 @@
// 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_TRAIT_XML_LOCATION = 'XMLLocationTrait';
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_CLIENT_INTERFACE_CLIENT = 'ClientInterface';
const PHPFHIR_CLIENT_CLASSNAME_CLIENT = 'Client';
Expand Down
11 changes: 10 additions & 1 deletion src/Utilities/ImportUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public static function buildVersionTypeImports(Type $type): void
if (!$type->isAbstract()) {
$imports->addCoreFileImportsByName(
PHPFHIR_ENCODING_CLASSNAME_XML_WRITER,
PHPFHIR_ENCODING_ENUM_XML_LOCATION,
);
}

Expand Down Expand Up @@ -98,6 +97,16 @@ public static function buildVersionTypeImports(Type $type): void
);
}

if ($type->isValueContainer()
|| $type->hasValueContainerParent()
|| $type->hasPrimitiveContainerParent()
|| $typeKind->isOneOf(TypeKindEnum::PRIMITIVE, TypeKindEnum::LIST, TypeKindEnum::PRIMITIVE_CONTAINER)) {
$imports->addCoreFileImportsByName(PHPFHIR_ENCODING_ENUM_XML_LOCATION);
if (!$type->hasValueContainerParent() && !$type->hasPrimitiveContainerParent()) {
$imports->addCoreFileImportsByName(PHPFHIR_ENCODING_TRAIT_XML_LOCATION);
}
}

if ($restrictionBaseType = $type->getRestrictionBaseFHIRType()) {
$imports->addImport(
$restrictionBaseType->getFullyQualifiedNamespace(false),
Expand Down
10 changes: 6 additions & 4 deletions src/Utilities/TypeHintUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ public static function primitivePHPValueTypeHint(Version $version,
bool $nullable,
bool $stringable = false): string
{
return ($nullable ? 'null|' : '')
. ($stringable ? 'string|' : '')
. $primitiveType->getPHPReturnValueTypeHint();
$base = $primitiveType->getPHPReturnValueTypeHint();
if ($stringable && !str_contains($base, 'string')) {
$base = "string|{$base}";
}
return $nullable ? "null|{$base}" : $base;
}

/**
Expand Down Expand Up @@ -349,6 +351,6 @@ public static function buildSetterParameterHint(Version $version,
array_unshift($hintTypes, 'null');
}

return implode('|', $hintTypes);
return implode('|', array_unique($hintTypes));
}
}
25 changes: 20 additions & 5 deletions src/Version/Definition/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,9 @@ public function hasPropertiesWithValidations(): bool
public function getAllPropertiesIndexedIterator(): iterable
{
$p = [];
foreach ($this->getParentTypes() as $parentType) {
foreach ($this->getRootFirstParentTypes() as $parentType) {
foreach ($parentType->getProperties()->getGenerator() as $property) {
if (!isset($p[$property->getName()])) {
$p[$property->getName()] = $property;
}
$p[$property->getName()] = $property;
}
}
foreach ($this->getProperties()->getGenerator() as $property) {
Expand All @@ -403,8 +401,9 @@ public function getAllPropertiesIndexedIterator(): iterable
public function getParentPropertiesIterator(): iterable
{
$p = [];
foreach ($this->getParentTypes() as $parentType) {
foreach ($this->getRootFirstParentTypes() as $parentType) {
foreach ($parentType->getProperties()->getGenerator() as $property) {
// do not include properties that are overloaded by this type
if (!$this->_properties->hasProperty($property->getName()) && !isset($p[$property->getName()])) {
$p[$property->getName()] = $property;
}
Expand All @@ -427,6 +426,14 @@ public function getParentTypes(): array
return $parents;
}

/**
* @return \DCarbone\PHPFHIR\Version\Definition\Type[]
*/
public function getRootFirstParentTypes(): array
{
return array_reverse($this->getParentTypes());
}

/**
* @return bool
*/
Expand Down Expand Up @@ -852,6 +859,14 @@ public function getDirectlyUsedTraits(): array
->getFullyQualifiedNamespace(false);
}

if (($this->isValueContainer()
|| $this->getKind()->isOneOf(TypeKindEnum::LIST, TypeKindEnum::PRIMITIVE, TypeKindEnum::PRIMITIVE_CONTAINER))
&& !($this->hasValueContainerParent() || $this->hasPrimitiveContainerParent() || $this->hasPrimitiveParent())) {
$traits[PHPFHIR_ENCODING_TRAIT_XML_LOCATION] = $coreFiles
->getCoreFileByEntityName(PHPFHIR_ENCODING_TRAIT_XML_LOCATION)
->getFullyQualifiedNamespace(false);
}

return $traits;
}

Expand Down
22 changes: 14 additions & 8 deletions template/core/class_factory_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@ final class <?php echo PHPFHIR_CLASSNAME_FACTORY_CONFIG; ?>
* <?php echo PHPFHIR_CLASSNAME_FACTORY_CONFIG; ?> Constructor
* @param array $config
*/
public function __construct(array $config = [])
public function __construct(null|iterable $versions = null)
{
foreach(<?php echo PHPFHIR_ENUM_FACTORY_CONFIG_KEY; ?>::cases() as $k) {
if (isset($config[$k->value]) || array_key_exists($k->value, $config)) {
$this->{"set{$k->value}"}($config[$k->value]);
}
if (null !== $versions) {
$this->setVersions(...$versions);
}
}

Expand Down Expand Up @@ -111,10 +109,10 @@ public function getVersion(string $name): null|<?php echo PHPFHIR_CLASSNAME_FACT
/**
* Define all versions at once. Will overwrite any existing versions.
*
* @param array|<?php echo $factVersionConfigClass->getFullyQualifiedName(true); ?>[] $versions Array of version configurations.
* @param array|<?php echo $factVersionConfigClass->getFullyQualifiedName(true); ?> ...$versions Array of version configurations.
* @return self
*/
public function setVersions(array $versions): self
public function setVersions(array|<?php echo PHPFHIR_CLASSNAME_FACTORY_VERSION_CONFIG; ?> ...$versions): self
{
$this->_versions = [];
foreach($versions as $config) {
Expand All @@ -124,15 +122,23 @@ public function setVersions(array $versions): self
}

/**
* Return iterator containing all registered versions.
* Returns iterator containing all registered versions.
*
* @return \ArrayIterator<<?php echo $factVersionConfigClass->getFullyQualifiedName(true); ?>>
*/
public function getVersionsIterator(): iterable
{
if ([] === $this->_versions) {
return new \EmptyIterator();
}
return new \ArrayIterator($this->_versions);
}

/**
* Produces generator over all registered versions.
*
* @return \Generator<?php echo $factVersionConfigClass->getFullyQualifiedName(true); ?>>
*/
public function getVersionsGenerator(): \Generator
{
foreach($this->_versions as $version) {
Expand Down
43 changes: 25 additions & 18 deletions template/core/class_version_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,27 @@ class <?php echo PHPFHIR_CLASSNAME_VERSION_CONFIG; ?> implements <?php echo PHPF
];

/** @var <?php echo $unserializeConfigClass->getFullyQualifiedName(true); ?> */
private <?php echo PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG; ?> $unserializeConfig;
private <?php echo PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG; ?> $_unserializeConfig;

/** @var <?php echo $serializeConfigClass->getFullyQualifiedName(true); ?> */
private <?php echo PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG; ?> $serializeConfig;
private <?php echo PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG; ?> $_serializeConfig;

/**
* <?php echo PHPFHIR_CLASSNAME_VERSION_CONFIG; ?> constructor.
* @param array $config
* @param null|array|<?php echo $serializeConfigClass->getFullyQualifiedName(true); ?> $serializeConfig
* @param null|array|<?php echo $unserializeConfigClass->getFullyQualifiedName(true); ?> $unserializeConfig
*/
public function __construct(array $config = [])
public function __construct(null|array|<?php echo $unserializeConfigClass->getEntityName(); ?> $unserializeConfig = null,
null|array|<?php echo $serializeConfigClass->getEntityName(); ?> $serializeConfig = null)
{
foreach(<?php echo PHPFHIR_ENUM_VERSION_CONFIG_KEY; ?>::cases() as $k) {
if (isset($config[$k->value]) || array_key_exists($k->value, $config)) {
$this->{"set{$k->value}"}($config[$k->value]);
}
}

if (!isset($this->_unserializeConfig)) {
if (null !== $unserializeConfig) {
$this->setUnserializeConfig($unserializeConfig);
} else {
$this->setUnserializeConfig(self::_DEFAULT_UNSERIALIZE_CONFIG);
}
if (!isset($this->_serializeConfig)) {
if (null !== $serializeConfig) {
$this->setSerializeConfig($serializeConfig);
} else {
$this->setSerializeConfig(self::_DEFAULT_SERIALIZE_CONFIG);
}
}
Expand All @@ -89,9 +89,12 @@ public function __construct(array $config = [])
public function setUnserializeConfig(array|<?php echo PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG; ?> $config): self
{
if (is_array($config)) {
$config = new <?php echo PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG; ?>($config);
$config = new <?php echo PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG; ?>(
libxmlOpts: $config['libxmlOpts'] ?? null,
jsonDecodeMaxDepth: $config['jsonDecodeMaxDepth'] ?? null,
);
}
$this->unserializeConfig = $config;
$this->_unserializeConfig = $config;
return $this;
}

Expand All @@ -102,7 +105,7 @@ public function setUnserializeConfig(array|<?php echo PHPFHIR_ENCODING_CLASSNAME
public function getUnserializeConfig(): <?php echo PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG; ?>

{
return $this->unserializeConfig;
return $this->_unserializeConfig;
}

/**
Expand All @@ -112,9 +115,13 @@ public function getUnserializeConfig(): <?php echo PHPFHIR_ENCODING_CLASSNAME_UN
public function setSerializeConfig(array|<?php echo PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG; ?> $config): self
{
if (is_array($config)) {
$config = new <?php echo PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG; ?>($config);
$config = new <?php echo PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG; ?>(
overrideSourceXMLNS: $config['overrideSourceXMLNS'] ?? null,
rootXMLNS: $config['rootXMLNS'] ?? null,
libxmlOpts: $config['libxmlOpts'] ?? null,
);
}
$this->serializeConfig = $config;
$this->_serializeConfig = $config;
return $this;
}

Expand All @@ -125,7 +132,7 @@ public function setSerializeConfig(array|<?php echo PHPFHIR_ENCODING_CLASSNAME_S
public function getSerializeConfig(): <?php echo PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG; ?>

{
return $this->serializeConfig;
return $this->_serializeConfig;
}
}
<?php return ob_get_clean();
51 changes: 47 additions & 4 deletions template/core/client/class_request.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,15 @@ class <?php echo PHPFHIR_CLIENT_CLASSNAME_REQUEST; ?>
/** @var string */
public string $at;

/** @var <?php echo $formatEnum->getFullyQualifiedName(true); ?> */
public <?php echo PHPFHIR_CLIENT_ENUM_RESPONSE_FORMAT; ?> $format;
/**
* The serialization type to request from the server. Typically this is 'json' or 'xml'.
*
* @var string
*/
public string $format;

/** @var <?php echo $sortEnum->getFullyQualifiedName(true); ?> */
public <?php echo PHPFHIR_CLIENT_ENUM_SORT_DIRECTION; ?> $sort;
/** @var string */
public string $sort;

/**
* Extra query parameters.
Expand All @@ -81,5 +85,44 @@ class <?php echo PHPFHIR_CLIENT_CLASSNAME_REQUEST; ?>
* @var array
*/
public array $options;

public function __construct(string|<?php echo PHPFHIR_CLIENT_ENUM_HTTP_METHOD; ?> $method,
string $path,
null|int $count = null,
null|string $since = null,
null|string $at = null,
null|string|<?php echo PHPFHIR_CLIENT_ENUM_RESPONSE_FORMAT; ?> $format = null,
null|string|<?php echo PHPFHIR_CLIENT_ENUM_SORT_DIRECTION; ?> $sort = null,
null|array $queryParams = null,
null|bool $parseResponseHeaders = null,
null|array $options = null)
{
$this->method = (string)$method;
$this->path = $path;
if (null !== $count) {
$this->count = $count;
}
if (null !== $since) {
$this->since = $since;
}
if (null !== $at) {
$this->at = $at;
}
if (null !== $format) {
$this->format = (string)$format;
}
if (null !== $sort) {
$this->sort = (string)$sort;
}
if (null !== $queryParams) {
$this->queryParams = $queryParams;
}
if (null !== $parseResponseHeaders) {
$this->parseResponseHeaders = $parseResponseHeaders;
}
if (null !== $options) {
$this->options = $options;
}
}
}
<?php return ob_get_clean();
16 changes: 11 additions & 5 deletions template/core/encoding/class_serialize_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,18 @@ class <?php echo PHPFHIR_ENCODING_CLASSNAME_SERIALIZE_CONFIG; ?>
/** @var int */
private int $_xhtmlLibxmlOpts;

public function __construct(array $config)
public function __construct(null|bool $overrideSourceXMLNS = null,
null|string $rootXMLNS = null,
null|int $xhtmlLibxmlOpts = null)
{
foreach(<?php echo PHPFHIR_ENCODING_ENUM_SERIALIZE_CONFIG_KEY; ?>::cases() as $k) {
if (isset($config[$k->value]) || array_key_exists($k->value, $config)) {
$this->{"set{$k->value}"}($config[$k->value]);
}
if (null !== $overrideSourceXMLNS) {
$this->setOverrideSourceXMLNS($overrideSourceXMLNS);
}
if (null !== $rootXMLNS) {
$this->setRootXMLNS($rootXMLNS);
}
if (null !== $xhtmlLibxmlOpts) {
$this->setXHTMLLibxmlOpts($xhtmlLibxmlOpts);
}
}

Expand Down
12 changes: 7 additions & 5 deletions template/core/encoding/class_unserialize_config.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ class <?php echo PHPFHIR_ENCODING_CLASSNAME_UNSERIALIZE_CONFIG; ?>
/** @var int */
private int $_jsonDecodeMaxDepth;

public function __construct(array $config)
public function __construct(null|int $libxmlOpts = null,
null|int $jsonDecodeMaxDepth = null)
{
foreach(<?php echo PHPFHIR_ENCODING_ENUM_UNSERIALIZE_CONFIG_KEY; ?>::cases() as $k) {
if (isset($config[$k->value]) || array_key_exists($k->value, $config)) {
$this->{"set{$k->value}"}($config[$k->value]);
}
if (null !== $libxmlOpts) {
$this->setLibxmlOpts($libxmlOpts);
}
if (null !== $jsonDecodeMaxDepth) {
$this->setJsonDecodeMaxDepth($jsonDecodeMaxDepth);
}
}

Expand Down
Loading

0 comments on commit 0721106

Please sign in to comment.