BxDolXmlParser.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php defined('BX_DOL') or defined('BX_DOL_INSTALL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. class BxDolXmlParser extends BxDolFactory implements iBxDolSingleton
  10. {
  11. protected $rParser;
  12. protected function __construct()
  13. {
  14. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  15. trigger_error ('Multiple instances are not allowed for the class: ' . get_class($this), E_USER_ERROR);
  16. parent::__construct();
  17. }
  18. /**
  19. * Prevent cloning the instance
  20. */
  21. public function __clone()
  22. {
  23. if (isset($GLOBALS['bxDolClasses'][get_class($this)]))
  24. trigger_error('Clone is not allowed for the class: ' . get_class($this), E_USER_ERROR);
  25. }
  26. /**
  27. * Get singleton instance of the class
  28. */
  29. public static function getInstance()
  30. {
  31. if(!isset($GLOBALS['bxDolClasses'][__CLASS__]))
  32. $GLOBALS['bxDolClasses'][__CLASS__] = new BxDolXmlParser();
  33. return $GLOBALS['bxDolClasses'][__CLASS__];
  34. }
  35. /**
  36. * Get the value of specified attribute for specified tag.
  37. */
  38. function getAttribute($sXmlContent, $sXmlTag, $sXmlAttribute)
  39. {
  40. $aValues = $aIndexes = array();
  41. $rParser = xml_parser_create("UTF-8");
  42. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  43. xml_parser_free($rParser);
  44. $sTag = strtoupper($sXmlTag);
  45. if(!isset($aIndexes[$sTag]))
  46. return false;
  47. $iFieldIndex = $aIndexes[$sTag][0];
  48. $sAttribute = strtoupper($sXmlAttribute);
  49. if(!isset($aValues[$iFieldIndex]['attributes'][$sAttribute]))
  50. return false;
  51. return $aValues[$iFieldIndex]['attributes'][$sAttribute];
  52. }
  53. /**
  54. * Get an array of attributes for specified tag or an array of tags with the same name.
  55. */
  56. function getAttributes($sXmlContent, $sXmlTagName, $sXmlTagIndex = -1)
  57. {
  58. $aValues = $aIndexes = array();
  59. $rParser = xml_parser_create("UTF-8");
  60. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  61. xml_parser_free($rParser);
  62. /**
  63. * gets two-dimensional array of attributes.
  64. * tags-attributes
  65. */
  66. if($sXmlTagIndex == -1) {
  67. $aResult = array();
  68. $aTagIndexes = $aIndexes[strtoupper($sXmlTagName)];
  69. if(count($aTagIndexes) <= 0) return NULL;
  70. foreach($aTagIndexes as $iTagIndex)
  71. $aResult[] = $aValues[$iTagIndex]['attributes'];
  72. return $aResult;
  73. } else {
  74. $iTagIndex = $aIndexes[strtoupper($sXmlTagName)][$sXmlTagIndex];
  75. return $aValues[$iTagIndex]['attributes'];
  76. }
  77. }
  78. /**
  79. * Get an array of tags or one tag if its index is specified.
  80. */
  81. function getTags($sXmlContent, $sXmlTagName, $iXmlTagIndex = -1)
  82. {
  83. $aValues = $aIndexes = array();
  84. $rParser = xml_parser_create("UTF-8");
  85. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  86. xml_parser_free($rParser);
  87. //--- Get an array of tags ---//
  88. if($iXmlTagIndex == -1) {
  89. $aResult = array();
  90. if (isset($aIndexes[strtoupper($sXmlTagName)])){
  91. $aTagIndexes = $aIndexes[strtoupper($sXmlTagName)];
  92. if(count($aTagIndexes) <= 0) return NULL;
  93. foreach($aTagIndexes as $iTagIndex)
  94. $aResult[] = $aValues[$iTagIndex];
  95. return $aResult;
  96. }
  97. } else {
  98. $iTagIndex = $aIndexes[strtoupper($sXmlTagName)][$iXmlTagIndex];
  99. return $aValues[$iTagIndex];
  100. }
  101. }
  102. /**
  103. * Gets the values of the given tag with some attribute. 'name' is used as default attribute.
  104. *
  105. * Usage:
  106. * Input:
  107. * <string name="string1"><![CDATA[value1]]></string>
  108. * <string name="string2"><![CDATA[value2]]></string>
  109. * <string name="string3"><![CDATA[value3]]></string>
  110. * Output:
  111. * array(
  112. * 'string1' => 'value1',
  113. * 'string2' => 'value2',
  114. * 'string3' => 'value3'
  115. * )
  116. */
  117. function getValues($sXmlContent, $sXmlTagName, $sXmlAttrName = 'name')
  118. {
  119. $sXmlAttrName = strtoupper($sXmlAttrName);
  120. $aValues = $aIndexes = array();
  121. $rParser = xml_parser_create("UTF-8");
  122. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  123. xml_parser_free($rParser);
  124. $sTag = strtoupper($sXmlTagName);
  125. $aTagIndexes = isset($aIndexes[$sTag]) ? $aIndexes[$sTag] : array();
  126. $aReturnValues = array();
  127. foreach($aTagIndexes as $iTagIndex) {
  128. if(!isset($aValues[$iTagIndex]['attributes'][$sXmlAttrName]))
  129. continue;
  130. $aReturnValues[$aValues[$iTagIndex]['attributes'][$sXmlAttrName]] = isset($aValues[$iTagIndex]['value']) ? $aValues[$iTagIndex]['value'] : NULL;
  131. }
  132. return $aReturnValues;
  133. }
  134. /**
  135. * Sets the values of tag where attribute "key" equals to specified.
  136. */
  137. function setValues($sXmlContent, $sXmlTagName, $aKeyValues)
  138. {
  139. $aValues = $aIndexes = array();
  140. $rParser = xml_parser_create("UTF-8");
  141. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  142. xml_parser_free($rParser);
  143. $aTagIndexes = $aIndexes[strtoupper($sXmlTagName)];
  144. if(count($aTagIndexes) == 0) return $this->getContent();
  145. foreach($aTagIndexes as $iTagIndex)
  146. foreach($aKeyValues as $sKey => $sValue)
  147. if($aValues[$iTagIndex]['attributes']['KEY'] == $sKey) {
  148. $aValues[$iTagIndex]['value'] = $sValue;
  149. break;
  150. }
  151. return $this->getContent($aValues);
  152. }
  153. /**
  154. * Adds given values to XML content.
  155. */
  156. function addValues($sXmlContent, $sXmlTagName, $aKeyValues)
  157. {
  158. $aValues = $aIndexes = array();
  159. $rParser = xml_parser_create("UTF-8");
  160. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  161. xml_parser_free($rParser);
  162. $aTagIndexes = $aIndexes[strtoupper($sXmlTagName)];
  163. $iLastTagIndex = $aTagIndexes[count($aTagIndexes) - 1];
  164. $iAddsCount = count($aKeyValues);
  165. $iLevel = $aValues[$iLastTagIndex]["level"];
  166. for($i=count($aValues)-1; $i>$iLastTagIndex; $i--)
  167. $aValues[$i+$iAddsCount] = $aValues[$i];
  168. $i = $iLastTagIndex;
  169. foreach($aKeyValues as $sKey => $sValue) {
  170. $i++;
  171. $aValues[$i] = Array("tag" => $sXmlTagName, "type" => "complete", "level" => $iLevel, "attributes" => Array("KEY" => $sKey), "value" => $sValue);
  172. }
  173. return $this->getContent($aValues);
  174. }
  175. /**
  176. * get content in XML format from given values array
  177. */
  178. function getContent($aValues = array())
  179. {
  180. $sContent = "";
  181. foreach($aValues as $aValue) {
  182. $sTagName = strtolower($aValue['tag']);
  183. switch($aValue['type']) {
  184. case "open":
  185. $sContent .= "<" . $sTagName . ">";
  186. break;
  187. case "complete":
  188. $sContent .= "<" . $sTagName;
  189. if(isset($aValue['attributes']))
  190. foreach($aValue['attributes'] as $sAttrKey => $sAttrValue)
  191. $sContent .= " " . strtolower($sAttrKey) . "=\"" . $sAttrValue . "\"";
  192. $sContent .= isset($aValue['value']) ? "><![CDATA[" . $aValue['value'] . "]]></" . $sTagName . ">" : " />";
  193. break;
  194. case "close":
  195. $sContent .= "</" . $sTagName . ">";
  196. break;
  197. }
  198. }
  199. return $sContent;
  200. }
  201. }
  202. /** @} */