Home » Php » php – Example of wrapped document/literal array

php – Example of wrapped document/literal array

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’ve having trouble finding a solid example of a simple array in a wrapped document/literal style.

Consider a PHP function that generates an array up to a max count.

/**
 * @param int $max
 * @return string[] $count
 */
public function countTo($max)
{
    $array = array();
    for ($i = 0; $i < $max; $i++) {
        $array[] = 'Number: ' . ($i + 1);
    }
    return $array;
}

The WSDL types generated for this are:

<xsd:complexType name="countTo">
  <xsd:sequence>
    <xsd:element name="max" type="xsd:int"/>
  </xsd:sequence>
</xsd:complexType>
<xsd:element name="countTo" nillable="true" type="ns:countTo"/>
<xsd:complexType name="ArrayOfCount">
  <xsd:sequence>
    <xsd:element minOccurs="0" maxOccurs="unbounded" name="count" nillable="true" type="xsd:string"/>
  </xsd:sequence>
</xsd:complexType>
<xsd:complexType name="countToResponse">
  <xsd:sequence>
    <xsd:element name="count" type="ns:ArrayOfCount"/>
  </xsd:sequence>
</xsd:complexType>
<xsd:element name="countToResponse" nillable="true" type="ns:countToResponse"/>

The request in the body would look like:

<ns1:countTo>
  <max>5</max>
</ns1:countTo>

But what does the response look like, and what is the convention?

Currently, SoapServer is generating

<ns1:countToResponse>
  <count>
    <count>Number: 1</count>
    <count>Number: 2</count>
    <count>Number: 3</count>
    <count>Number: 4</count>
    <count>Number: 5</count>
  </count>
</ns1:countToResponse>

I’m not sure about the nested count elements. Perhaps this should be item instead (and the WSDL would need to be updated to make it happen).

<ns1:countToResponse>
  <count>
    <item>Number: 1</item>
    <item>Number: 2</item>
    <item>Number: 3</item>
    <item>Number: 4</item>
    <item>Number: 5</item>
  </count>
</ns1:countToResponse>
How to&Answers:

There really is no convention for this. It sometimes is convenient when you have an array of item to name the collection items. But since your element is named count is a bit more difficult, since counts would not be an accepted answer. You might choose result, but that would only fit when it is also the only container element, as in your example.

<xsd:complexType name="countToResponse">
  <xsd:sequence>
    <xsd:element name="result" type="ns:ArrayOfCount"/>
  </xsd:sequence>
</xsd:complexType>