Skip to content

Commit

Permalink
more work on json encoding, need to fix xhtml type
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Jan 22, 2025
1 parent a1e8ab0 commit 56313c9
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 113 deletions.
10 changes: 7 additions & 3 deletions phpunit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,27 @@
const PHPFHIR_TETS_GENERATED_AUTOLOADER_PATH = PHPFHIR_TEST_CONFIG_ROOT_DIR . '/../output/src/DCarbone/PHPFHIRGenerated/Autoloader.php';

// require generator autoloader
(function () {
(function (): void {
$composer_autoloader = realpath(PHPFHIR_TEST_COMPOSER_AUTOLOADER_PATH);
echo "Requiring composer autoloader: {$composer_autoloader}\n";
require $composer_autoloader;
})();

// generate code for test target
(function () {
(function (): void {
$phpfhir_test_target = getenv('PHPFHIR_TEST_TARGET');
if ('true' === (string)getenv('PHPFHIR_TEST_SKIP_GENERATE')) {
echo "Skipping code generation for target: {$phpfhir_test_target}\n";
return;
}
echo "Generating code for target: {$phpfhir_test_target}\n";
$config = \DCarbone\PHPFHIR\Config::fromArray(require PHPFHIR_TEST_CONFIG_FILE);
$builder = new DCarbone\PHPFHIR\Builder($config);
$builder->render();
})();

// require generated autoloader
(function () {
(function (): void {
$generated_autoloader = realpath(PHPFHIR_TETS_GENERATED_AUTOLOADER_PATH);
echo "Requiring generated autoloader: {$generated_autoloader}\n";
require $generated_autoloader;
Expand Down
3 changes: 1 addition & 2 deletions template/core/interface_primitive_type.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@

<?php echo ImportUtils::compileImportStatements($imports); ?>

interface <?php echo PHPFHIR_INTERFACE_PRIMITIVE_TYPE; ?>

interface <?php echo PHPFHIR_INTERFACE_PRIMITIVE_TYPE; ?> extends \JsonSerializable
{
/**
* Returns the FHIR name represented by this Type
Expand Down
3 changes: 3 additions & 0 deletions template/tests/versions/types/class.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,16 @@ public function testCanTranscodeBundleJSON()
$rc = $client->readRaw(
resourceType: <?php echo $versionTypeEnum->getEntityName(); ?>::<?php echo $type->getConstName(false); ?>,
format: <?php echo $clientFormatEnum->getEntityName(); ?>::JSON,
count: 5,
);
if (404 === $rc->getCode()) {
$this->markTestSkipped(sprintf(
'Configured test endpoint "%s" has no resources of type "<?php echo $type->getFHIRName(); ?>"',
$this->_getTestEndpoint(),
));
}
$this->assertIsString($rc->getResp());
$this->assertJSON($rc->getResp());
$this->assertEquals(200, $rc->getCode(), sprintf('Configured test endpoint "%s" returned non-200 response code', $this->_getTestEndpoint()));
$bundle = <?php echo $bundleType->getClassName(); ?>::jsonUnserialize(
json: $rc->getResp(),
Expand Down
21 changes: 11 additions & 10 deletions template/versions/types/class_default.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,19 @@ public function _getResourceType(): string
);
endif;

if (!$type->isPrimitiveOrListType() && $type->hasLocalProperties()) :
if ($type->hasLocalProperties()) :
echo "\n";
if (!$type->isPrimitiveOrListType()) :
echo require_with(
PHPFHIR_TEMPLATE_VERSION_TYPES_SERIALIZATION_DIR . '/xml.php',
[
'version' => $version,
'type' => $type,
]
);

echo require_with(
PHPFHIR_TEMPLATE_VERSION_TYPES_SERIALIZATION_DIR . '/xml.php',
[
'version' => $version,
'type' => $type,
]
);

echo "\n";
echo "\n";
endif;

echo require_with(
PHPFHIR_TEMPLATE_VERSION_TYPES_SERIALIZATION_DIR . '/json.php',
Expand Down
3 changes: 3 additions & 0 deletions template/versions/types/class_xhtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,9 @@ public function getXMLReader(int $libxmlOpts): null|\XMLReader

// TODO: Right now this assumes a single string value. Need an example use case.
?>
if (null === $json) {
return $type;
}
if ([] !== $json) {
$v = reset($json);
if (is_string($v)) {
Expand Down
38 changes: 19 additions & 19 deletions template/versions/types/serialization/json.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,36 @@
* limitations under the License.
*/

use DCarbone\PHPFHIR\Enum\TypeKindEnum;

/** @var \DCarbone\PHPFHIR\Version $version */
/** @var \DCarbone\PHPFHIR\Version\Definition\Type $type */

$typeKind = $type->getKind();

ob_start();

echo require_with(
PHPFHIR_TEMPLATE_VERSION_TYPES_SERIALIZATION_DIR . '/json/unserialize/header.php',
[
'version' => $version,
'type' => $type,
]
);
if (!$type->isPrimitiveOrListType()) :
echo require_with(
PHPFHIR_TEMPLATE_VERSION_TYPES_SERIALIZATION_DIR . '/json/unserialize/header.php',
[
'version' => $version,
'type' => $type,
]
);

echo "\n";
echo "\n";

echo require_with(
PHPFHIR_TEMPLATE_VERSION_TYPES_SERIALIZATION_DIR . '/json/unserialize/body.php',
[
'version' => $version,
'type' => $type,
]
);
echo require_with(
PHPFHIR_TEMPLATE_VERSION_TYPES_SERIALIZATION_DIR . '/json/unserialize/body.php',
[
'version' => $version,
'type' => $type,
]
);

echo "\n";
echo "\n";
endif;

if ($typeKind->isOneOf(TypeKindEnum::PRIMITIVE, TypeKindEnum::LIST)) :
if ($type->isPrimitiveOrListType()) :
echo require_with(
PHPFHIR_TEMPLATE_VERSION_TYPES_SERIALIZATION_DIR . '/json/serialize/primitive.php',
[
Expand Down
131 changes: 52 additions & 79 deletions template/versions/types/serialization/json/unserialize/body.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,99 +61,72 @@
<?php elseif ($propType->getKind() === TypeKindEnum::PRIMITIVE_CONTAINER || $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 = (isset($json[self::<?php echo $propConstExt; ?>]) && is_array($json[self::<?php echo $propConstExt; ?>])) ? $json[self::<?php echo $propConstExt; ?>] : [];
if (null !== $value) {
if ($value instanceof <?php echo $propTypeClass; ?>) {
$type-><?php echo $setter; ?>($value);
} else <?php if ($property->isCollection()) : ?>if (is_array($value)) {
foreach($value as $i => $v) {
if ($v instanceof <?php echo $propTypeClass; ?>) {
$type-><?php echo $setter; ?>($v);
} else {
$iext = (isset($ext[$i]) && is_array($ext[$i])) ? $ext[$i] : [];
if (is_array($v)) {
$type-><?php echo $setter; ?>(new <?php echo $propTypeClass; ?>(array_merge($v, $iext)));
} else {
$type-><?php echo $setter; ?>(new <?php echo $propTypeClass; ?>(value: [<?php echo $propTypeClass; ?>::FIELD_VALUE => $v] + $iext));
}
}
}
} else<?php endif; ?>if (is_array($value)) {
$type-><?php echo $setter; ?>(new <?php echo $propTypeClass; ?>(array_merge($ext, $value)));
} else {
$type-><?php echo $setter; ?>(<?php echo $propTypeClass; ?>::jsonUnserialize([<?php echo $propTypeClass; ?>::FIELD_VALUE => $value] + $ext));
}
} elseif ([] !== $ext) {
<?php if ($property->isCollection()) : ?>
foreach($ext as $iext) {
$type-><?php echo $setter; ?>(new <?php echo $propTypeClass; ?>($iext));
}
<?php else : ?>
$type-><?php echo $setter; ?>(new <?php echo $propTypeClass; ?>($ext));
<?php endif; ?>
} else {
$type-><?php echo $setter; ?>(new <?php echo $propTypeClass; ?>(null));
$ext = (array)($json[self::<?php echo $propConstExt; ?>] ?? []);
<?php if ($property->isCollection()) : ?>
if (!is_array($value)) {
$value = [$value];
}
$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 else : ?>
$type-><?php echo $setter; ?>(<?php echo $propTypeClass; ?>::jsonUnserialize(
json: [<?php echo $propTypeClass; ?>::FIELD_VALUE => $value] + $ext,
config: $config,
));
<?php endif; ?>
}
<?php elseif ($propTypeKind->isResourceContainer($version)) : ?>
if (isset($data[self::<?php echo $propConst; ?>])) {
if (isset($json[self::<?php echo $propConst; ?>])) {
<?php if ($property->isCollection()) : ?>
if (is_array($data[self::<?php echo $propConst; ?>])) {
if (is_int(key($data[self::<?php echo $propConst; ?>]))) {
$type-><?php echo $setter; ?>($data[self::<?php echo $propConst; ?>]);
} else {
$typeClassName = <?php echo PHPFHIR_VERSION_CLASSNAME_VERSION_TYPE_MAP; ?>::getContainedTypeClassNameFromArray($data[self::<?php echo $propConst; ?>]);
$d = $data[self::<?php echo $propConst; ?>];
unset($d[<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>::JSON_FIELD_RESOURCE_TYPE]);
$type-><?php echo $setter; ?>(new $typeClassName($d));
}
} elseif (!is_object($data[self::<?php echo $propConst; ?>]) || !($data[self::<?php echo $propConst; ?>] instanceof <?php echo PHPFHIR_VERSION_INTERFACE_VERSION_CONTAINED_TYPE; ?>)) {
throw new \InvalidArgumentException(sprintf(
'<?php echo $propTypeClass; ?> - Field "<?php echo $property->getName(); ?>" must be an array of objects implementing <?php echo $version->getFullyQualifiedName(true, PHPFHIR_VERSION_INTERFACE_VERSION_CONTAINED_TYPE); ?>, value of type %s seen',
is_object($data[self::<?php echo $propConst; ?>]) ? get_class($data[self::<?php echo $propConst; ?>]) : gettype($data[self::<?php echo $propConst; ?>])
));
} else {
$type-><?php echo $setter; ?>($data[self::<?php echo $propConst; ?>]);
$d = $json[self::<?php echo $propConst; ?>];
if (!is_int(key($d))) {
$d = [$d];
}
<?php else : ?>
if (is_array($data[self::<?php echo $propConst; ?>])) {
$typeClassName = <?php echo PHPFHIR_VERSION_CLASSNAME_VERSION_TYPE_MAP; ?>::getContainedTypeClassNameFromArray($data[self::<?php echo $propConst; ?>]);
$d = $data[self::<?php echo $propConst; ?>];
unset($d[<?php echo PHPFHIR_CLASSNAME_CONSTANTS; ?>::JSON_FIELD_RESOURCE_TYPE]);
$typeClass = new $typeClassName($d);
} else if (!is_object($data[self::<?php echo $propConst; ?>]) || !($data[self::<?php echo $propConst; ?>] instanceof <?php echo PHPFHIR_VERSION_INTERFACE_VERSION_CONTAINED_TYPE; ?>)) {
throw new \InvalidArgumentException(sprintf(
'<?php echo $propTypeClass; ?> - Field "<?php echo $property->getName(); ?>" must be an array or object implementing <?php echo $version->getFullyQualifiedName(true, PHPFHIR_VERSION_INTERFACE_VERSION_CONTAINED_TYPE); ?>, %s seen',
is_object($data[self::<?php echo $propConst; ?>]) ? get_class($data[self::<?php echo $propConst; ?>]) : gettype($data[self::<?php echo $propConst; ?>])
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,
));
} else {
$typeClass = $data[self::<?php echo $propConst; ?>];
}
$type-><?php echo $setter; ?>($typeClass);
<?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,
));
<?php endif; ?>
}
<?php else : ?>
if (isset($json[self::<?php echo $propConst; ?>]) || array_key_exists(self::<?php echo $propConst; ?>, $json)) {
<?php if ($property->isCollection()) : ?>
if (is_array($json[self::<?php echo $propConst; ?>])) {
foreach($json[self::<?php echo $propConst; ?>] as $v) {
if ($v instanceof <?php echo $propTypeClass; ?>) {
$type-><?php echo $setter; ?>($v);
} else {
$type-><?php echo $setter; ?>(<?php echo $propTypeClass; ?>::jsonUnserialize($v));
}
}
} elseif ($json[self::<?php echo $propConst; ?>] instanceof <?php echo $propTypeClass; ?>) {
$type-><?php echo $setter; ?>($json[self::<?php echo $propConst; ?>]);
} else {
$type-><?php echo $setter; ?>(new <?php echo $propTypeClass; ?>($json[self::<?php echo $propConst; ?>]));
$vs = $json[self::<?php echo $propConst; ?>];
if (!is_int(key($vs))) {
$vs = [$vs];
}
<?php else : ?>
if ($json[self::<?php echo $propConst; ?>] instanceof <?php echo $propTypeClass; ?>) {
$type-><?php echo $setter; ?>($json[self::<?php echo $propConst; ?>]);
} else {
$type-><?php echo $setter; ?>(<?php echo $propTypeClass; ?>::jsonUnserialize($json[self::<?php echo $propConst; ?>]));
foreach($vs as $v) {
$type-><?php echo $setter; ?>(<?php echo $propTypeClass; ?>::jsonUnserialize(
json: $v,
config: $config,
));
}
<?php else : ?>
$type-><?php echo $setter; ?>(<?php echo $propTypeClass; ?>::jsonUnserialize(
json: $json[self::<?php echo $propConst; ?>],
config: $config,
));
<?php endif; ?>
}
<?php endif;
Expand Down

0 comments on commit 56313c9

Please sign in to comment.