gpt4 book ai didi

php - Zend soap 自动发现和生成的 WSDL 中的 nillable ="true"

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:01:44 26 4
gpt4 key购买 nike

我正在使用 Zend soap autodiscovery为我的网络服务器生成一个 WSDL 文件。问题是每个 complexType 的每个元素都默认为 nillable="true"。我如何根据需要声明元素?我阅读了 PHPDoc 但一无所获。

编辑:代码:

class MyService {
/**
* Identify remote user.
*
* @param LoginReq
* @return LoginResp
*/
public function login($request) {
// Code ....
}
}

class LoginReq {
/** @var string */
public $username;
/** @var string */
public $password;
}
class LoginResp {
/** @var string */
public $errorCode;
}

生成的 WSDL:

  <xsd:complexType name="LoginReq">
<xsd:all>
<xsd:element name="username" type="xsd:string" nillable="true"/>
<xsd:element name="password" type="xsd:string" nillable="true"/>
</xsd:all>
</xsd:complexType>
<xsd:complexType name="LoginResp">
<xsd:all>
<xsd:element name="errorCode" type="xsd:string" nillable="true"/>
</xsd:all>
</xsd:complexType>

EDIT2: 我刚刚发现要将元素声明为必需/可选,您需要使用 minOccurs/maxOcurrs。它们都默认为 1,因此默认情况下每个元素都是必需的。为了声明一个可选元素,您可以使用 minOccurs="1" 声明它。 Nillable 只是为了允许元素为空。同样,我如何将一个元素声明为可选元素(以便 Zend 向该元素添加 minOccurs="0")?

最佳答案

如果您在函数定义中设置了默认值,它将可以为空。

public function myMethod($argument = 'hello') {
// $argument is nillable
}

如果不是这样,您可以发布带有文档 block 的代码吗?

编辑:您的代码示例说明了很多。

如果您查看 Zend/Soap/Wsdl/Strategy/DefaultComplesType.php 的第 76 行,您会看到:

            // If the default value is null, then this property is nillable.
if ($defaultProperties[$propertyName] === null) {
$element->setAttribute('nillable', 'true');
}

这是确定您的“复杂类型”属性是否可为空的代码。我会尝试更新您的代码以包含字符串的默认值。像这样的东西:

class LoginReq {
/** @var string */
public $username = '';
/** @var string */
public $password = '';
}

如果这样做,=== null 的计算结果应该为 false。不过,请确保您的代码正确处理数据验证。

如果这不起作用,请告诉我!

关于php - Zend soap 自动发现和生成的 WSDL 中的 nillable ="true",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6041577/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com