仓库初始化
This commit is contained in:
114
extend/AliPayV2/alipay-sdk/aopV2/schema/AttributeRule.php
Normal file
114
extend/AliPayV2/alipay-sdk/aopV2/schema/AttributeRule.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
class AttributeRule
|
||||
{
|
||||
private $type;
|
||||
private $name;
|
||||
private $value;
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $type
|
||||
* @param $value
|
||||
*/
|
||||
public function __construct($type = null, $name = null, $value = null)
|
||||
{
|
||||
$this->type = $type;
|
||||
$this->name = $name;
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
public function checkRuleType($ruleType)
|
||||
{
|
||||
$ruleTypeArr = array(
|
||||
"required" => "是否必填,true/false",
|
||||
"maxLength" => "字符串要求最大长度",
|
||||
"minLength" => "字符串要求最小长度",
|
||||
"exclusiveMaximum" => "最大值,包含最大值,<=",
|
||||
"exclusiveMinimum" => "最小值,包含最小值,>=",
|
||||
"maximum" => "最大值,不包含最大值,<",
|
||||
"minimum" => "最小值,不包含最小值,>",
|
||||
"maxSize" => "最大个数",
|
||||
"minSize" => "最大个数",
|
||||
"precision" => "精度",
|
||||
"pattern" => "正则表达式",
|
||||
"valueType" => "值类型",
|
||||
"devTip" => "输入提示",
|
||||
"height" => "高度",
|
||||
"width" => "长度",
|
||||
"urlSchema" => "url格式"
|
||||
);
|
||||
if (empty($ruleType)) return false;
|
||||
if (array_key_exists($ruleType, $ruleTypeArr)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function toElement($dom, $parent, $attributeId)
|
||||
{
|
||||
if (empty($this->name) && $this->name != '0') {
|
||||
throw new \Exception("id=[" . $attributeId . "] AttrRule格式错误!AttrRule缺少name!");
|
||||
}
|
||||
if (!$this->checkRuleType($this->type)) {
|
||||
throw new \Exception("id=[" . $attributeId . "] AttrRule类型type不合法!");
|
||||
}
|
||||
if (empty($this->value) && $this->value != '0') {
|
||||
throw new \Exception("id=[" . $attributeId . "] AttrRule格式错误!AttrRule缺少value!");
|
||||
}
|
||||
$rule = $dom->createElement("rule");
|
||||
$rule->setAttribute("name", $this->name);
|
||||
$rule->setAttribute("type", $this->type);
|
||||
$rule->setAttribute("value", $this->value);
|
||||
$parent->appendChild($rule);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $name
|
||||
*/
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getType()
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $type
|
||||
*/
|
||||
public function setType($type)
|
||||
{
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getValue()
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function setValue($value)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
}
|
||||
66
extend/AliPayV2/alipay-sdk/aopV2/schema/Option.php
Normal file
66
extend/AliPayV2/alipay-sdk/aopV2/schema/Option.php
Normal file
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
class Option
|
||||
{
|
||||
private $displayName;
|
||||
private $value;
|
||||
|
||||
/**
|
||||
* @param $displayName
|
||||
* @param $value
|
||||
*/
|
||||
public function __construct($displayName = null, $value = null)
|
||||
{
|
||||
$this->displayName = $displayName;
|
||||
$this->value = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function toElement($dom, $parent, $attributeId)
|
||||
{
|
||||
if (empty($this->displayName) && $this->displayName != '0') {
|
||||
throw new \Exception("id=[" . $attributeId . "] Option格式错误!Option名称displayName不能为空!");
|
||||
}
|
||||
if (empty($this->value) && $this->value != '0') {
|
||||
throw new \Exception("id=[" . $attributeId . "] Option格式错误!Option的值value不能为空!");
|
||||
}
|
||||
$option = $dom->createElement("option");
|
||||
$option->setAttribute("displayName", $this->displayName);
|
||||
$option->setAttribute("value", $this->value);
|
||||
$parent->appendChild($option);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getDisplayName()
|
||||
{
|
||||
return $this->displayName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $displayName
|
||||
*/
|
||||
public function setDisplayName($displayName)
|
||||
{
|
||||
$this->displayName = $displayName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function getValue()
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function setValue($value)
|
||||
{
|
||||
$this->value = $value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
require_once 'XMLAttribute.php';
|
||||
require_once 'AttributeRule.php';
|
||||
require_once 'Option.php';
|
||||
|
||||
class ServiceSchemaFactory
|
||||
{
|
||||
public static function createAttribute($id = null, $name = null, $type = null, $valueType = null)
|
||||
{
|
||||
$attribute = new XMLAttribute();
|
||||
$attribute->setId($id);
|
||||
$attribute->setName($name);
|
||||
$attribute->setType($type);
|
||||
$attribute->setValueType($valueType);
|
||||
return $attribute;
|
||||
}
|
||||
|
||||
public static function createRule($type = null, $name = null, $value = null)
|
||||
{
|
||||
return new AttributeRule($type, $name, $value);
|
||||
}
|
||||
|
||||
public static function createOption($displayName = null, $value = null)
|
||||
{
|
||||
return new Option($displayName, $value);
|
||||
}
|
||||
}
|
||||
256
extend/AliPayV2/alipay-sdk/aopV2/schema/ServiceSchemaReader.php
Normal file
256
extend/AliPayV2/alipay-sdk/aopV2/schema/ServiceSchemaReader.php
Normal file
@@ -0,0 +1,256 @@
|
||||
<?php
|
||||
require_once 'XMLAttribute.php';
|
||||
require_once 'AttributeRule.php';
|
||||
require_once 'Option.php';
|
||||
require_once 'ServiceSchemaFactory.php';
|
||||
|
||||
class ServiceSchemaReader
|
||||
{
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function readXmlForArrayByFile($filePath)
|
||||
{
|
||||
set_error_handler('ServiceSchemaReader::errorHandler');
|
||||
$dom = new DOMDocument('1.0', 'utf-8');
|
||||
try {
|
||||
$dom->load($filePath);
|
||||
} catch (Exception $e) {
|
||||
throw new \Exception("XML格式错误,无法正常解析!");
|
||||
}
|
||||
return self::readXmlForList($dom->documentElement);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function readXmlForArrayByString($xmlString)
|
||||
{
|
||||
set_error_handler('ServiceSchemaReader::errorHandler');
|
||||
$dom = new DOMDocument('1.0', 'utf-8');
|
||||
try {
|
||||
$dom->loadXML($xmlString);
|
||||
} catch (Exception $e) {
|
||||
throw new \Exception("XML格式错误,无法正常解析!");
|
||||
}
|
||||
return self::readXmlForList($dom->documentElement);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function readXmlForList(DOMElement $rootEle)
|
||||
{
|
||||
$attributeArr = array();
|
||||
foreach ($rootEle->getElementsByTagName('attribute') as $item) {
|
||||
if ($item->parentNode === $rootEle) {
|
||||
$attribute = self::elementToAttribute($item);
|
||||
$attributeArr[$attribute->getId()] = $attribute;
|
||||
}
|
||||
}
|
||||
return $attributeArr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function elementToAttribute($attributeElm)
|
||||
{
|
||||
$attributeId = $attributeElm->getAttribute("id");
|
||||
if (self::checkEmpty($attributeId)) {
|
||||
throw new \Exception("Attribute属性缺少id! 节点路径 [" . $attributeElm->getNodePath() . "]");
|
||||
}
|
||||
$attributeName = $attributeElm->getAttribute("name");
|
||||
$attributeType = $attributeElm->getAttribute("type");
|
||||
if (self::checkEmpty($attributeType)) {
|
||||
throw new \Exception("Attribute属性缺少type! 节点路径 [" . $attributeElm->getNodePath() . "].attributeId=" . $attributeId);
|
||||
}
|
||||
|
||||
$valueType = $attributeElm->getAttribute("valueType");
|
||||
$attribute = ServiceSchemaFactory::createAttribute($attributeId, $attributeName, $attributeType, $valueType);
|
||||
if (!$attribute->checkAttributeValueType($valueType)) {
|
||||
throw new \Exception("Attribute属性valueType不正确! 节点路径 [" . $attributeElm->getNodePath() . "].attributeId=" . $attributeId);
|
||||
}
|
||||
|
||||
if ("single" === $attributeType) {
|
||||
ServiceSchemaReader::elementToSingleAttribute($attributeElm, $attribute);
|
||||
} elseif ("multi" === $attributeType) {
|
||||
ServiceSchemaReader::elementToMultiAttribute($attributeElm, $attribute);
|
||||
} elseif ("complex" === $attributeType) {
|
||||
ServiceSchemaReader::elementToComplexAttribute($attributeElm, $attribute);
|
||||
} elseif ("multiComplex" === $attributeType) {
|
||||
ServiceSchemaReader::elementToMultiComplexAttribute($attributeElm, $attribute);
|
||||
} else {
|
||||
throw new \Exception("Attribute属性type类型不正确! 节点路径 [" . $attributeElm->getNodePath() . "].attributeId=" . $attributeId);
|
||||
}
|
||||
return $attribute;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
private static function elementToSingleAttribute($attributeElm, $attribute)
|
||||
{
|
||||
self::elementToRule($attributeElm, $attribute);
|
||||
self::elementToOption($attributeElm, $attribute);
|
||||
foreach ($attributeElm->getElementsByTagName('value') as $value) {
|
||||
if ($value->parentNode === $attributeElm) {
|
||||
$attribute->setValues($value->nodeValue);
|
||||
//只取第一个<value>标签
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
private static function elementToMultiAttribute($attributeElm, $attribute)
|
||||
{
|
||||
self::elementToRule($attributeElm, $attribute);
|
||||
self::elementToOption($attributeElm, $attribute);
|
||||
foreach ($attributeElm->getElementsByTagName('values') as $values) {
|
||||
if ($values->parentNode === $attributeElm) {
|
||||
$valueArr = array();
|
||||
foreach ($values->getElementsByTagName('value') as $value) {
|
||||
if ($value->parentNode === $values) {
|
||||
$valueArr[] = $value->nodeValue;
|
||||
}
|
||||
}
|
||||
$attribute->setValues($valueArr);
|
||||
//只取第一个<values>标签
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
private static function elementToComplexAttribute($attributeElm, $attribute)
|
||||
{
|
||||
self::elementToRule($attributeElm, $attribute);
|
||||
foreach ($attributeElm->getElementsByTagName('attributes') as $attributes) {
|
||||
if ($attributes->parentNode === $attributeElm) {
|
||||
$attributeArr = array();
|
||||
foreach ($attributes->getElementsByTagName('attribute') as $item) {
|
||||
if ($item->parentNode === $attributes) {
|
||||
$attributeType = $item->getAttribute("type");
|
||||
if ("single" === $attributeType || "multi" === $attributeType) {
|
||||
$attributeArr[] = self::elementToAttribute($item);
|
||||
}
|
||||
} else {
|
||||
throw new \Exception("Attribute属性type类型不正确! 节点路径 [" . $item->getNodePath() . "]");
|
||||
}
|
||||
}
|
||||
$attribute->setValues($attributeArr);
|
||||
//只取第一个<attributes>标签
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
private static function elementToMultiComplexAttribute($attributeElm, $attribute)
|
||||
{
|
||||
$attributeArr = array();
|
||||
foreach ($attributeElm->getElementsByTagName('attributes') as $attributes) {
|
||||
if ($attributes->parentNode === $attributeElm) {
|
||||
$complexAttr = new XMLAttribute();//每一个$complexAttr都是一个ComplexAttribute对象
|
||||
$valuesArr = array();
|
||||
foreach ($attributes->getElementsByTagName('attribute') as $item) {
|
||||
if ($item->parentNode === $attributes) {
|
||||
$attributeType = $item->getAttribute("type");
|
||||
if ("single" === $attributeType || "multi" === $attributeType) {
|
||||
$valuesArr[] = self::elementToAttribute($item);
|
||||
}
|
||||
} else {
|
||||
throw new \Exception("Attribute属性type类型不正确! 节点路径 [" . $item->getNodePath() . "]");
|
||||
}
|
||||
}
|
||||
$complexAttr->setValues($valuesArr);
|
||||
$attributeArr[] = $complexAttr;
|
||||
}
|
||||
}
|
||||
$attribute->setValues($attributeArr);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
private static function elementToRule($attributeElm, $attribute)
|
||||
{
|
||||
foreach ($attributeElm->getElementsByTagName('rules') as $rules) {
|
||||
if ($rules->parentNode === $attributeElm) {
|
||||
$ruleArr = array();
|
||||
foreach ($rules->getElementsByTagName('rule') as $rule) {
|
||||
if ($rule->parentNode === $rules) {
|
||||
$ruleType = $rule->getAttribute("type");
|
||||
$ruleName = $rule->getAttribute("name");
|
||||
$ruleValue = $rule->getAttribute("value");
|
||||
if (self::checkEmpty($ruleValue)) {
|
||||
throw new \Exception("id=[" . $attribute->getId() . "] AttrRule格式错误!AttrRule缺少value!");
|
||||
}
|
||||
$ruleObj = ServiceSchemaFactory::createRule($ruleType, $ruleName, $ruleValue);
|
||||
if (!$ruleObj->checkRuleType($ruleType)) {
|
||||
throw new \Exception("id=[" . $attribute->getId() . "] AttrRule类型type不合法!");
|
||||
}
|
||||
$ruleArr[] = $ruleObj;
|
||||
}
|
||||
}
|
||||
$attribute->setRules($ruleArr);
|
||||
//只取第一个<rules>标签
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
private static function elementToOption($attributeElm, $attribute)
|
||||
{
|
||||
foreach ($attributeElm->getElementsByTagName('options') as $options) {
|
||||
if ($options->parentNode === $attributeElm) {
|
||||
$optionsArr = array();
|
||||
foreach ($options->getElementsByTagName('option') as $option) {
|
||||
if ($option->parentNode === $options) {
|
||||
$displayName = $option->getAttribute("displayName");
|
||||
if (self::checkEmpty($displayName)) {
|
||||
throw new \Exception("id=[" . $attribute->getId() . "] Option格式错误!Option名称displayName不能为空!");
|
||||
}
|
||||
$value = $option->getAttribute("value");
|
||||
if (self::checkEmpty($value)) {
|
||||
throw new \Exception("id=[" . $attribute->getId() . "] Option格式错误!Option的值value不能为空!");
|
||||
}
|
||||
$optionObj = ServiceSchemaFactory::createOption($displayName, $value);
|
||||
$optionsArr[] = $optionObj;
|
||||
}
|
||||
}
|
||||
$attribute->setOptions($optionsArr);
|
||||
//只取第一个<options>标签
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static function checkEmpty($value)
|
||||
{
|
||||
if (is_null($value))
|
||||
return true;
|
||||
if (trim($value) == "")
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
static function errorHandler($errno, $errstr, $errfile, $errline)
|
||||
{
|
||||
throw new \Exception($errstr);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
class ServiceSchemaWriter
|
||||
{
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function writeSchemaXmlString(array $attributesArr)
|
||||
{
|
||||
$dom = new DOMDocument('1.0', 'utf-8');
|
||||
$root = $dom->createElement("serviceSchema");
|
||||
foreach ($attributesArr as $attribute) {
|
||||
if ($attribute instanceof XMLAttribute) {
|
||||
$attribute->toValueElement($dom, $root);
|
||||
}
|
||||
}
|
||||
$dom->appendChild($root);
|
||||
return $dom->saveXML($dom->documentElement);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function writeFullchemaXmlString(array $attributesArr)
|
||||
{
|
||||
$dom = new DOMDocument('1.0', 'utf-8');
|
||||
$root = $dom->createElement("serviceSchema");
|
||||
foreach ($attributesArr as $attribute) {
|
||||
if ($attribute instanceof XMLAttribute) {
|
||||
$attribute->toElement($dom, $root);
|
||||
}
|
||||
}
|
||||
$dom->appendChild($root);
|
||||
return $dom->saveXML($dom->documentElement);
|
||||
}
|
||||
}
|
||||
342
extend/AliPayV2/alipay-sdk/aopV2/schema/XMLAttribute.php
Normal file
342
extend/AliPayV2/alipay-sdk/aopV2/schema/XMLAttribute.php
Normal file
@@ -0,0 +1,342 @@
|
||||
<?php
|
||||
|
||||
class XMLAttribute
|
||||
{
|
||||
private $id;
|
||||
private $name;
|
||||
private $type;
|
||||
private $valueType;
|
||||
private $rules;
|
||||
private $options;
|
||||
private $values;
|
||||
|
||||
public function checkAttributeValueType($attributeValueType)
|
||||
{
|
||||
$valueTypeArr = array(
|
||||
"text" => "文本类型",
|
||||
"boolean" => "boolean类型",
|
||||
"numeric" => "数据类型",
|
||||
"enum" => "枚举型",
|
||||
"object" => "对象型"
|
||||
);
|
||||
if (empty($attributeValueType)) return false;
|
||||
// foreach ($valueTypeArr as $key => $value){
|
||||
// if($attributeValueType === $key) return true;
|
||||
// }
|
||||
if (array_key_exists($attributeValueType, $valueTypeArr)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function toElement($dom, $parent)
|
||||
{
|
||||
$this->checkAttribute();
|
||||
if ("single" === $this->type || "multi" === $this->type) {
|
||||
$attributeElm = $this->appendAttributeValues($dom, $parent, $this);
|
||||
$this->appendRulesElement($dom, $attributeElm, $this->rules, $this->id);
|
||||
$this->appendOptionsElement($dom, $attributeElm, $this->options, $this->id);
|
||||
} elseif ("complex" === $this->type) {
|
||||
$attributeElm = $dom->createElement("attribute");
|
||||
$attributeElm->setAttribute("id", $this->id);
|
||||
$attributeElm->setAttribute("type", $this->type);
|
||||
if ($this->name != "" && !is_null($this->name)) {
|
||||
$attributeElm->setAttribute("name", $this->name);
|
||||
}
|
||||
if ($this->checkAttributeValueType($this->valueType)) {
|
||||
$attributeElm->setAttribute("valueType", $this->valueType);
|
||||
}
|
||||
|
||||
$attributesElm = $dom->createElement("attributes");
|
||||
if (is_array($this->values)) {
|
||||
foreach ($this->values as $attribute) {
|
||||
if ($attribute instanceof XMLAttribute) {
|
||||
$attributeElmTmp = $this->appendAttributeValues($dom, $attributesElm, $attribute);
|
||||
$this->appendRulesElement($dom, $attributeElmTmp, $attribute->rules, $attribute->id);
|
||||
$this->appendOptionsElement($dom, $attributeElmTmp, $attribute->options, $attribute->id);
|
||||
}
|
||||
}
|
||||
} elseif (!empty($this->values)) {//complex类型的values必须是数组 或 空
|
||||
throw new \Exception("id=[" . $this->id . "] XMLAttribute属性values不合法!");
|
||||
}
|
||||
$attributeElm->appendChild($attributesElm);
|
||||
$this->appendRulesElement($dom, $attributeElm, $this->rules, $this->id);
|
||||
$parent->appendChild($attributeElm);
|
||||
} elseif ("multiComplex" === $this->type) {
|
||||
$attributeElm = $dom->createElement("attribute");
|
||||
$attributeElm->setAttribute("id", $this->id);
|
||||
if ($this->name != "" && !is_null($this->name)) {
|
||||
$attributeElm->setAttribute("name", $this->name);
|
||||
}
|
||||
$attributeElm->setAttribute("type", $this->type);
|
||||
$attributeElm->setAttribute("valueType", "object");
|
||||
|
||||
if (is_array($this->values)) {
|
||||
foreach ($this->values as $complexAttribute) {
|
||||
if ($complexAttribute instanceof XMLAttribute) {
|
||||
$attributesElm = $dom->createElement("attributes");
|
||||
if (is_array($complexAttribute->getValues())) {
|
||||
foreach ($complexAttribute->getValues() as $attribute) {
|
||||
if ($attribute instanceof XMLAttribute) {
|
||||
if (!$this->checkAttributeValueType($attribute->getValueType())) {
|
||||
throw new \Exception("id=[" . $attribute->getId() . "] XMLAttribute属性valueType不正确!");
|
||||
}
|
||||
$attributeElmTmp = $this->appendAttributeValues($dom, $attributesElm, $attribute);
|
||||
$this->appendRulesElement($dom, $attributeElmTmp, $attribute->rules, $attribute->id);
|
||||
$this->appendOptionsElement($dom, $attributeElmTmp, $attribute->options, $attribute->id);
|
||||
}
|
||||
}
|
||||
} elseif (!empty($complexAttribute->getValues())) {//complex类型的values必须是数组 或 空
|
||||
throw new \Exception("id=[" . $complexAttribute->getId() . "] XMLAttribute属性values不合法!");
|
||||
}
|
||||
$attributeElm->appendChild($attributesElm);
|
||||
}
|
||||
}
|
||||
} elseif (!empty($this->values)) {//multiComplex类型的values必须是数组 或 空
|
||||
throw new \Exception("id=[" . $this->id . "] XMLAttribute属性values不合法!");
|
||||
}
|
||||
$parent->appendChild($attributeElm);
|
||||
} else {
|
||||
throw new \Exception("id=[" . $this->id . "] XMLAttribute属性type类型不正确!");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function toValueElement($dom, $parent)
|
||||
{
|
||||
$this->checkAttribute();
|
||||
if ("single" === $this->type || "multi" === $this->type) {
|
||||
$this->appendAttributeValues($dom, $parent, $this);
|
||||
} elseif ("complex" === $this->type) {
|
||||
$attributeElm = $dom->createElement("attribute");
|
||||
$attributeElm->setAttribute("id", $this->id);
|
||||
$attributeElm->setAttribute("type", $this->type);
|
||||
if ($this->name != "" && !is_null($this->name)) {
|
||||
$attributeElm->setAttribute("name", $this->name);
|
||||
}
|
||||
if ($this->checkAttributeValueType($this->valueType)) {
|
||||
$attributeElm->setAttribute("valueType", $this->valueType);
|
||||
}
|
||||
|
||||
$attributesElm = $dom->createElement("attributes");
|
||||
if (is_array($this->values)) {
|
||||
foreach ($this->values as $attribute) {
|
||||
if ($attribute instanceof XMLAttribute) {
|
||||
$this->appendAttributeValues($dom, $attributesElm, $attribute);
|
||||
}
|
||||
}
|
||||
} elseif (!empty($this->values)) {//complex类型的values必须是数组 或 空
|
||||
throw new \Exception("id=[" . $this->id . "] XMLAttribute属性values不合法!");
|
||||
}
|
||||
$attributeElm->appendChild($attributesElm);
|
||||
$parent->appendChild($attributeElm);
|
||||
} elseif ("multiComplex" === $this->type) {
|
||||
$attributeElm = $dom->createElement("attribute");
|
||||
$attributeElm->setAttribute("id", $this->id);
|
||||
if ($this->name != "" && !is_null($this->name)) {
|
||||
$attributeElm->setAttribute("name", $this->name);
|
||||
}
|
||||
$attributeElm->setAttribute("type", $this->type);
|
||||
$attributeElm->setAttribute("valueType", "object");
|
||||
|
||||
if (is_array($this->values)) {
|
||||
foreach ($this->values as $complexAttribute) {
|
||||
if ($complexAttribute instanceof XMLAttribute) {
|
||||
$attributesElm = $dom->createElement("attributes");
|
||||
if (is_array($complexAttribute->getValues())) {
|
||||
foreach ($complexAttribute->getValues() as $attribute) {
|
||||
if ($attribute instanceof XMLAttribute) {
|
||||
if (!$this->checkAttributeValueType($attribute->getValueType())) {
|
||||
throw new \Exception("id=[" . $attribute->getId() . "] XMLAttribute属性valueType不正确!");
|
||||
}
|
||||
$this->appendAttributeValues($dom, $attributesElm, $attribute);
|
||||
}
|
||||
}
|
||||
} elseif (!empty($complexAttribute->getValues())) {//complex类型的values必须是数组 或 空
|
||||
throw new \Exception("id=[" . $this->id . "] XMLAttribute属性values不合法!");
|
||||
}
|
||||
$attributeElm->appendChild($attributesElm);
|
||||
}
|
||||
}
|
||||
} elseif (!empty($this->values)) {//multiComplex类型的values必须是数组 或 空
|
||||
throw new \Exception("id=[" . $this->id . "] XMLAttribute属性values不合法!");
|
||||
}
|
||||
$parent->appendChild($attributeElm);
|
||||
} else {
|
||||
throw new \Exception("id=[" . $this->id . "] XMLAttribute属性type类型不正确!");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function appendAttributeValues($dom, $parent, $attribute)
|
||||
{
|
||||
$attributeElm = $dom->createElement("attribute");
|
||||
if ($attribute->getId() != "" && !is_null($attribute->getId())) {
|
||||
$attributeElm->setAttribute("id", $attribute->getId());
|
||||
}
|
||||
if ($attribute->getName() != "" && !is_null($attribute->getName())) {
|
||||
$attributeElm->setAttribute("name", $attribute->getName());
|
||||
}
|
||||
if ($attribute->getType() != "" && !is_null($attribute->getType())) {
|
||||
$attributeElm->setAttribute("type", $attribute->getType());
|
||||
}
|
||||
if ($this->checkAttributeValueType($attribute->getValueType())) {
|
||||
$attributeElm->setAttribute("valueType", $attribute->getValueType());
|
||||
}
|
||||
$values = $attribute->getValues();
|
||||
|
||||
if ("single" === $attribute->getType()) {
|
||||
$valueElm = $dom->createElement("value");
|
||||
if (is_string($values)) {
|
||||
$text = $dom->createTextNode($values);
|
||||
$valueElm->appendChild($text);
|
||||
} elseif (!is_null($values)) {//single类型的values必须是字符串 或 空
|
||||
throw new \Exception("id=[" . $attribute->getId() . "] XMLAttribute属性values不合法!");
|
||||
}
|
||||
$attributeElm->appendChild($valueElm);
|
||||
$parent->appendChild($attributeElm);
|
||||
} elseif ("multi" === $attribute->getType()) {
|
||||
$valuesElm = $dom->createElement("values");
|
||||
if (is_array($values)) {
|
||||
foreach ($values as $value) {
|
||||
if (is_string($value) || is_null($value)) {
|
||||
$valueElm = $dom->createElement("value", $value);
|
||||
$valuesElm->appendChild($valueElm);
|
||||
}
|
||||
}
|
||||
} elseif (!empty($values)) {//multi类型的values必须是数组 或 空
|
||||
throw new \Exception("id=[" . $attribute->getId() . "] XMLAttribute属性values不合法!");
|
||||
}
|
||||
$attributeElm->appendChild($valuesElm);
|
||||
$parent->appendChild($attributeElm);
|
||||
} else {
|
||||
throw new \Exception("id=[" . $attribute->getId() . "] XMLAttribute属性type类型不正确!");
|
||||
}
|
||||
return $attributeElm;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function appendRulesElement($dom, $parent, $rules, $attributeId)
|
||||
{
|
||||
if (empty($rules)) return;
|
||||
if (!is_array($rules)) {
|
||||
throw new \Exception("id=[" . $attributeId . "] XMLAttribute属性rules不合法!");
|
||||
}
|
||||
$rulesElm = $dom->createElement("rules");
|
||||
foreach ($rules as $rule) {
|
||||
if ($rule instanceof AttributeRule) {
|
||||
$rule->toElement($dom, $rulesElm, $attributeId);
|
||||
}
|
||||
}
|
||||
$parent->appendChild($rulesElm);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function appendOptionsElement($dom, $parent, $options, $attributeId)
|
||||
{
|
||||
if (empty($options)) return;
|
||||
if (!is_array($options)) {
|
||||
throw new \Exception("id=[" . $attributeId . "] XMLAttribute属性options不合法!");
|
||||
}
|
||||
$optionsElm = $dom->createElement("options");
|
||||
foreach ($options as $option) {
|
||||
if ($option instanceof Option) {
|
||||
$option->toElement($dom, $optionsElm, $attributeId);
|
||||
}
|
||||
}
|
||||
$parent->appendChild($optionsElm);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function checkAttribute()
|
||||
{
|
||||
if (empty($this->id) && $this->id != '0') {
|
||||
throw new \Exception("XMLAttribute属性缺少id!");
|
||||
}
|
||||
if (empty($this->type) && $this->type != '0') {
|
||||
throw new \Exception("id=[" . $this->id . "] XMLAttribute属性缺少type!");
|
||||
}
|
||||
}
|
||||
|
||||
public function getId()
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function setId($id)
|
||||
{
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
public function getType()
|
||||
{
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
public function setType($type)
|
||||
{
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
public function getValueType()
|
||||
{
|
||||
return $this->valueType;
|
||||
}
|
||||
|
||||
public function setValueType($valueType)
|
||||
{
|
||||
$this->valueType = $valueType;
|
||||
}
|
||||
|
||||
public function getRules()
|
||||
{
|
||||
return $this->rules;
|
||||
}
|
||||
|
||||
public function setRules($rules)
|
||||
{
|
||||
$this->rules = $rules;
|
||||
}
|
||||
|
||||
public function getOptions()
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
public function setOptions($options)
|
||||
{
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
public function getValues()
|
||||
{
|
||||
return $this->values;
|
||||
}
|
||||
|
||||
public function setValues($values)
|
||||
{
|
||||
$this->values = $values;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user