Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type replacer #525

Merged
merged 1 commit into from
Jun 11, 2024
Merged

Type replacer #525

merged 1 commit into from
Jun 11, 2024

Conversation

veewee
Copy link
Contributor

@veewee veewee commented Jun 11, 2024

By default, a "date" type from the XSD namespace http://www.w3.org/2001/XMLSchema will be converted to a DateTimeImmutable object.
However, if you configure an encoder that does not support DateTimeImmutable,
you might want to replace it with a int type that represents the amount of seconds since the unix epoch.

This can be configured in the client configuration:

use Phpro\SoapClient\CodeGenerator\Config\Config;
use Phpro\SoapClient\Soap\Metadata\Manipulators\TypeReplacer\TypeReplacers;

return Config::create()
    //...
    ->setTypeReplacements(
        TypeReplacers::defaults()
            ->add(new MyDateReplacer())
    )
    // ...

The MyDateReplacer class should implement the TypeReplacerInterface and should return the correct type for the given type.

use Phpro\SoapClient\Soap\Metadata\Manipulators\TypeReplacer\TypeReplacer;
use Soap\Engine\Metadata\Model\XsdType;
use Soap\WsdlReader\Metadata\Predicate\IsOfType;use Soap\Xml\Xmlns;

final class MyDateReplacer implements TypeReplacer
{
    public function __invoke(XsdType $xsdType) : XsdType
    {
        $check = new IsOfType(Xmlns::xsd()->value(), 'date');
        if (!$check($xsdType)) {
            return $xsdType;
        }

        return $xsdType->copy('int')->withBaseType('int');
    }
}

This way, the generated code will use the int type instead of the DateTimeImmutable type for the date type in the XSD.

The TypeReplacers contain a default set of type replacements that are being used to improve the generated code:

  • array for SOAP 1.1 and 1.2 Arrays
  • object for SOAP 1.1 Objects
  • array for Apache Map types

@veewee veewee changed the base branch from v3.x to v4.x June 11, 2024 10:44
@veewee veewee added this to the 4.0.0 milestone Jun 11, 2024
@veewee veewee merged commit ae2a668 into phpro:v4.x Jun 11, 2024
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant