Skip to content

Conversation

@akayumeru
Copy link

@akayumeru akayumeru commented Aug 12, 2024

Add a xsi:type resolve method to the Element Context Enhancer in order to properly resolve types for XML generation

Q A
Type improvement
BC Break yes
Fixed issues phpro/soap-client#539

Summary

To resolve type for xsi:type attribute I needed a value that I am passing to Encoder. That's why I suggesting to add resolveXsiType method in Element Context Enhancer interface.

Usage example

    public function resolveXsiType(Context $context, mixed $value): Context
    {
        $xsd = Xmlns::xsd()->value();
        $resolvedType = null;

        if (is_a($value, DateTime::class)) {
            $resolvedType = $context->type->copy('dateTime')
                ->withXmlTypeName('dateTime')
                ->withXmlNamespaceName($context->namespaces->lookupNameFromNamespace($xsd)->unwrap())
                ->withXmlNamespace($xsd);
        }

        if (is_a($value, Date::class)) {
            $resolvedType = $context->type->copy('date')
                ->withXmlTypeName('date')
                ->withXmlNamespaceName($context->namespaces->lookupNameFromNamespace($xsd)->unwrap())
                ->withXmlNamespace($xsd);
        }

        if ($resolvedType !== null) {
            return $context->withType($resolvedType);
        }

        return $context;
    }

Add a xsi:type resolve method to the Element Context Enhancer in order to properly resolve types for XML generation
@akayumeru
Copy link
Author

I added usage example

@veewee
Copy link
Member

veewee commented Aug 13, 2024

Thanks for the PR.

I'm not sure it's a good idea to always force people to implement resolveXsiType when enhancing the element encoders context.

Will play around with it to see if there are better alternatives once I'm back from vacation in a few weeks.

@veewee
Copy link
Member

veewee commented Aug 21, 2024

@LazyTechwork

I've provided an alternative PR to solve this issue:
#22

I'm not completely sure it's the best way to go. Care to take a look at it and play around with it?
Any feedback is welcome.

@veewee
Copy link
Member

veewee commented Aug 30, 2024

Thanks for the PR and pointing out the issue.
I decided to go for #22

@veewee veewee closed this Aug 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants