goetas/xsd2php

Generated YAML files use incorrect (?) DateTime class

Closed this issue · 4 comments

I extracted the XML Schema from the WSDL described on this page

When generating the PHP Classes, normal \DateTime classes are used, as expected.

When generating the YAML files to have JSM Serializer serialize the PHP objects to XML, the following type is used for a DateTime:

type: Goetas\Xsd\XsdToPhp\XMLSchema\DateTime

Caused by: https://github.com/goetas/xsd2php/blob/master/lib/Jms/YamlConverter.php#L32

During serialization of the object to XML, this causes a 'class not found' exception.

It seems this is intended behaviour, but why? The JMS Serializer can deal with the native \DateTime class right?

Changing the YAML file fixed the issue, but I guess I miss some detail here ;).

Goetas\Xsd\XsdToPhp\XMLSchema\DateTime is just the class alias for conversion. The resulting object will be a DateTime anyway.

Did you configured correctly the serializer? (see readme.md)

use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\Handler\HandlerRegistryInterface;

use Goetas\Xsd\XsdToPhp\Jms\Handler\BaseTypesHandler;
use Goetas\Xsd\XsdToPhp\Jms\Handler\XmlSchemaDateHandler;

$serializerBuilder = SerializerBuilder::create();
$serializerBuilder->addMetadataDir('metadata dir', 'DemoNs'); // metadata di
$serializerBuilder->configureHandlers(function (HandlerRegistryInterface $handler) use ($serializerBuilder) {
    $serializerBuilder->addDefaultHandlers();
    $handler->registerSubscribingHandler(new BaseTypesHandler()); // !! XMLSchema List handling
    $handler->registerSubscribingHandler(new XmlSchemaDateHandler()); // !! date handling
});

The JMS Serializer can deal with the native \DateTime class right?

Jms is not able to handle the multi-format date as xsd:dateTime, due to this we need a custom handler

Ok, thanks for the feedback! Missed reading that part in detail, sorry.

We used Composer to install the xsd2php project, typically only in the require-dev section, since generation of the PHP and YAML files would not be something to do in other environments than development.

However, since the handlers are now also required during (de)serialization (which DOES occur in staging and production environments when communicating with various APIs), we need to add the xsd2php project to the common require section of composer:

  • Goetas\Xsd\XsdToPhp\Jms\Handler\BaseTypesHandler;
  • Goetas\Xsd\XsdToPhp\Jms\Handler\XmlSchemaDateHandler;

Might it be an idea to divide the xsd2php project into a pure generation oriented part (dev only) and a more serialization oriented part? The xsd2php project depends on a number of project we would like to keep out of our production environment (zendframework/zend-code, zendframework/zend-eventmanager, etc).

Or maybe even create a Pull Request to adopt these handlers in https://github.com/schmittjoh/serializer/ ...

Might it be an idea to divide the xsd2php project into a pure generation oriented part (dev only) and a more serialization oriented part?

This is a good idea! @holtkamp Can you create a issue for this?

Or maybe even create a Pull Request to adopt these handlers in https://github.com/schmittjoh/serializer/

Unfortunately the project does not look so active... i had 2/3 pending pull requests for year.. and then i decided to fork it..