Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of wrapped document/literal array

Tags:

arrays

php

soap

xml

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>
like image 925
Reuben Avatar asked Jun 24 '14 00:06

Reuben


1 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>
like image 91
Kevin Sandow Avatar answered Oct 02 '22 14:10

Kevin Sandow