Skip to content

Commit

Permalink
friggin json
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Jan 31, 2025
1 parent f726a75 commit 6ed5f39
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 35 deletions.
45 changes: 15 additions & 30 deletions template/versions/types/serialization/json/unserialize/body.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@
$propConstExt = $property->getFieldConstantExtensionName();

if ($propType->isPrimitiveOrListType() || $propType->hasPrimitiveOrListParent()) : ?>
if (!is_array($json)) {
$type->setValue($json);
return $type;
}
if ([] === $json) {
return $type;
}
Expand All @@ -63,28 +59,29 @@
$type-><?php echo $setter; ?>($json[self::<?php echo $propConst; ?>]);
}
<?php elseif ($propType->isPrimitiveContainer() || $propType->hasPrimitiveContainerParent() || $propType->isValueContainer()) : ?>
if (isset($json[self::<?php echo $propConst; ?>]) || isset($json[self::<?php echo $propConstExt; ?>]) || array_key_exists(self::<?php echo $propConst; ?>, $json) || array_key_exists(self::<?php echo $propConstExt; ?>, $json)) {
$value = $json[self::<?php echo $propConst; ?>] ?? null;
$ext = (array)($json[self::<?php echo $propConstExt; ?>] ?? []);
if (isset($json[self::<?php echo $propConst; ?>])
|| isset($json[self::<?php echo $propConstExt; ?>])
|| array_key_exists(self::<?php echo $propConst; ?>, $json)
|| array_key_exists(self::<?php echo $propConstExt; ?>, $json)) {
<?php if ($property->isCollection()) : ?>
if (!is_array($value)) {
$value = [$value];
}
$value = (array)($json[self::<?php echo $propConst; ?>] ?? []);
$ext = (array)($json[self::<?php echo $propConstExt; ?>] ?? []);
$cnt = count($value);
$extCnt = count($ext);
if ($extCnt > $cnt) {
$cnt = $extCnt;
}
for ($i = 0; $i < $cnt; $i++) {
$type-><?php echo $setter; ?>(<?php echo $propTypeClass; ?>::jsonUnserialize(
json: [<?php echo $propTypeClass; ?>::FIELD_VALUE => $value[$i] ?? null] + (array)($ext[$i] ?? []),
config: $config,
[<?php echo $propTypeClass; ?>::FIELD_VALUE => $value[$i] ?? null] + ($ext[$i] ?? []),
$config,
));
}
<?php else : ?>
$value = $json[self::<?php echo $propConst; ?>] ?? null;
$type-><?php echo $setter; ?>(<?php echo $propTypeClass; ?>::jsonUnserialize(
json: [<?php echo $propTypeClass; ?>::FIELD_VALUE => $value] + $ext,
config: $config,
(is_array($value) ? $value : [<?php echo $propTypeClass; ?>::FIELD_VALUE => $value]) + ($json[self::<?php echo $propConstExt; ?>] ?? []),
$config,
));
<?php endif; ?>
}
Expand All @@ -98,19 +95,13 @@
foreach($d as $v) {
$typeClassName = <?php echo PHPFHIR_VERSION_CLASSNAME_VERSION_TYPE_MAP; ?>::getContainedTypeClassNameFromArray($v);
unset($v[<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>::JSON_FIELD_RESOURCE_TYPE]);
$type-><?php echo $setter; ?>($typeClassName::jsonUnserialize(
json: $v,
config: $config,
));
$type-><?php echo $setter; ?>($typeClassName::jsonUnserialize($v, $config));
}
<?php else : ?>
$typeClassName = <?php echo PHPFHIR_VERSION_CLASSNAME_VERSION_TYPE_MAP; ?>::getContainedTypeClassNameFromArray($json[self::<?php echo $propConst; ?>]);
$d = $json[self::<?php echo $propConst; ?>];
unset($d[<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>::JSON_FIELD_RESOURCE_TYPE]);
$type-><?php echo $setter; ?>($typeClassName::jsonUnserialize(
json: $d,
config: $config,
));
$type-><?php echo $setter; ?>($typeClassName::jsonUnserialize($d, $config));
<?php endif; ?>
}
<?php else : ?>
Expand All @@ -121,16 +112,10 @@
$vs = [$vs];
}
foreach($vs as $v) {
$type-><?php echo $setter; ?>(<?php echo $propTypeClass; ?>::jsonUnserialize(
json: $v,
config: $config,
));
$type-><?php echo $setter; ?>(<?php echo $propTypeClass; ?>::jsonUnserialize($v, $config));
}
<?php else : ?>
$type-><?php echo $setter; ?>(<?php echo $propTypeClass; ?>::jsonUnserialize(
json: $json[self::<?php echo $propConst; ?>],
config: $config,
));
$type-><?php echo $setter; ?>(<?php echo $propTypeClass; ?>::jsonUnserialize($json[self::<?php echo $propConst; ?>], $config));
<?php endif; ?>
}
<?php endif;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public static function jsonUnserialize(<?php if ($type->isResourceType() || $typ
get_class($type)
));
}
<?php if ($type->isResourceType() || $type->hasResourceTypeParent()) : ?>
if (null === $config) {
$config = (new <?php echo $versionClass->getEntityName(); ?>())->getConfig()->getUnserializeConfig();
}
Expand All @@ -69,11 +70,12 @@ public static function jsonUnserialize(<?php if ($type->isResourceType() || $typ
} else if (is_object($json)) {
$json = (array)$json;
}
<?php if ($type->hasConcreteParent()) : ?>
parent::jsonUnserialize($json, $config, $type);<?php elseif (!$type->hasCommentContainerParent() && $type->isCommentContainer()) : ?>
if (isset($data[<?php echo $constantsClass->getEntityName(); ?>::JSON_FIELD_FHIR_COMMENTS])) {
$type->_setFHIRComments((array)$data[<?php echo $constantsClass->getEntityName(); ?>::JSON_FIELD_FHIR_COMMENTS]);
}
<?php endif;
if ($type->hasConcreteParent()) : ?>
parent::jsonUnserialize($json, $config, $type); <?php
elseif (!$type->hasCommentContainerParent() && $type->isCommentContainer()) : ?>
if (isset($json[<?php echo $constantsClass->getEntityName(); ?>::JSON_FIELD_FHIR_COMMENTS])) {
$type->_setFHIRComments((array)$json[<?php echo $constantsClass->getEntityName(); ?>::JSON_FIELD_FHIR_COMMENTS]);
}<?php endif;

return ob_get_clean();

0 comments on commit 6ed5f39

Please sign in to comment.