Skip to content

Commit 6ed5f39

Browse files
committed
friggin json
1 parent f726a75 commit 6ed5f39

File tree

2 files changed

+22
-35
lines changed

2 files changed

+22
-35
lines changed

template/versions/types/serialization/json/unserialize/body.php

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@
3838
$propConstExt = $property->getFieldConstantExtensionName();
3939

4040
if ($propType->isPrimitiveOrListType() || $propType->hasPrimitiveOrListParent()) : ?>
41-
if (!is_array($json)) {
42-
$type->setValue($json);
43-
return $type;
44-
}
4541
if ([] === $json) {
4642
return $type;
4743
}
@@ -63,28 +59,29 @@
6359
$type-><?php echo $setter; ?>($json[self::<?php echo $propConst; ?>]);
6460
}
6561
<?php elseif ($propType->isPrimitiveContainer() || $propType->hasPrimitiveContainerParent() || $propType->isValueContainer()) : ?>
66-
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)) {
67-
$value = $json[self::<?php echo $propConst; ?>] ?? null;
68-
$ext = (array)($json[self::<?php echo $propConstExt; ?>] ?? []);
62+
if (isset($json[self::<?php echo $propConst; ?>])
63+
|| isset($json[self::<?php echo $propConstExt; ?>])
64+
|| array_key_exists(self::<?php echo $propConst; ?>, $json)
65+
|| array_key_exists(self::<?php echo $propConstExt; ?>, $json)) {
6966
<?php if ($property->isCollection()) : ?>
70-
if (!is_array($value)) {
71-
$value = [$value];
72-
}
67+
$value = (array)($json[self::<?php echo $propConst; ?>] ?? []);
68+
$ext = (array)($json[self::<?php echo $propConstExt; ?>] ?? []);
7369
$cnt = count($value);
7470
$extCnt = count($ext);
7571
if ($extCnt > $cnt) {
7672
$cnt = $extCnt;
7773
}
7874
for ($i = 0; $i < $cnt; $i++) {
7975
$type-><?php echo $setter; ?>(<?php echo $propTypeClass; ?>::jsonUnserialize(
80-
json: [<?php echo $propTypeClass; ?>::FIELD_VALUE => $value[$i] ?? null] + (array)($ext[$i] ?? []),
81-
config: $config,
76+
[<?php echo $propTypeClass; ?>::FIELD_VALUE => $value[$i] ?? null] + ($ext[$i] ?? []),
77+
$config,
8278
));
8379
}
8480
<?php else : ?>
81+
$value = $json[self::<?php echo $propConst; ?>] ?? null;
8582
$type-><?php echo $setter; ?>(<?php echo $propTypeClass; ?>::jsonUnserialize(
86-
json: [<?php echo $propTypeClass; ?>::FIELD_VALUE => $value] + $ext,
87-
config: $config,
83+
(is_array($value) ? $value : [<?php echo $propTypeClass; ?>::FIELD_VALUE => $value]) + ($json[self::<?php echo $propConstExt; ?>] ?? []),
84+
$config,
8885
));
8986
<?php endif; ?>
9087
}
@@ -98,19 +95,13 @@
9895
foreach($d as $v) {
9996
$typeClassName = <?php echo PHPFHIR_VERSION_CLASSNAME_VERSION_TYPE_MAP; ?>::getContainedTypeClassNameFromArray($v);
10097
unset($v[<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>::JSON_FIELD_RESOURCE_TYPE]);
101-
$type-><?php echo $setter; ?>($typeClassName::jsonUnserialize(
102-
json: $v,
103-
config: $config,
104-
));
98+
$type-><?php echo $setter; ?>($typeClassName::jsonUnserialize($v, $config));
10599
}
106100
<?php else : ?>
107101
$typeClassName = <?php echo PHPFHIR_VERSION_CLASSNAME_VERSION_TYPE_MAP; ?>::getContainedTypeClassNameFromArray($json[self::<?php echo $propConst; ?>]);
108102
$d = $json[self::<?php echo $propConst; ?>];
109103
unset($d[<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>::JSON_FIELD_RESOURCE_TYPE]);
110-
$type-><?php echo $setter; ?>($typeClassName::jsonUnserialize(
111-
json: $d,
112-
config: $config,
113-
));
104+
$type-><?php echo $setter; ?>($typeClassName::jsonUnserialize($d, $config));
114105
<?php endif; ?>
115106
}
116107
<?php else : ?>
@@ -121,16 +112,10 @@
121112
$vs = [$vs];
122113
}
123114
foreach($vs as $v) {
124-
$type-><?php echo $setter; ?>(<?php echo $propTypeClass; ?>::jsonUnserialize(
125-
json: $v,
126-
config: $config,
127-
));
115+
$type-><?php echo $setter; ?>(<?php echo $propTypeClass; ?>::jsonUnserialize($v, $config));
128116
}
129117
<?php else : ?>
130-
$type-><?php echo $setter; ?>(<?php echo $propTypeClass; ?>::jsonUnserialize(
131-
json: $json[self::<?php echo $propConst; ?>],
132-
config: $config,
133-
));
118+
$type-><?php echo $setter; ?>(<?php echo $propTypeClass; ?>::jsonUnserialize($json[self::<?php echo $propConst; ?>], $config));
134119
<?php endif; ?>
135120
}
136121
<?php endif;

template/versions/types/serialization/json/unserialize/header.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public static function jsonUnserialize(<?php if ($type->isResourceType() || $typ
6161
get_class($type)
6262
));
6363
}
64+
<?php if ($type->isResourceType() || $type->hasResourceTypeParent()) : ?>
6465
if (null === $config) {
6566
$config = (new <?php echo $versionClass->getEntityName(); ?>())->getConfig()->getUnserializeConfig();
6667
}
@@ -69,11 +70,12 @@ public static function jsonUnserialize(<?php if ($type->isResourceType() || $typ
6970
} else if (is_object($json)) {
7071
$json = (array)$json;
7172
}
72-
<?php if ($type->hasConcreteParent()) : ?>
73-
parent::jsonUnserialize($json, $config, $type);<?php elseif (!$type->hasCommentContainerParent() && $type->isCommentContainer()) : ?>
74-
if (isset($data[<?php echo $constantsClass->getEntityName(); ?>::JSON_FIELD_FHIR_COMMENTS])) {
75-
$type->_setFHIRComments((array)$data[<?php echo $constantsClass->getEntityName(); ?>::JSON_FIELD_FHIR_COMMENTS]);
76-
}
7773
<?php endif;
74+
if ($type->hasConcreteParent()) : ?>
75+
parent::jsonUnserialize($json, $config, $type); <?php
76+
elseif (!$type->hasCommentContainerParent() && $type->isCommentContainer()) : ?>
77+
if (isset($json[<?php echo $constantsClass->getEntityName(); ?>::JSON_FIELD_FHIR_COMMENTS])) {
78+
$type->_setFHIRComments((array)$json[<?php echo $constantsClass->getEntityName(); ?>::JSON_FIELD_FHIR_COMMENTS]);
79+
}<?php endif;
7880

7981
return ob_get_clean();

0 commit comments

Comments
 (0)