php JMS序列化任何不同的属性

mqxuamgl  于 2023-08-02  发布在  PHP
关注(0)|答案(1)|浏览(95)

我需要与一些第三方API通过XML像这样

<App:Envelope xmlns:App="http://somenamespace.com/version/1_0">
   <Head/>
   <Body>
     <AppData>
       <method:Auth xmlns:method="http://somenamespace/method/1_0">
          <Login>MyUserName</Login>
          <Password>123456</Password>
       </method:Auth>
     </AppData>
   </Body>
</App:Envelope>

字符串
我使用goetasxsd2php为JMS序列化程序生成类和元数据
这是工作正常,但我的AppData类有不同的类型取决于请求类型,我不知道如何序列化它。我得到了AppData节为空的XML
Envelope.php

class Envelope
{
    private $header = null;
    private $body = null;
    public function getHeader()
    {
        return $this->header;
    }
    public function setHeader(\HeaderCType $header)
    {
        $this->header = $header;
        return $this;
    }
    public function getBody()
    {
        return $this->body;
    }
    public function setBody(\BodyCType $body)
    {
        $this->body = $body;
        return $this;
    }
    public function __construct()
    {
        $this->body = new BodyCType();
        $this->header = new HeaderCType();
    }

    public function createAuth($login, $password){
        $app = new AppDataType();
        $app->setAuth(new Auth($login, $password));
        $this->body->setAppData($app);
    }           
}


AppDataType.php

class AppDataType
    {

    }


我尝试使用订阅处理程序为anyType从goetas xsd2php的例子,但不能理解如何序列化验证部分?任何人都可以给我一个真实的的代码示例,我怎么能做到这一点?

a11xaf1n

a11xaf1n1#

解决了一个问题

public function serializeAnyType(XmlSerializationVisitor $visitor, $data, array $type, Context $context)
{

    foreach ($data as $key => $value) {

        $assestor = new \JMS\Serializer\Accessor\DefaultAccessorStrategy();
        $metadata = $context->getMetadataFactory()->getMetadataForClass(get_class($value));
        $visitor->getCurrentNode()->appendChild($visitor->createRoot($metadata));
        foreach ($metadata->propertyMetadata as $property) {
            $val = $assestor->getValue($data->{$key}, $property, $context);
            $visitor->visitProperty($property, $val);
        }
    }
}

字符串

相关问题