123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- require_once '../schema/ServiceSchemaWriter.php';
- require_once '../schema/ServiceSchemaFactory.php';
- require_once '../schema/ServiceSchemaReader.php';
- try {
- $result = ServiceSchemaReader::readXmlForArrayByFile('your_xml_file_path');
- echo var_dump($result), PHP_EOL;
- } catch (Exception $e) {
- var_dump($e->getMessage());
- }
- try {
-
- $xml_string = 'your_xml_string';
- $result = ServiceSchemaReader::readXmlForArrayByString($xml_string);
- echo var_dump($result), PHP_EOL;
-
- $res = ServiceSchemaWriter::writeSchemaXmlString(handleResult($result));
- echo var_dump($res), PHP_EOL;
- } catch (Exception $e) {
- var_dump($e->getMessage());
- }
- function handleResult($result){
- $attributeArr = array();
- foreach ($result as $key => $value) {
- switch ($key){
- case "serviceName":
- $value->setValues("sdk构造测试");
- break;
- case "serviceDesc":
- $value->setValues("sdk构造测试-描述");
- break;
- case "serviceUrl":
- foreach ($value->getValues() as $item){
- foreach ($item->getValues() as $attr) {
- if($attr->getId() == 'carrierType'){
- $attr->setValues('SC_MINI_APP');
- }elseif ($attr->getId() == 'carrierUrl'){
- $attr->setValues('alipays://platformapi/startapp?appId=2019101468340048&page=pages');
- }
- }
- }
- break;
- case "poi":
- foreach ($value->getValues() as $attr){
- if($attr->getId() == 'poiName'){
- $attr->setValues('肯德基');
- }elseif ($attr->getId() == 'address'){
- $attr->setValues('蚂蚁金服A空间');
- }elseif ($attr->getId() == 'cityCode'){
- $attr->setValues('330100');
- }
- }
- break;
- case "A2021031200406612":
- $value->setValues(array("6","5"));
- break;
- }
- $attributeArr[] = $value;
- }
- return $attributeArr;
- }
- $singleAttribute = setSingleAttribute();
- $multiAttributes = setMultiAttribute();
- $complexAttributes = setComplexAttribute();
- $multiComplexAttributes = setMultiComplexAttribute();
- try {
- echo var_dump(ServiceSchemaWriter::writeSchemaXmlString(array($singleAttribute, $multiAttributes, $complexAttributes, $multiComplexAttributes))), PHP_EOL;
- } catch (Exception $e) {
- var_dump($e->getMessage());
- }
- try {
- echo var_dump(ServiceSchemaWriter::writeFullchemaXmlString(array($singleAttribute, $multiAttributes, $complexAttributes, $multiComplexAttributes))), PHP_EOL;
- } catch (Exception $e) {
- var_dump($e->getMessage());
- }
- function setSingleAttribute($errType=null)
- {
- $attribute = ServiceSchemaFactory::createAttribute("serviceName", "服务名称", "single", "text");
-
- if($errType=='errId'){
- $attribute = ServiceSchemaFactory::createAttribute(null, "服务名称", "single", "text");
- }elseif ($errType=='errValueType'){
- $attribute = ServiceSchemaFactory::createAttribute("serviceName", "服务名称", "single", "text123");
- }
-
- $attribute->setValues("服务提报demo");
- if($errType=='errValue'){
- $attribute->setValues(array("服务提报demo"));
- }
-
- $rules = array(
- $errType == 'errRule' ? ServiceSchemaFactory::createRule("required", "必填") : ServiceSchemaFactory::createRule("required", "必填", "true"),
- ServiceSchemaFactory::createRule("maxLength", "最大长度", "15")
- );
- $attribute->setRules($rules);
-
- $options = array(
- $errType == 'errOption' ? ServiceSchemaFactory::createOption("支付宝小程序") : ServiceSchemaFactory::createOption("支付宝小程序", "SC_MINI_APP"),
- ServiceSchemaFactory::createOption("高德小程序", "AMAP_MINI_APP")
- );
- $attribute->setOptions($options);
- return $attribute;
- }
- function setMultiAttribute($errType=null)
- {
- $attribute = ServiceSchemaFactory::createAttribute("serviceName", "服务名称", "multi", "text");
-
- $values = array("周一", "周二");
- $attribute->setValues($values);
- if($errType=='errValue'){
- $attribute->setValues("周一");
- }
-
- $rules = array(
- ServiceSchemaFactory::createRule("pattern", "正则表达式", "[\\u4E00-\\u9FA5A-Za-z0-9_+\\-()()]+"),
- ServiceSchemaFactory::createRule("minLength", "最小长度", "2")
- );
- $attribute->setRules($rules);
-
- $options = array(
- ServiceSchemaFactory::createOption("UC小程序地址", "UC_MINI_APP"),
- ServiceSchemaFactory::createOption("夸克小程序地址", "QUARK_MINI_APP")
- );
- $attribute->setOptions($options);
- return $attribute;
- }
- function setComplexAttribute($errType=null)
- {
- $attributes = ServiceSchemaFactory::createAttribute("serviceName", "服务名称", "complex", "text");
- $singleAttribute = setSingleAttribute();
- $multiAttributes = setMultiAttribute();
-
- $attributes->setValues(array($singleAttribute, $multiAttributes));
- if($errType=='errValue'){
- $attributes->setValues("服务提报demo");
- }
- $attributes->setRules(array(
- ServiceSchemaFactory::createRule("pattern", "正则表达式", "[\\u4E00-\\u9FA5A-Za-z0-9_+\\-()()]+"),
- ServiceSchemaFactory::createRule("minLength", "最小长度", "2")
- ));
- return $attributes;
- }
- function setMultiComplexAttribute($errType=null)
- {
- $attribute = ServiceSchemaFactory::createAttribute("serviceName", "服务名称", "multiComplex", "text");
- $attributesComplex = setComplexAttribute();
-
- $values = array($attributesComplex, $attributesComplex);
- $attribute->setValues($values);
- if($errType=='errValue'){
- $attribute->setValues(array("服务提报demo"));
- }
- return $attribute;
- }
|