Skip to content

Commit

Permalink
couple fixes, need to finish json unserialize for primitives
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Jan 17, 2025
1 parent 677b53c commit 6b4b748
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 61 deletions.
79 changes: 21 additions & 58 deletions src/Utilities/TypeHintUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,13 @@ class TypeHintUtils
* @param \DCarbone\PHPFHIR\Version $version
* @param \DCarbone\PHPFHIR\Enum\PrimitiveTypeEnum $primitiveType
* @param bool $nullable
* @param bool $stringable
* @return string
*/
public static function primitivePHPValueTypeHint(Version $version,
PrimitiveTypeEnum $primitiveType,
bool $nullable,
bool $stringable = false): string
bool $nullable): string
{
$base = $primitiveType->getPHPReturnValueTypeHint();
if ($stringable && !str_contains($base, 'string')) {
$base = "string|{$base}";
}
return $nullable ? "null|{$base}" : $base;
}

Expand Down Expand Up @@ -85,33 +80,6 @@ public static function primitivePHPValueTypeSetterDoc(Version $version
return implode('|', $hintTypes);
}

/**
* @param \DCarbone\PHPFHIR\Version $version
* @param \DCarbone\PHPFHIR\Version\Definition\Type $type
* @param bool $nullable
* @return string
*/
public static function typeHint(Version $version, Type $type, bool $nullable): string
{
$tk = $type->getKind();

// if this is an inline resource
if ($tk->isOneOf(TypeKindEnum::RESOURCE_INLINE, TypeKindEnum::RESOURCE_CONTAINER)) {
return sprintf(
'%s%s',
$nullable ? 'null|' : '',
PHPFHIR_VERSION_INTERFACE_VERSION_CONTAINED_TYPE
);
}

// if we land here, use the value type's class
return sprintf(
'%s%s',
$nullable ? 'null|' : ':',
$type->getClassName()
);
}

/**
* @param \DCarbone\PHPFHIR\Version $version
* @param \DCarbone\PHPFHIR\Version\Definition\Type $type
Expand Down Expand Up @@ -273,11 +241,10 @@ public static function buildSetterParameterDocHint(Version $version,
$pt = $property->getValueFHIRType();

if (null === $pt) {
return self::primitivePHPValueTypeHint(
return self::primitivePHPValueTypeSetterDoc(
version: $version,
primitiveType: $property->getMemberOf()->getPrimitiveType(),
nullable: $nullable,
stringable: true,
);
}

Expand Down Expand Up @@ -315,42 +282,38 @@ public static function buildSetterParameterDocHint(Version $version,
* @param bool $nullable
* @param bool $ignoreCollection
* @return string
* @throws \Exception
*/
public static function buildSetterParameterHint(Version $version,
Property $property,
bool $nullable,
bool $ignoreCollection = false): string
{
$pt = $property->getValueFHIRType();

if (null === $pt) {
return self::primitivePHPValueTypeHint(
version: $version,
primitiveType: $property->getMemberOf()->getPrimitiveType(),
nullable: $nullable,
stringable: true,
);
}

if (!$ignoreCollection && $property->isCollection()) {
return $nullable ? 'null|iterable' : 'iterable';
}

$hintTypes = self::buildBaseHintParts($version, $pt, false);
$pt = $property->getValueFHIRType();

$ptk = $pt->getKind();
if (null === $pt) {
$hintTypes = $property->getMemberOf()->getPrimitiveType()->getPHPReceiveValueTypeHints();
} else {
$hintTypes = self::buildBaseHintParts($version, $pt, false);

if ($property->isValueProperty() || $ptk->isOneOf(TypeKindEnum::LIST, TypeKindEnum::PRIMITIVE)) {
$hintTypes[] = $pt->getClassName();
}
$ptk = $pt->getKind();

if ($ptk === TypeKindEnum::PRIMITIVE_CONTAINER) {
$vp = $pt->getProperties()->getProperty(PHPFHIR_VALUE_PROPERTY_NAME);
array_push(
$hintTypes,
$vp->getValueFHIRType()->getClassName(),
$pt->getClassName(),
);
if ($property->isValueProperty() || $ptk->isOneOf(TypeKindEnum::LIST, TypeKindEnum::PRIMITIVE)) {
$hintTypes[] = $pt->getClassName();
}

if ($ptk === TypeKindEnum::PRIMITIVE_CONTAINER) {
$vp = $pt->getProperties()->getProperty(PHPFHIR_VALUE_PROPERTY_NAME);
array_push(
$hintTypes,
$vp->getValueFHIRType()->getClassName(),
$pt->getClassName(),
);
}
}

if ($nullable) {
Expand Down
3 changes: 2 additions & 1 deletion template/core/client/class_client.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ public function exec(<?php echo PHPFHIR_CLIENT_CLASSNAME_REQUEST; ?> $request):
+ [CURLOPT_CUSTOMREQUEST => $request->method]
+ array_merge($this->_config->getCurlOpts(), $request->options ?? []);

$parseResponseHeaders = ($this->_config->getParseResponseHeaders() && (!isset($req->parseResponseHeaders) || $req->parseResponseHeaders))
$parseResponseHeaders = ($this->_config->getParseResponseHeaders()
&& (!isset($req->parseResponseHeaders) || $req->parseResponseHeaders))
|| (isset($req->parseResponseHeaders) && $req->parseResponseHeaders);

if ($parseResponseHeaders) {
Expand Down
2 changes: 1 addition & 1 deletion template/versions/types/methods/constructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

/**
* <?php echo $typeClassName; ?> Constructor
* @param <?php echo TypeHintUtils::primitivePHPValueTypeSetterDoc($version, $primitiveType, true, false); ?> $value
* @param <?php echo TypeHintUtils::primitivePHPValueTypeSetterDoc($version, $primitiveType, true); ?> $value
* @param <?php echo $xmlLocationEnum->getFullyQualifiedName(true); ?> $xmlLocation
*/
public function __construct(<?php echo TypeHintUtils::buildSetterParameterHint($version, $valueProperty, true); ?> $value = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static function jsonUnserialize(string|array|\stdClass $json,
<?php if ($type->hasConcreteParent()) : ?>
parent::jsonUnserialize($json, $type, $config);<?php elseif (!$type->hasCommentContainerParent() && $type->isCommentContainer()) : ?>
if (isset($data[<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>::JSON_FIELD_FHIR_COMMENTS])) {
$this->_setFHIRComments((array)$data[<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>::JSON_FIELD_FHIR_COMMENTS]);
$type->_setFHIRComments((array)$data[<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>::JSON_FIELD_FHIR_COMMENTS]);
}
<?php endif;

Expand Down

0 comments on commit 6b4b748

Please sign in to comment.