Просмотр исходного кода

ZF-6742 - Applied patch by Fabien Crespel which implements support for Objcets and Arrays of Objects in ArrayOfTypeSequence WSDL generation strategy

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@16935 44c647ce-9c0f-0410-b52a-842ac1e357ba
beberlei 16 лет назад
Родитель
Сommit
ebb6ab3638

+ 3 - 3
documentation/manual/en/module_specs/Zend_Soap_Wsdl.xml

@@ -370,9 +370,9 @@ $soapMyClassType = $wsdl->getType('MyClass');
                 </listitem>
                 <listitem>
                     <para>Class <classname>Zend_Soap_Wsdl_Strategy_ArrayOfTypeSequence</classname>: This strategy allows
-                    to specify return parameters of the type: <code>int[]</code> or <code>string[]</code>. It can only
-                    handle simple PHP types such as int, string, boolean, float and so on, but allows to specify
-                    nested arrays of arrays of type.</para>
+                    to specify return parameters of the type: <code>int[]</code> or <code>string[]</code>. As of
+                    Zend Framework version 1.9 it can handle both simple PHP types such as int, string, boolean, float
+                    aswell as objects and arrays of objects.</para>
                 </listitem>
                 <listitem>
                     <para>Class <classname>Zend_Soap_Wsdl_Strategy_ArrayOfTypeComplex</classname>: This strategy allows

+ 10 - 17
library/Zend/Soap/Wsdl/Strategy/ArrayOfTypeSequence.php

@@ -19,9 +19,9 @@
  * @version    $Id$
  */
 
-require_once "Zend/Soap/Wsdl/Strategy/Abstract.php";
+require_once "Zend/Soap/Wsdl/Strategy/DefaultComplexType.php";
 
