diff --git a/src/Utilities/TypeHintUtils.php b/src/Utilities/TypeHintUtils.php index ac91b539..65ad64ab 100644 --- a/src/Utilities/TypeHintUtils.php +++ b/src/Utilities/TypeHintUtils.php @@ -30,77 +30,57 @@ 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): string + public static function primitivePHPValueTypeHint(Version $version, + PrimitiveTypeEnum $primitiveType, + bool $nullable, + bool $stringable = false): string { - // this assumes the property's value type is a primiive. - // it will bomb if not. - return sprintf( - '%s%s', - $nullable ? 'null|' : '', - $primitiveType->getPHPReturnValueTypeHint() - ); - } - - public static function primitivePHPReturnValueTypeDoc(Version $version, PrimitiveTypeEnum $primitiveType, bool $nullable, bool $asCollection): string - { - $hint = $primitiveType->getPHPReturnValueTypeHint(); - - if ($asCollection) { - return sprintf('%s[]', $hint); - } - - if ($nullable) { - return sprintf('null|%s', $hint); - } - - return $hint; + return ($nullable ? 'null|' : '') + . ($stringable ? 'string|' : '') + . $primitiveType->getPHPReturnValueTypeHint(); } /** * @param \DCarbone\PHPFHIR\Version $version * @param \DCarbone\PHPFHIR\Enum\PrimitiveTypeEnum $primitiveType * @param bool $nullable - * @param bool $asCollection + * @param bool $asArray + * @param bool $asVariadic * @return string */ - public static function primitivePHPValueTypeSetterDoc(Version $version, PrimitiveTypeEnum $primitiveType, bool $nullable, bool $asCollection): string + public static function primitivePHPValueTypeSetterDoc(Version $version, + PrimitiveTypeEnum $primitiveType, + bool $nullable, + bool $asArray = false, + bool $asVariadic = false): string { + if ($asArray && $asVariadic) { + throw new \InvalidArgumentException('Cannot set both array and variadic'); + } + if ($asVariadic && $nullable) { + throw new \InvalidArgumentException('Cannot set both nullable and variadic'); + } + $hintTypes = $primitiveType->getPHPReceiveValueTypeHints(); - if ($asCollection) { + if ($asArray) { $hintTypes[] = array_map(function (string $v) { return sprintf('%s[]', $v); }, $hintTypes); - } else if ($nullable) { - array_unshift($hintTypes, 'null'); } - return implode('|', $hintTypes); - } + if ($nullable) { + array_unshift($hintTypes, 'null'); + } - /** - * @param \DCarbone\PHPFHIR\Version $version - * @param \DCarbone\PHPFHIR\Version\Definition\Property $property - * @param bool $nullable - * @return string - */ - public static function primitiveValuePropertyTypeHint(Version $version, Property $property, bool $nullable): string - { - return self::primitivePHPValueTypeHint($version, $property->getMemberOf()->getPrimitiveType(), $nullable); - } + if ($asVariadic) { + return sprintf('...%s', implode('|', $hintTypes)); + } - /** - * @param \DCarbone\PHPFHIR\Version $version - * @param \DCarbone\PHPFHIR\Version\Definition\Property $property - * @param bool $nullable - * @param bool $asCollection - * @return string - */ - public static function primitiveValuePropertyTypeDoc(Version $version, Property $property, bool $nullable, bool $asCollection): string - { - return self::primitivePHPReturnValueTypeDoc($version, $property->getMemberOf()->getPrimitiveType(), $nullable, $asCollection); + return implode('|', $hintTypes); } /** @@ -109,7 +89,7 @@ public static function primitiveValuePropertyTypeDoc(Version $version, Property * @param bool $nullable * @return string */ - public static function typeTypeHint(Version $version, Type $type, bool $nullable): string + public static function typeHint(Version $version, Type $type, bool $nullable): string { $tk = $type->getKind(); @@ -161,49 +141,52 @@ public static function typeSetterTypeHint(Version $version, Type $type, bool $nu } /** + * Compiles base array of hint components for a given FHIR type. + * * @param \DCarbone\PHPFHIR\Version $version * @param \DCarbone\PHPFHIR\Version\Definition\Type $type - * @param bool $nullable - * @param bool $asCollection - * @return string + * @param bool $fullyQualified + * @return array */ - public static function typeTypeDoc(Version $version, Type $type, bool $nullable, bool $asCollection): string + public static function buildBaseHintParts(Version $version, Type $type, bool $fullyQualified): array { - // if nullable, add to list of additional types - $types = []; - // fetch type's kind $tk = $type->getKind(); - // if this is an inline resource - if ($tk->isOneOf(TypeKindEnum::RESOURCE_INLINE, TypeKindEnum::RESOURCE_CONTAINER)) { - $types[] = $version->getFullyQualifiedName(true, PHPFHIR_VERSION_INTERFACE_VERSION_CONTAINED_TYPE); + if ($tk->isOneOf(TypeKindEnum::PRIMITIVE, TypeKindEnum::LIST)) { + $hintTypes = $type->getPrimitiveType()->getPHPReceiveValueTypeHints(); + } else if ($tk === TypeKindEnum::PRIMITIVE_CONTAINER) { + $ptp = $type->getProperties()->getProperty(PHPFHIR_VALUE_PROPERTY_NAME)->getValueFHIRType(); + $hintTypes = $ptp->getPrimitiveType()->getPHPReceiveValueTypeHints(); + array_merge($hintTypes, self::buildBaseHintParts($version, $ptp, $fullyQualified)); + } else if ($tk->isOneOf(TypeKindEnum::RESOURCE_INLINE, TypeKindEnum::RESOURCE_CONTAINER)) { + $hintTypes = [ + match ($fullyQualified) { + true => $version->getFullyQualifiedName(true, PHPFHIR_VERSION_INTERFACE_VERSION_CONTAINED_TYPE), + false => PHPFHIR_VERSION_INTERFACE_VERSION_CONTAINED_TYPE, + }, + ]; } else { - $types[] = $type->getFullyQualifiedClassName(true); + $hintTypes = [ + match ($fullyQualified) { + true => $type->getFullyQualifiedClassName(true), + false => $type->getClassName(), + }, + ]; } - // if this type is a collection, add [] to end of each element - if ($asCollection) { - $types = array_map(function (string $n): string { - return sprintf('%s[]', $n); - }, $types); - } - - // if this type is nullable, prepend list with 'null' - if ($nullable) { - array_unshift($types, 'null'); - } - - return implode('|', $types); + return array_unique($hintTypes); } /** + * Builds the type hint for a property declaration. + * * @param \DCarbone\PHPFHIR\Version $version * @param \DCarbone\PHPFHIR\Version\Definition\Property $property * @param bool $nullable * @return string */ - public static function propertyTypeHint(Version $version, Property $property, bool $nullable): string + public static function propertyDeclarationHint(Version $version, Property $property, bool $nullable): string { // if this proprety is a collection, the type hint must be a potentially nullable array if ($property->isCollection()) { @@ -215,64 +198,151 @@ public static function propertyTypeHint(Version $version, Property $property, bo // if null, the (hopefully) only possibility is that this is a value property for a primitive type if (null === $t) { - return self::primitiveValuePropertyTypeHint($version, $property, $nullable); + return self::primitivePHPValueTypeHint( + $version, + $property->getMemberOf()->getPrimitiveType(), + $nullable, + ); } // otherwise, hint as the underlying type - return self::typeTypeHint($version, $t, $nullable); + return self::typeHint($version, $t, $nullable); } /** + * Builds a TypeDoc hint for a property when returned from a method. + * * @param \DCarbone\PHPFHIR\Version $version * @param \DCarbone\PHPFHIR\Version\Definition\Property $property * @param bool $nullable * @return string */ - public static function propertyGetterTypeDoc(Version $version, Property $property, bool $nullable): string + public static function propertyGetterTypeDocHint(Version $version, + Property $property, + bool $nullable): string { - // determine if this property contains a FHIR type or a raw php type $t = $property->getValueFHIRType(); if (null === $t) { - return self::primitiveValuePropertyTypeDoc($version, $property, $nullable, $property->isCollection()); + return self::primitivePHPValueTypeHint( + $version, + $property->getMemberOf()->getPrimitiveType(), + $nullable, + ); } - return self::typeTypeDoc($version, $t, $nullable, $property->isCollection()); + $hintTypes = self::buildBaseHintParts($version, $t, false); + + if ($property->isCollection()) { + $hintTypes = array_map(fn(string $n) => "{$n}[]", $hintTypes); + } + + if ($nullable) { + array_unshift($hintTypes, 'null'); + } + + return implode('|', $hintTypes); } /** + * Builds a TypeDoc hint for a property when received as a parameter in a method. + * * @param \DCarbone\PHPFHIR\Version $version * @param \DCarbone\PHPFHIR\Version\Definition\Property $property - * @param bool $asCollection + * @param bool $nullable * @return string */ - public static function propertySetterTypeDoc(Version $version, Property $property, bool $asCollection): string + public static function buildConstructorParameterDocHint(Version $version, + Property $property, + bool $nullable): string { - // determine if this property contains a FHIR type or a raw php type $pt = $property->getValueFHIRType(); - if (null === $pt) { - return self::primitiveValuePropertyTypeDoc($version, $property, !$asCollection, $asCollection); + + if ($property->isValueProperty()) { + if ($property->getMemberOf()->getKind() !== TypeKindEnum::PRIMITIVE_CONTAINER) { + return self::primitivePHPValueTypeHint( + version: $version, + primitiveType: $property->getMemberOf()->getPrimitiveType(), + nullable: $nullable, + stringable: true, + ); + } + $hintTypes = [ + 'null', + ]; + } else { + $hintTypes = self::buildBaseHintParts($version, $pt, true); } + + $ptk = $pt->getKind(); - $hintTypes = ['null']; + if ($ptk->isOneOf(TypeKindEnum::PRIMITIVE_CONTAINER)) { + $vp = $pt->getProperties()->getProperty(PHPFHIR_VALUE_PROPERTY_NAME); + array_push( + $hintTypes, + $vp->getValueFHIRType()->getFullyQualifiedClassName(true), + $pt->getFullyQualifiedClassName(true), + ); + } + + if ($property->isCollection()) { + $hintTypes = array_map(fn(string $n) => "{$n}[]", $hintTypes); + } + + if ($nullable) { + array_unshift($hintTypes, 'null'); + } + + return implode('|', $hintTypes); + } + + /** + * @param \DCarbone\PHPFHIR\Version $version + * @param \DCarbone\PHPFHIR\Version\Definition\Property $property + * @param bool $nullable + * @return string + */ + public static function buildConstructorParameterHint(Version $version, + Property $property, + bool $nullable): string + { + $pt = $property->getValueFHIRType(); + if (null === $pt) { + return self::primitivePHPValueTypeHint( + version: $version, + primitiveType: $property->getMemberOf()->getPrimitiveType(), + nullable: $nullable, + stringable: true, + ); + } + + if ($property->isCollection()) { + return $nullable ? 'null|array' : 'array'; + } - if ($ptk === TypeKindEnum::PRIMITIVE_CONTAINER) { - $ptp = $pt->getProperties()->getProperty('value')->getValueFHIRType(); - $hintTypes = array_merge($hintTypes, $ptp->getPrimitiveType()->getPHPReceiveValueTypeHints()); + $hintTypes = self::buildBaseHintParts($version, $pt, false); + + $ptk = $pt->getKind(); + + if ($ptk->isOneOf(TypeKindEnum::PRIMITIVE_CONTAINER)) { + $vp = $pt->getProperties()->getProperty(PHPFHIR_VALUE_PROPERTY_NAME); array_push( $hintTypes, - self::typeTypeDoc($version, $ptp, false, $asCollection), - self::typeTypeDoc($version, $pt, false, $asCollection), + $vp->getValueFHIRType()->getClassName(), + $pt->getClassName(), ); - } else if ($ptk->isOneOf(TypeKindEnum::PRIMITIVE, TypeKindEnum::LIST)) { - $hintTypes = array_merge($hintTypes, $pt->getPrimitiveType()->getPHPReceiveValueTypeHints()); - $hintTypes[] = self::typeTypeDoc($version, $pt, false, $asCollection); - } else { - $hintTypes[] = self::typeTypeDoc($version, $pt, false, $asCollection); } - return implode('|', array_unique($hintTypes)); + if ($property->isCollection()) { + $hintTypes = array_map(fn(string $n) => "{$n}[]", $hintTypes); + } + + if ($nullable) { + array_unshift($hintTypes, 'null'); + } + + return implode('|', $hintTypes); } /** diff --git a/template/versions/types/class_default.php b/template/versions/types/class_default.php index 4e67f3e5..7c0a48d7 100644 --- a/template/versions/types/class_default.php +++ b/template/versions/types/class_default.php @@ -61,10 +61,10 @@ foreach ($type->getProperties()->getGenerator() as $property) : ?> /** - * @var + * @var */ - protected $getName(); ?> = isCollection() ? '[]' : 'null'; ?>; + protected $getName(); ?> = isCollection() ? '[]' : 'null'; ?>; isOneOf(TypeKindeNum::PRIMITIVE, TypeKindEnum::LIST)) : $primitiveType = $type->getPrimitiveType(); - if (null !== $parentType) : - // only define constructor if this parent has more than just a "value" property. + // only define constructor if this primitive does not have a parent. + if (null === $parentType) : ?> - if ($parentType->getKind() !== TypeKindeNum::PRIMITIVE && !$parentType->isValueContainer()) : ?> - /** - * getClassName(); ?> Constructor - * @param |array $value - */ - public function __construct(|array $value = null) - { - if (null === $value) { - parent::__construct(null); - } elseif (is_scalar($value)) { - parent::__construct(null); - $this->setValue($value); - } elseif (is_array($value)) { - parent::__construct($value); - if (array_key_exists(self::FIELD_VALUE, $value)) { - $this->setValue($value[self::FIELD_VALUE]); - } - } else { - throw new \InvalidArgumentException(sprintf( - '::__construct - $data expected to be null, getPHPValueTypes(); ?>, or array, %s seen', - gettype($value) - )); - } - } - /** * Constructor * @param $value */ - public function __construct( $value = null) + public function __construct( $value = null) { $this->setValue($value); } @@ -86,58 +57,37 @@ public function __construct( /** - * Constructor - * @param null|array $data - * @param |getClassName(); ?> $value + * Constructor BOOTY + * @param $value getAllPropertiesIndexedIterator() as $property) : if ($property->isValueProperty()) { continue; } ?> - * @param $getName(); ?> + * @param $getName(); ?> hasCommentContainerParent() || $type->isCommentContainer()) : ?> * @param null|array $fhirComments */ - public function __construct(null|array $data = null, - $value = nullgetAllPropertiesIndexedIterator() as $property) : + public function __construct( $value = nullgetAllPropertiesIndexedIterator() as $property) : if ($property->isValueProperty()) { continue; } ?>, - $getName(); ?> = nullhasCommentContainerParent() || $type->isCommentContainer()) : ?>, + $getName(); ?> = nullhasCommentContainerParent() || $type->isCommentContainer()) : ?>, null|array $fhirComments = null) { - parent::__construct(data: $datagetParentPropertiesIterator() as $property) : ?>, - getName(); ?>: $getName(); ?>hasCommentContainerParent()) : ?>, + parent::__construct(getParentPropertiesIterator() as $i => $property) : if ($i > 0) : ?>, + getName(); ?>: $getName(); ?>hasCommentContainerParent()) : ?>, fhirComments: $fhirComments); - - if (null === $data) { - return; - } - if (is_scalar($data) || $data instanceof getImportByType($valuePropertyType); ?>) { - $this->setValue($data); - return; - } - - parent::__construct($data); -hasCommentContainerParent() && $type->isCommentContainer()) : ?> +hasCommentContainerParent() && $type->isCommentContainer()) : ?> if (null !== $fhirComments && [] !== $fhirComments) { $this->_setFHIRComments($fhirComments); - } else if (isset($data[::JSON_FIELD_FHIR_COMMENTS])) { - if (is_array($data[::JSON_FIELD_FHIR_COMMENTS])) { - $this->_setFHIRComments($data[::JSON_FIELD_FHIR_COMMENTS]); - } elseif (is_string($data[::JSON_FIELD_FHIR_COMMENTS])) { - $this->_addFHIRComment($data[::JSON_FIELD_FHIR_COMMENTS]); - } } getGenerator() as $property) : - if ($property->getOverloadedProperty()) { - continue; - } - echo require_with( PHPFHIR_TEMPLATE_VERSION_TYPES_CONSTRUCTORS_DIR . DIRECTORY_SEPARATOR . 'default_property_setter_call.php', [ @@ -150,10 +100,9 @@ public function __construct(null|array $data = null, /** * Constructor - * @param null|array $data isValueContainer()) : ?> - * @param $value = null + * @param $value = null getAllPropertiesIndexedIterator() as $property) : $pt = $property->getValueFHIRType(); @@ -161,19 +110,17 @@ public function __construct(null|array $data = null, continue; } ?> - * @param $getName(); ?> + * @param $getName(); ?> hasCommentContainerParent() && $type->isCommentContainer()) : ?> * @param null|array $fhirComments */ - public function __construct(null|array $data = nullisValueContainer()) : ?>, - $value = null -getAllPropertiesIndexedIterator() as $property) : + public function __construct(isValueContainer()) : echo TypeHintUtils::propertySetterTypeHint($version, $valueProperty, true);?> $value = nullgetAllPropertiesIndexedIterator() as $i => $property) : if ($type->isValueContainer() && $property->isValueProperty()) { continue; } - ?>, - $getName(); ?> = nullhasCommentContainerParent() && $type->isCommentContainer()) : ?>, + if ($type->isValueContainer() || $i > 0) : ?>, + $getName(); ?> = nullhasCommentContainerParent() && $type->isCommentContainer()) : ?>, null|array $fhirComments = null) { if (null === $data || [] === $data) { diff --git a/template/versions/types/methods/constructors/default_property_setter_call.php b/template/versions/types/methods/constructors/default_property_setter_call.php index a4399709..1a16fca5 100644 --- a/template/versions/types/methods/constructors/default_property_setter_call.php +++ b/template/versions/types/methods/constructors/default_property_setter_call.php @@ -21,29 +21,28 @@ /** @var \DCarbone\PHPFHIR\Version\Definition\Property $property */ $propertyType = $property->getValueFHIRType(); -$fieldConstantName = $property->getFieldConstantName(); +$propertyVarName = "\${$property->getName()}"; +$propertyTypeClassName = $propertyType->getClassName(); +$propertyFieldConst = $property->getFieldConstantName(); +$propertyFieldConstExt = $property->getFieldConstantExtensionName(); +$setter = $property->getSetterName(); $requireArgs = [ 'property' => $property ]; -ob_start(); - -if ($propertyType->getKind()->isOneOf(TypeKindEnum::PRIMITIVE, TypeKindEnum::LIST) || $propertyType->hasPrimitiveParent()) : - echo require_with( - __DIR__ . DIRECTORY_SEPARATOR . 'property_setter_primitive.php', - $requireArgs - ); -elseif ($propertyType->getKind() === TypeKindEnum::PRIMITIVE_CONTAINER || $propertyType->hasPrimitiveContainerParent() || $propertyType->isValueContainer()) : - echo require_with( - __DIR__ . DIRECTORY_SEPARATOR . 'property_setter_primitive_container.php', - $requireArgs - ); -else : - echo require_with( - __DIR__ . DIRECTORY_SEPARATOR . 'property_setter_default.php', - $requireArgs - ); -endif; - +ob_start(); ?> + if (null !== isCollection()) : ?> && [] !== ) { +isCollection()) : ?> + foreach( as $i => $v) { + if (!($v instanceof getValueFHIRType()->getClassName(); ?>)) { + [$i] = new getValueFHIRType()->getClassName(); ?>($v); + } + } + $this->setgetName(); ?>(...$v); + + $this->(); + + } +getValueFHIRType()->getClassName(); -$propertyFieldConst = $property->getFieldConstantName(); -$propertyFieldConstExt = $property->getFieldConstantExtensionName(); -$setter = $property->getSetterName(); - -ob_start(); ?> - if (isset($data[self::]) || array_key_exists(self::, $data)) { -isCollection()) : ?> - if (is_array($data[self::])) { - foreach($data[self::] as $v) { - if ($v instanceof ) { - $this->($v); - } else { - $this->(new ($v)); - } - } - } elseif ($data[self::] instanceof ) { - $this->($data[self::]); - } else { - $this->(new ($data[self::])); - } - - if ($data[self::] instanceof ) { - $this->($data[self::]); - } else { - $this->(new ($data[self::])); - } - - } - diff --git a/template/versions/types/properties/declaration.php b/template/versions/types/properties/declaration.php index 8a01374a..d891fc00 100644 --- a/template/versions/types/properties/declaration.php +++ b/template/versions/types/properties/declaration.php @@ -26,13 +26,13 @@ ob_start(); if ('' === $documentation) : ?> - /** @var */ + /** @var */ /** - * @var + * @var */ - protected $getName(); ?> = isCollection() ? '[]' : 'null'; ?>; + protected $getName(); ?> = isCollection() ? '[]' : 'null'; ?>; * - * @return + * @return */ - public function get(): + public function get(): { return $this->; @@ -96,7 +96,7 @@ public function getGenerator(): \Generator * - * @param $ + * @param $ * @param getFullyQualifiedName(true); ?> $xmlLocation * @return static @@ -216,7 +216,7 @@ public function set(array $ * - * @param $ + * @param $ * @return static */ diff --git a/template/versions/types/properties/methods/primitive.php b/template/versions/types/properties/methods/primitive.php index 91cc5004..b4ad1f94 100644 --- a/template/versions/types/properties/methods/primitive.php +++ b/template/versions/types/properties/methods/primitive.php @@ -27,7 +27,7 @@ ob_start(); ?> /** - * @return + * @return */ public function getValue(): diff --git a/template/versions/types/serialization/json.php b/template/versions/types/serialization/json.php index efff06af..b66e1dd0 100644 --- a/template/versions/types/serialization/json.php +++ b/template/versions/types/serialization/json.php @@ -26,7 +26,13 @@ ob_start(); if ($typeKind->isOneOf(TypeKindEnum::PRIMITIVE, TypeKindEnum::LIST)) : echo require_with( - PHPFHIR_TEMPLATE_VERSION_TYPES_SERIALIZATION_DIR . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR . 'primitive.php', + PHPFHIR_TEMPLATE_VERSION_TYPES_SERIALIZATION_DIR + . DIRECTORY_SEPARATOR + . 'json' + . DIRECTORY_SEPARATOR + . 'serialize' + . DIRECTORY_SEPARATOR + . 'primitive.php', [ 'version' => $version, 'type' => $type, @@ -34,7 +40,13 @@ ); else: echo require_with( - PHPFHIR_TEMPLATE_VERSION_TYPES_SERIALIZATION_DIR . DIRECTORY_SEPARATOR . 'json' . DIRECTORY_SEPARATOR . 'default.php', + PHPFHIR_TEMPLATE_VERSION_TYPES_SERIALIZATION_DIR + . DIRECTORY_SEPARATOR + . 'json' + . DIRECTORY_SEPARATOR + . 'serialize' + . DIRECTORY_SEPARATOR + . 'default.php', [ 'version' => $version, 'type' => $type, diff --git a/template/versions/types/serialization/json/default.php b/template/versions/types/serialization/json/serialize/default.php similarity index 100% rename from template/versions/types/serialization/json/default.php rename to template/versions/types/serialization/json/serialize/default.php diff --git a/template/versions/types/serialization/json/primitive.php b/template/versions/types/serialization/json/serialize/primitive.php similarity index 90% rename from template/versions/types/serialization/json/primitive.php rename to template/versions/types/serialization/json/serialize/primitive.php index afc9c003..66ec7be7 100644 --- a/template/versions/types/serialization/json/primitive.php +++ b/template/versions/types/serialization/json/serialize/primitive.php @@ -26,7 +26,7 @@ ob_start(); ?> /** - * @return getPrimitiveType(), true, false); ?> + * @return getPrimitiveType(), true); ?> */ public function jsonSerialize(): mixed diff --git a/template/versions/types/serialization/json/unserialize/default.php b/template/versions/types/serialization/json/unserialize/default.php new file mode 100644 index 00000000..e69de29b diff --git a/template/versions/types/methods/constructors/property_setter_primitive.php b/template/versions/types/serialization/json/unserialize/default_property_setter_call.php similarity index 52% rename from template/versions/types/methods/constructors/property_setter_primitive.php rename to template/versions/types/serialization/json/unserialize/default_property_setter_call.php index 1d01b346..94ac0dbd 100644 --- a/template/versions/types/methods/constructors/property_setter_primitive.php +++ b/template/versions/types/serialization/json/unserialize/default_property_setter_call.php @@ -16,12 +16,23 @@ * limitations under the License. */ +use DCarbone\PHPFHIR\Enum\TypeKindEnum; + /** @var \DCarbone\PHPFHIR\Version\Definition\Property $property */ +$propertyType = $property->getValueFHIRType(); +$propertyTypeClassName = $propertyType->getClassName(); $propertyFieldConst = $property->getFieldConstantName(); +$propertyFieldConstExt = $property->getFieldConstantExtensionName(); $setter = $property->getSetterName(); -ob_start(); ?> +$requireArgs = [ + 'property' => $property +]; + +ob_start(); + +if ($propertyType->getKind()->isOneOf(TypeKindEnum::PRIMITIVE, TypeKindEnum::LIST) || $propertyType->hasPrimitiveParent()) :?> if (isset($data[self::]) || array_key_exists(self::, $data)) { isCollection()) : ?> if (is_array($data[self::])) { @@ -45,4 +56,36 @@ } getKind() === TypeKindEnum::PRIMITIVE_CONTAINER || $propertyType->hasPrimitiveContainerParent() || $propertyType->isValueContainer()) : + echo require_with( + __DIR__ . DIRECTORY_SEPARATOR . 'property_setter_primitive_container.php', + $requireArgs + ); +else : ?> + if (isset($data[self::]) || array_key_exists(self::, $data)) { +isCollection()) : ?> + if (is_array($data[self::])) { + foreach($data[self::] as $v) { + if ($v instanceof ) { + $this->($v); + } else { + $this->(new ($v)); + } + } + } elseif ($data[self::] instanceof ) { + $this->($data[self::]); + } else { + $this->(new ($data[self::])); + } + + if ($data[self::] instanceof ) { + $this->($data[self::]); + } else { + $this->(new ($data[self::])); + } + + } +::JSON_FIELD_FHIR_COMMENTS])) { + if (is_array($data[::JSON_FIELD_FHIR_COMMENTS])) { + $this->_setFHIRComments($data[::JSON_FIELD_FHIR_COMMENTS]); + } elseif (is_string($data[::JSON_FIELD_FHIR_COMMENTS])) { + $this->_addFHIRComment($data[::JSON_FIELD_FHIR_COMMENTS]); + } + } \ No newline at end of file diff --git a/template/versions/types/serialization/json/unserialize/property_setter_primitive_container.php b/template/versions/types/serialization/json/unserialize/property_setter_primitive_container.php new file mode 100644 index 00000000..fd41260b --- /dev/null +++ b/template/versions/types/serialization/json/unserialize/property_setter_primitive_container.php @@ -0,0 +1,66 @@ +getValueFHIRType()->getClassName(); +$propertyFieldConst = $property->getFieldConstantName(); +$propertyFieldConstExt = $property->getFieldConstantExtensionName(); +$setter = $property->getSetterName(); + +// these types are a pain in the ass + +ob_start(); ?> + if (isset($data[self::]) || isset($data[self::]) || array_key_exists(self::, $data) || array_key_exists(self::, $data)) { + $value = $data[self::] ?? null; + $ext = (isset($data[self::]) && is_array($data[self::])) ? $data[self::] : []; + if (null !== $value) { + if ($value instanceof ) { + $this->($value); + } else isCollection()) : ?>if (is_array($value)) { + foreach($value as $i => $v) { + if ($v instanceof ) { + $this->($v); + } else { + $iext = (isset($ext[$i]) && is_array($ext[$i])) ? $ext[$i] : []; + if (is_array($v)) { + $this->(new (array_merge($v, $iext))); + } else { + $this->(new ([::FIELD_VALUE => $v] + $iext)); + } + } + } + } elseif (is_array($value)) { + $this->(new (array_merge($ext, $value))); + } else { + $this->(new ([::FIELD_VALUE => $value] + $ext)); + } + } elseif ([] !== $ext) { +isCollection()) : ?> + foreach($ext as $iext) { + $this->(new ($iext)); + } + + $this->(new ($ext)); + + } else { + $this->(new (null)); + } + } + diff --git a/template/versions/types/serialization/json/unserialize/resource_container_property_setter_call.php b/template/versions/types/serialization/json/unserialize/resource_container_property_setter_call.php new file mode 100644 index 00000000..a9f54d3a --- /dev/null +++ b/template/versions/types/serialization/json/unserialize/resource_container_property_setter_call.php @@ -0,0 +1,68 @@ +getConfig(); +$version = $type->getVersion(); +$typeClassName = $type->getClassName(); +$propertyName = $property->getName(); +$propertyFieldConst = $property->getFieldConstantName(); +$propertyType = $property->getValueFHIRType(); +$setter = ($property->isCollection() ? 'add' : 'set') . ucfirst($propertyName); + +ob_start(); ?> + if (isset($data[self::])) { +isCollection()) : ?> + if (is_array($data[self::])) { + if (is_int(key($data[self::]))) { + $this->set($data[self::]); + } else { + $typeClassName = ::getContainedTypeClassNameFromArray($data[self::]); + $d = $data[self::]; + unset($d[::JSON_FIELD_RESOURCE_TYPE]); + $this->(new $typeClassName($d)); + } + } elseif (!is_object($data[self::]) || !($data[self::] instanceof )) { + throw new \InvalidArgumentException(sprintf( + ' - Field "" must be an array of objects implementing getFullyQualifiedName(true, PHPFHIR_VERSION_INTERFACE_VERSION_CONTAINED_TYPE); ?>, value of type %s seen', + is_object($data[self::]) ? get_class($data[self::]) : gettype($data[self::]) + )); + } else { + $this->($data[self::]); + } + + if (is_array($data[self::])) { + $typeClassName = ::getContainedTypeClassNameFromArray($data[self::]); + $d = $data[self::]; + unset($d[::JSON_FIELD_RESOURCE_TYPE]); + $typeClass = new $typeClassName($d); + } else if (!is_object($data[self::]) || !($data[self::] instanceof )) { + throw new \InvalidArgumentException(sprintf( + ' - Field "" must be an array or object implementing getFullyQualifiedName(true, PHPFHIR_VERSION_INTERFACE_VERSION_CONTAINED_TYPE); ?>, %s seen', + is_object($data[self::]) ? get_class($data[self::]) : gettype($data[self::]) + )); + } else { + $typeClass = $data[self::]; + } + $this->($typeClass); + + } +