Zend Framework 2 SOAP AutoDiscover and complex types

与世无争的帅哥 提交于 2019-12-04 03:12:20

I did something similar and this is a sample code:

/* code.... */
if (array_key_exists('wsdl', $this->request->getQuery()) || array_key_exists('WSDL', $this->request->getQuery())) {

                    $auto = new \Zend\Soap\AutoDiscover(new \Zend\Soap\Wsdl\ComplexTypeStrategy\ArrayOfTypeSequence());

                    $auto->setClass($controllerClassName);
                    $auto->setUri(sprintf('%s://%s%s', \Application\Bootstrap::getServiceManager()->get('config')[APPLICATION_ENV]['webServer']['protocol'],
                                                     $this->request->getUri()->getHost() , $this->request->getUri()->getPath()));
                    $auto->setServiceName(ucfirst($this->request->getModuleName()) . ucfirst($this->request->getControllerName()));

                    header('Content-type: application/xml');

                    echo $auto->toXML();



                } elseif (count($this->request->getQuery()) == 0) {

                    $this->preDispatch();

                    $wsdl = sprintf('%s://%s%s?wsdl', \Application\Bootstrap::getServiceManager()->get('config')[APPLICATION_ENV]['webServer']['protocol'],
                                                     $this->request->getUri()->getHost() , $this->request->getUri()->getPath());

                    $soapServer = new \Zend\Soap\Server($wsdl);
                    $soapServer->setClass($controllerClassName);
                    $soapServer->handle();
                }

/* code */

This is a fragment of the function signature of one of the classes that the autodiscover will generate the wsdl based on the annotations:

/**
 * Allows to search for a patient based on the patient id
 *
 * @param int $id
 * @return \ViewModels\PatientViewModel
 * @throws \Application\Exception
 */
protected function searchPatientById($id) {
 /* .... code */

This is the class \ViewModels\PatientViewModel and \ViewModel\DiagnosisViewModel Notice here how i used the annotations to declare that a field conatins an array of a complextype, and then how that is translated as ArrayOfDiagnosisViewModel on the wsdl

    namespace ViewModels;

    class PatientViewModel {

        /**
         * @var int
         * */
        public $id;

        /**
         * @var string
         * */
        public $firstname;

        /**
         * @var string
         * */
        public $lastname;

        /**
         *** @var \ViewModels\DiagnosisViewModel[]**
         * */
        public $diagnosis;

    }

class DiagnosisViewModel {

    /**
     * @var int
     */
    public $id;

    /**
     * @var string
     */
    public $name;

}

And this is the WSDL generated

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://soa.local/soap/Sample/Main" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" name="SampleMain" targetNamespace="http://soa.local/soap/Sample/Main">
    <types>
        <xsd:schema targetNamespace="http://soa.local/soap/Sample/Main">
            <xsd:complexType name="DiagnosisViewModel">
                <xsd:all>
                    <xsd:element name="id" type="xsd:int" nillable="true"/>
                    <xsd:element name="name" type="xsd:string" nillable="true"/>
                </xsd:all>
            </xsd:complexType>
            **<xsd:complexType name="ArrayOfDiagnosisViewModel">
                <xsd:sequence>
                    <xsd:element name="item" type="tns:DiagnosisViewModel" minOccurs="0" maxOccurs="unbounded"/>
                </xsd:sequence>
            </xsd:complexType>**
            <xsd:complexType name="PatientViewModel">
                <xsd:all>
                    <xsd:element name="id" type="xsd:int" nillable="true"/>
                    <xsd:element name="firstname" type="xsd:string" nillable="true"/>
                    <xsd:element name="lastname" type="xsd:string" nillable="true"/>
                    <xsd:element name="diagnosis" type="tns:ArrayOfDiagnosisViewModel" nillable="true"/>
                </xsd:all>
            </xsd:complexType>
        </xsd:schema>
    </types>
    <portType name="SampleMainPort">
        <operation name="searchPatientById">
            <documentation>Allows to search for a patient based on the patient id</documentation>
            <input message="tns:searchPatientByIdIn"/>
            <output message="tns:searchPatientByIdOut"/>
        </operation>
    </portType>
    <binding name="SampleMainBinding" type="tns:SampleMainPort">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="searchPatientById">
            <soap:operation soapAction="http://soa.local/soap/Sample/Main#searchPatientById"/>
            <input>
                <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://soa.local/soap/Sample/Main"/>
            </input>
            <output>
                <soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://soa.local/soap/Sample/Main"/>
            </output>
        </operation>
    </binding>
    <service name="SampleMainService">
        <port name="SampleMainPort" binding="tns:SampleMainBinding">
            <soap:address location="http://soa.local/soap/Sample/Main"/>
        </port>
    </service>
    <message name="searchPatientByIdIn">
        <part name="id" type="xsd:int"/>
    </message>
    <message name="searchPatientByIdOut">
        <part name="return" type="tns:PatientViewModel"/>
    </message>
</definitions>

NOTICE THAT JUST BY CHANGING THE STRATEGY AND THE RIGHT DOCBLOCKS ANNOTATIONS YOU CAN ACHIEVE THAT.

HOPE THIS SNIPPETS CAN HELP YOU TO FIND A SOLUTION.

Just in case anyone comes here from the Google and wants to know what the issue is.

I found out it's because the docblock parser is not smart enough to pick up classes that are used in the unqualified way. So for example:

<?php

use myNameSpace\MyClass;

class Foo
{
    /**
     * @var MyClass
     */ 
    public $customer;
}

Does not work. When Zend parses it, it will not parse the namespace, just the classname "MyClass", and then fail.

You must do it like this:

<?php

class Foo
{
    /**
     * The backspace qualifier is only necessary if this is namespaced.
     * @var \myNameSpace\MyClass
     */ 
    public $customer;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!