SchemaTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. require_once '../schema/ServiceSchemaWriter.php';
  3. require_once '../schema/ServiceSchemaFactory.php';
  4. require_once '../schema/ServiceSchemaReader.php';
  5. //读☺️xml文件 转成数组
  6. try {
  7. $result = ServiceSchemaReader::readXmlForArrayByFile('your_xml_file_path');
  8. echo var_dump($result), PHP_EOL;
  9. } catch (Exception $e) {
  10. var_dump($e->getMessage());
  11. }
  12. try {
  13. //获取字符串 转成数组
  14. $xml_string = 'your_xml_string';
  15. // $xml_string = "<serviceSchema><attribute id=\"serviceName\" name=\"服务名称\" type=\"single\" valueType=\"text\"><value/><rules><rule type=\"required\" name=\"是否必填\" value=\"true\"/><rule type=\"maxLength\" name=\"最大长度\" value=\"10\"/><rule type=\"minLength\" name=\"最小长度\" value=\"2\"/><rule type=\"pattern\" name=\"正则表达式\" value=\"[\\u4E00-\\u9FA5A-Za-z0-9_+]+\"/><rule type=\"devTip\" name=\"开发者提示\" value=\"这是框外文案填写格式:可以由中文、数字、英文、下划线、+、-组成审核提醒:1.服务名称代表小程序内的某个功能或一类服务,服务名称不可重复且需代表不同的功能2.服务名称不可与小程序名称重复3.二级服务需要与小程序本身提供的服务相关,如租赁类小程序的服务名称需要与租赁服务有关\"/></rules></attribute><attribute id=\"serviceDesc\" name=\"服务描述\" type=\"single\" valueType=\"text\"><value/><rules><rule type=\"required\" name=\"是否必填\" value=\"true\"/><rule type=\"maxLength\" name=\"最大长度\" value=\"16\"/><rule type=\"minLength\" name=\"最小长度\" value=\"5\"/><rule type=\"devTip\" name=\"开发者提示\" value=\"这是框外文案审核提醒:&amp;lt;br/&amp;gt;1.要清楚介绍小程序的服务功能,比如提供了什么功能,便于通过审核&amp;lt;br/&amp;gt;2.请不要填写无意义的内容&amp;lt;br/&amp;gt;3.请不要出现向外部应用引流的文案\"/></rules></attribute><attribute id=\"serviceUrl\" name=\"服务链接\" type=\"multiComplex\" valueType=\"object\"><attributes><attribute id=\"carrierType\" name=\"载体类型\" type=\"single\" valueType=\"enum\"><rules><rule type=\"required\" name=\"是否必填\" value=\"true\"/></rules><options><option displayName=\"支付宝小程序\" value=\"SC_MINI_APP\"/><option displayName=\"高德小程序\" value=\"AMAP_MINI_APP\"/><option displayName=\"UC小程序地址\" value=\"UC_MINI_APP\"/><option displayName=\"夸克小程序地址\" value=\"QUARK_MINI_APP\"/></options><value/></attribute><attribute id=\"carrierUrl\" name=\"载体url\" type=\"single\" valueType=\"text\"><rules><rule type=\"required\" name=\"是否必填\" value=\"true\"/></rules><value/></attribute></attributes></attribute><attribute id=\"poi\" name=\"poi信息\" type=\"complex\" valueType=\"object\"><attributes><attribute id=\"shopId\" name=\"门店ID\" type=\"single\" valueType=\"text\"><rules><rule type=\"required\" name=\"是否必填\" value=\"false\"/><rule type=\"devTip\" name=\"开发者提示\" value=\"如果服务对应的门店已创建过支付宝门店(https://opendocs.alipay.com/mini/0149jo),可以只传对应的shopID;门店ID和(门店名称+城市码+地址)二选一。\"/></rules><value/></attribute><attribute id=\"poiName\" name=\"门店名称\" type=\"single\" valueType=\"text\"><rules><rule type=\"required\" name=\"是否必填\" value=\"false\"/><rule type=\"devTip\" name=\"开发者提示\" value=\"服务对应的线下门店名称,需要填写标准,门店ID和(门店名称+城市码+地址)二选一。\"/></rules><value/></attribute><attribute id=\"address\" name=\"地址\" type=\"single\" valueType=\"text\"><rules><rule type=\"required\" name=\"是否必填\" value=\"false\"/><rule type=\"devTip\" name=\"开发者提示\" value=\"服门店详细地址,具体到门牌号,支付宝测会基于这个信息去高德转换经纬度信息,需要填写标准,门店ID和(门店名称+城市码+地址)二选一。\"/></rules><value/></attribute><attribute id=\"cityCode\" name=\"城市码\" type=\"single\" valueType=\"text\"><rules><rule type=\"required\" name=\"是否必填\" value=\"false\"/><rule type=\"devTip\" name=\"开发者提示\" value=\"服务对应线下门店所属城市6位行政编码,门店ID和(门店名称+城市码+地址)二选一。\"/></rules><value/></attribute></attributes><rules><rule type=\"devTip\" name=\"开发者提示\" value=\"门店id和(adress+门店名称+cityCode)二选一,有门店id传shopId,没有门店id就填门店详细地址adress;如果选择adress,对应的门店名称和门店所在城市码必须传\"/><rule type=\"required\" name=\"是否必填\" value=\"true\"/><rule type=\"maxSize\" name=\"最大条目数\" value=\"1\"/><rule type=\"minSize\" name=\"最小条目数\" value=\"1\"/></rules></attribute><attribute id=\"A2021031200406612\" name=\"标准属性_TEXT\" type=\"multi\" valueType=\"text\"><values/><rules><rule type=\"required\" name=\"是否必填\" value=\"true\"/><rule type=\"maxSize\" name=\"最大条目数\" value=\"10\"/><rule type=\"minSize\" name=\"最小条目数\" value=\"2\"/><rule type=\"devTip\" name=\"开发者提示\" value=\"这是框外文案\"/></rules></attribute></serviceSchema>";
  16. $result = ServiceSchemaReader::readXmlForArrayByString($xml_string);
  17. echo var_dump($result), PHP_EOL;
  18. //将处理后的数组转为最终结果
  19. $res = ServiceSchemaWriter::writeSchemaXmlString(handleResult($result));
  20. echo var_dump($res), PHP_EOL;
  21. } catch (Exception $e) {
  22. var_dump($e->getMessage());
  23. }
  24. function handleResult($result){
  25. $attributeArr = array();
  26. foreach ($result as $key => $value) {
  27. switch ($key){
  28. case "serviceName":
  29. $value->setValues("sdk构造测试");
  30. break;
  31. case "serviceDesc":
  32. $value->setValues("sdk构造测试-描述");
  33. break;
  34. case "serviceUrl":
  35. foreach ($value->getValues() as $item){
  36. foreach ($item->getValues() as $attr) {
  37. if($attr->getId() == 'carrierType'){
  38. $attr->setValues('SC_MINI_APP');
  39. }elseif ($attr->getId() == 'carrierUrl'){
  40. $attr->setValues('alipays://platformapi/startapp?appId=2019101468340048&page=pages');
  41. }
  42. }
  43. }
  44. break;
  45. case "poi":
  46. foreach ($value->getValues() as $attr){
  47. if($attr->getId() == 'poiName'){
  48. $attr->setValues('肯德基');
  49. }elseif ($attr->getId() == 'address'){
  50. $attr->setValues('蚂蚁金服A空间');
  51. }elseif ($attr->getId() == 'cityCode'){
  52. $attr->setValues('330100');
  53. }
  54. }
  55. break;
  56. case "A2021031200406612":
  57. $value->setValues(array("6","5"));
  58. break;
  59. }
  60. $attributeArr[] = $value;
  61. }
  62. return $attributeArr;
  63. }
  64. //single
  65. $singleAttribute = setSingleAttribute();
  66. //$singleAttribute = setSingleAttribute("errValue");
  67. //multi
  68. $multiAttributes = setMultiAttribute();
  69. //complex
  70. $complexAttributes = setComplexAttribute();
  71. //multiComplex
  72. $multiComplexAttributes = setMultiComplexAttribute();
  73. //数组构建schema的xml格式(不含rules和options)
  74. try {
  75. echo var_dump(ServiceSchemaWriter::writeSchemaXmlString(array($singleAttribute, $multiAttributes, $complexAttributes, $multiComplexAttributes))), PHP_EOL;
  76. } catch (Exception $e) {
  77. var_dump($e->getMessage());
  78. }
  79. //数组构建schema的xml格式(完整格式)
  80. try {
  81. echo var_dump(ServiceSchemaWriter::writeFullchemaXmlString(array($singleAttribute, $multiAttributes, $complexAttributes, $multiComplexAttributes))), PHP_EOL;
  82. } catch (Exception $e) {
  83. var_dump($e->getMessage());
  84. }
  85. function setSingleAttribute($errType=null)
  86. {
  87. $attribute = ServiceSchemaFactory::createAttribute("serviceName", "服务名称", "single", "text");
  88. //构造异常数据
  89. if($errType=='errId'){
  90. $attribute = ServiceSchemaFactory::createAttribute(null, "服务名称", "single", "text");
  91. }elseif ($errType=='errValueType'){
  92. $attribute = ServiceSchemaFactory::createAttribute("serviceName", "服务名称", "single", "text123");
  93. }
  94. //values
  95. $attribute->setValues("服务提报demo");
  96. if($errType=='errValue'){
  97. $attribute->setValues(array("服务提报demo"));
  98. }
  99. //rules
  100. $rules = array(
  101. $errType == 'errRule' ? ServiceSchemaFactory::createRule("required", "必填") : ServiceSchemaFactory::createRule("required", "必填", "true"),
  102. ServiceSchemaFactory::createRule("maxLength", "最大长度", "15")
  103. );
  104. $attribute->setRules($rules);
  105. //options
  106. $options = array(
  107. $errType == 'errOption' ? ServiceSchemaFactory::createOption("支付宝小程序") : ServiceSchemaFactory::createOption("支付宝小程序", "SC_MINI_APP"),
  108. ServiceSchemaFactory::createOption("高德小程序", "AMAP_MINI_APP")
  109. );
  110. $attribute->setOptions($options);
  111. return $attribute;
  112. }
  113. function setMultiAttribute($errType=null)
  114. {
  115. $attribute = ServiceSchemaFactory::createAttribute("serviceName", "服务名称", "multi", "text");
  116. //values
  117. $values = array("周一", "周二");
  118. $attribute->setValues($values);
  119. if($errType=='errValue'){
  120. $attribute->setValues("周一");
  121. }
  122. //rules
  123. $rules = array(
  124. ServiceSchemaFactory::createRule("pattern", "正则表达式", "[\\u4E00-\\u9FA5A-Za-z0-9_+\\-()()]+"),
  125. ServiceSchemaFactory::createRule("minLength", "最小长度", "2")
  126. );
  127. $attribute->setRules($rules);
  128. //options
  129. $options = array(
  130. ServiceSchemaFactory::createOption("UC小程序地址", "UC_MINI_APP"),
  131. ServiceSchemaFactory::createOption("夸克小程序地址", "QUARK_MINI_APP")
  132. );
  133. $attribute->setOptions($options);
  134. return $attribute;
  135. }
  136. function setComplexAttribute($errType=null)
  137. {
  138. $attributes = ServiceSchemaFactory::createAttribute("serviceName", "服务名称", "complex", "text");
  139. $singleAttribute = setSingleAttribute();
  140. $multiAttributes = setMultiAttribute();
  141. //values
  142. $attributes->setValues(array($singleAttribute, $multiAttributes));
  143. if($errType=='errValue'){
  144. $attributes->setValues("服务提报demo");
  145. }
  146. $attributes->setRules(array(
  147. ServiceSchemaFactory::createRule("pattern", "正则表达式", "[\\u4E00-\\u9FA5A-Za-z0-9_+\\-()()]+"),
  148. ServiceSchemaFactory::createRule("minLength", "最小长度", "2")
  149. ));
  150. return $attributes;
  151. }
  152. function setMultiComplexAttribute($errType=null)
  153. {
  154. $attribute = ServiceSchemaFactory::createAttribute("serviceName", "服务名称", "multiComplex", "text");
  155. $attributesComplex = setComplexAttribute();
  156. //values
  157. $values = array($attributesComplex, $attributesComplex);
  158. $attribute->setValues($values);
  159. if($errType=='errValue'){
  160. $attribute->setValues(array("服务提报demo"));
  161. }
  162. return $attribute;
  163. }