-class Zend_Soap_Wsdl_Strategy_ArrayOfTypeSequence extends Zend_Soap_Wsdl_Strategy_Abstract
+class Zend_Soap_Wsdl_Strategy_ArrayOfTypeSequence extends Zend_Soap_Wsdl_Strategy_DefaultComplexType
 {
     /**
      * Add an unbounded ArrayOfType based on the xsd:sequence syntax if type[] is detected in return value doc comment.
@@ -37,7 +37,7 @@ class Zend_Soap_Wsdl_Strategy_ArrayOfTypeSequence extends Zend_Soap_Wsdl_Strateg
             $singularType = $this->_getSingularType($type);
 
             for($i = 1; $i <= $nestedCounter; $i++) {
-                $complexTypeName = $this->_getTypeNameBasedOnNestingLevel($singularType, $i);
+                $complexTypeName = substr($this->_getTypeNameBasedOnNestingLevel($singularType, $i), 4);
                 $childTypeName = $this->_getTypeNameBasedOnNestingLevel($singularType, $i-1);
 
                 $this->_addElementFromWsdlAndChildTypes($complexTypeName, $childTypeName);
@@ -46,11 +46,12 @@ class Zend_Soap_Wsdl_Strategy_ArrayOfTypeSequence extends Zend_Soap_Wsdl_Strateg
             $this->getContext()->addType($complexTypeName);
 
             return "tns:$complexTypeName";
+        } else if (!in_array($type, $this->getContext()->getTypes())) {
+            // New singular complex type
+            return parent::addComplexType($type);
         } else {
-            require_once "Zend/Soap/Wsdl/Exception.php";
-            throw new Zend_Soap_Wsdl_Exception(sprintf(
-                'ArrayOfTypeSequence Strategy does not allow for complex types that are not in @return type[] syntax. "%s" type was specified.', $type
-            ));
+            // Existing complex type
+            return $this->getContext()->getType($type);
         }
     }
 
@@ -70,7 +71,7 @@ class Zend_Soap_Wsdl_Strategy_ArrayOfTypeSequence extends Zend_Soap_Wsdl_Strateg
             $prefix = str_repeat("ArrayOf", $level);
             $xsdType = $this->_getStrippedXsdType($singularType);
             $arrayType = $prefix.$xsdType;
-            return $arrayType;
+            return "tns:$arrayType";
         }
     }
 
@@ -95,14 +96,6 @@ class Zend_Soap_Wsdl_Strategy_ArrayOfTypeSequence extends Zend_Soap_Wsdl_Strateg
     protected function _getSingularType($type)
     {
         $singulartype = $this->getContext()->getType(str_replace("[]", "", $type));
-
-        if(substr($singulartype, 0, 4) != "xsd:") {
-            require_once "Zend/Soap/Wsdl/Exception.php";
-            throw new Zend_Soap_Wsdl_Exception(sprintf(
-                'ArrayOfTypeSequence Strategy works only with arrays of simple types like int, string, boolean, not with "%s".'.
-                'You may use Zend_Soap_Wsdl_Strategy_ArrayOfTypeComplex for more complex types.', $type
-            ));
-        }
         return $singulartype;
     }
 
@@ -147,4 +140,4 @@ class Zend_Soap_Wsdl_Strategy_ArrayOfTypeSequence extends Zend_Soap_Wsdl_Strateg
             $this->getContext()->addType($arrayType);
         }
     }
-}
+}

+ 34 - 21
tests/Zend/Soap/Wsdl/ArrayOfTypeSequenceStrategyTest.php

@@ -78,7 +78,7 @@ class Zend_Soap_Wsdl_ArrayOfTypeSequenceStrategyTest extends PHPUnit_Framework_T
 
         // Check for ArrayOfArrayOfString
         $this->assertContains(
-            '<xsd:complexType name="ArrayOfArrayOfString"><xsd:sequence><xsd:element name="item" type="ArrayOfString" minOccurs="0" maxOccurs="unbounded"/></xsd:sequence></xsd:complexType>',
+            '<xsd:complexType name="ArrayOfArrayOfString"><xsd:sequence><xsd:element name="item" type="tns:ArrayOfString" minOccurs="0" maxOccurs="unbounded"/></xsd:sequence></xsd:complexType>',
             $wsdl
         );
         // Check for ArrayOfString
@@ -97,12 +97,12 @@ class Zend_Soap_Wsdl_ArrayOfTypeSequenceStrategyTest extends PHPUnit_Framework_T
 
         // Check for ArrayOfArrayOfArrayOfString
         $this->assertContains(
-            '<xsd:complexType name="ArrayOfArrayOfArrayOfString"><xsd:sequence><xsd:element name="item" type="ArrayOfArrayOfString" minOccurs="0" maxOccurs="unbounded"/></xsd:sequence></xsd:complexType>',
+            '<xsd:complexType name="ArrayOfArrayOfArrayOfString"><xsd:sequence><xsd:element name="item" type="tns:ArrayOfArrayOfString" minOccurs="0" maxOccurs="unbounded"/></xsd:sequence></xsd:complexType>',
             $wsdl
         );
         // Check for ArrayOfArrayOfString
         $this->assertContains(
-            '<xsd:complexType name="ArrayOfArrayOfString"><xsd:sequence><xsd:element name="item" type="ArrayOfString" minOccurs="0" maxOccurs="unbounded"/></xsd:sequence></xsd:complexType>',
+            '<xsd:complexType name="ArrayOfArrayOfString"><xsd:sequence><xsd:element name="item" type="tns:ArrayOfString" minOccurs="0" maxOccurs="unbounded"/></xsd:sequence></xsd:complexType>',
             $wsdl
         );
         // Check for ArrayOfString
@@ -112,33 +112,46 @@ class Zend_Soap_Wsdl_ArrayOfTypeSequenceStrategyTest extends PHPUnit_Framework_T
         );
     }
 
-    public function testSpecifyingNonSimpleArrayTypeThrowsException()
+
+    public function testAddComplexTypeObject()
     {
-        try {
-            $return = $this->wsdl->addComplexType('Zend_Soap_Wsdl_SequenceTest[]');
-            $this->fail();
-        } catch(Zend_Soap_Wsdl_Exception $e) {
-            
-        }
+        $return = $this->wsdl->addComplexType('Zend_Soap_Wsdl_SequenceTest');
+
+        $this->assertEquals("tns:Zend_Soap_Wsdl_SequenceTest", $return);
+
+        $wsdl = $this->wsdl->toXML();
+
+        $this->assertContains(
+            '<xsd:complexType name="Zend_Soap_Wsdl_SequenceTest"><xsd:all><xsd:element name="var" type="xsd:int"/></xsd:all></xsd:complexType>',
+            $wsdl
+        );
     }
 
-    public function testStrategyCannotHandleReturnOfObjectsThrowingException()
+    public function testAddComplexTypeArrayOfObject()
     {
-        try {
-            $return = $this->wsdl->addComplexType('Zend_Soap_Wsdl_SequenceTest');
-            $this->fail();
-        } catch(Zend_Soap_Wsdl_Exception $e) {
-            
-        }
+         
+         $return = $this->wsdl->addComplexType('Zend_Soap_Wsdl_SequenceTest[]');
+
+         $this->assertEquals("tns:ArrayOfZend_soap_wsdl_sequencetest", $return);
+
+         $wsdl = $this->wsdl->toXML();
+
+         $this->assertContains(
+            '<xsd:complexType name="Zend_Soap_Wsdl_SequenceTest"><xsd:all><xsd:element name="var" type="xsd:int"/></xsd:all></xsd:complexType>',
+            $wsdl
+         );
+
+         $this->assertContains(
+            '<xsd:complexType name="ArrayOfZend_soap_wsdl_sequencetest"><xsd:sequence><xsd:element name="item" type="tns:Zend_Soap_Wsdl_SequenceTest" minOccurs="0" maxOccurs="unbounded"/></xsd:sequence></xsd:complexType>',
+            $wsdl
+         );
     }
 
     public function testAddComplexTypeOfNonExistingClassThrowsException()
     {
-        try {
-            $this->wsdl->addComplexType('Zend_Soap_Wsdl_UnknownClass[]');
-        } catch(Zend_Soap_Wsdl_Exception $e) {
+        $this->setExpectedException("Zend_Soap_Wsdl_Exception");
 
-        }
+        $this->wsdl->addComplexType('Zend_Soap_Wsdl_UnknownClass[]');
     }
 }