BxDolXmlParser.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. $sTag = strtoupper($sXmlTag);
  44. if(!isset($aIndexes[$sTag]))
  45. return false;
  46. $iFieldIndex = $aIndexes[$sTag][0];
  47. $sAttribute = strtoupper($sXmlAttribute);
  48. if(!isset($aValues[$iFieldIndex]['attributes'][$sAttribute]))
  49. return false;
  50. return $aValues[$iFieldIndex]['attributes'][$sAttribute];
  51. }
  52. /**
  53. * Get an array of attributes for specified tag or an array of tags with the same name.
  54. */
  55. function getAttributes($sXmlContent, $sXmlTagName, $sXmlTagIndex = -1)
  56. {
  57. $aValues = $aIndexes = array();
  58. $rParser = xml_parser_create("UTF-8");
  59. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  60. /**
  61. * gets two-dimensional array of attributes.
  62. * tags-attributes
  63. */
  64. if($sXmlTagIndex == -1) {
  65. $aResult = array();
  66. $aTagIndexes = $aIndexes[strtoupper($sXmlTagName)];
  67. if(count($aTagIndexes) <= 0) return NULL;
  68. foreach($aTagIndexes as $iTagIndex)
  69. $aResult[] = $aValues[$iTagIndex]['attributes'];
  70. return $aResult;
  71. } else {
  72. if (isset($aIndexes[strtoupper($sXmlTagName)][$sXmlTagIndex])){
  73. $iTagIndex = $aIndexes[strtoupper($sXmlTagName)][$sXmlTagIndex];
  74. return $aValues[$iTagIndex]['attributes'];
  75. }
  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. //--- Get an array of tags ---//
  87. if($iXmlTagIndex == -1) {
  88. $aResult = array();
  89. if (isset($aIndexes[strtoupper($sXmlTagName)])){
  90. $aTagIndexes = $aIndexes[strtoupper($sXmlTagName)];
  91. if(count($aTagIndexes) <= 0) return NULL;
  92. foreach($aTagIndexes as $iTagIndex)
  93. $aResult[] = $aValues[$iTagIndex];
  94. return $aResult;
  95. }
  96. } else {
  97. $iTagIndex = $aIndexes[strtoupper($sXmlTagName)][$iXmlTagIndex];
  98. return $aValues[$iTagIndex];
  99. }
  100. }
  101. /**
  102. * Gets the values of the given tag with some attribute. 'name' is used as default attribute.
  103. *
  104. * Usage:
  105. * Input:
  106. * <string name="string1"><![CDATA[value1]]></string>
  107. * <string name="string2"><![CDATA[value2]]></string>
  108. * <string name="string3"><![CDATA[value3]]></string>
  109. * Output:
  110. * array(
  111. * 'string1' => 'value1',
  112. * 'string2' => 'value2',
  113. * 'string3' => 'value3'
  114. * )
  115. */
  116. function getValues($sXmlContent, $sXmlTagName, $sXmlAttrName = 'name')
  117. {
  118. $sXmlAttrName = strtoupper($sXmlAttrName);
  119. $aValues = $aIndexes = array();
  120. $rParser = xml_parser_create("UTF-8");
  121. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  122. $sTag = strtoupper($sXmlTagName);
  123. $aTagIndexes = isset($aIndexes[$sTag]) ? $aIndexes[$sTag] : array();
  124. $aReturnValues = array();
  125. foreach($aTagIndexes as $iTagIndex) {
  126. if(!isset($aValues[$iTagIndex]['attributes'][$sXmlAttrName]))
  127. continue;
  128. $aReturnValues[$aValues[$iTagIndex]['attributes'][$sXmlAttrName]] = isset($aValues[$iTagIndex]['value']) ? $aValues[$iTagIndex]['value'] : NULL;
  129. }
  130. return $aReturnValues;
  131. }
  132. /**
  133. * Sets the values of tag where attribute "key" equals to specified.
  134. */
  135. function setValues($sXmlContent, $sXmlTagName, $aKeyValues)
  136. {
  137. $aValues = $aIndexes = array();
  138. $rParser = xml_parser_create("UTF-8");
  139. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  140. $aTagIndexes = $aIndexes[strtoupper($sXmlTagName)];
  141. if(count($aTagIndexes) == 0) return $this->getContent();
  142. foreach($aTagIndexes as $iTagIndex)
  143. foreach($aKeyValues as $sKey => $sValue)
  144. if($aValues[$iTagIndex]['attributes']['KEY'] == $sKey) {
  145. $aValues[$iTagIndex]['value'] = $sValue;
  146. break;
  147. }
  148. return $this->getContent($aValues);
  149. }
  150. /**
  151. * Adds given values to XML content.
  152. */
  153. function addValues($sXmlContent, $sXmlTagName, $aKeyValues)
  154. {
  155. $aValues = $aIndexes = array();
  156. $rParser = xml_parser_create("UTF-8");
  157. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  158. $aTagIndexes = $aIndexes[strtoupper($sXmlTagName)];
  159. $iLastTagIndex = $aTagIndexes[count($aTagIndexes) - 1];
  160. $iAddsCount = count($aKeyValues);
  161. $iLevel = $aValues[$iLastTagIndex]["level"];
  162. for($i=count($aValues)-1; $i>$iLastTagIndex; $i--)
  163. $aValues[$i+$iAddsCount] = $aValues[$i];
  164. $i = $iLastTagIndex;
  165. foreach($aKeyValues as $sKey => $sValue) {
  166. $i++;
  167. $aValues[$i] = Array("tag" => $sXmlTagName, "type" => "complete", "level" => $iLevel, "attributes" => Array("KEY" => $sKey), "value" => $sValue);
  168. }
  169. return $this->getContent($aValues);
  170. }
  171. /**
  172. * get content in XML format from given values array
  173. */
  174. function getContent($aValues = array())
  175. {
  176. $sContent = "";
  177. foreach($aValues as $aValue) {
  178. $sTagName = strtolower($aValue['tag']);
  179. switch($aValue['type']) {
  180. case "open":
  181. $sContent .= "<" . $sTagName . ">";
  182. break;
  183. case "complete":
  184. $sContent .= "<" . $sTagName;
  185. if(isset($aValue['attributes']))
  186. foreach($aValue['attributes'] as $sAttrKey => $sAttrValue)
  187. $sContent .= " " . strtolower($sAttrKey) . "=\"" . $sAttrValue . "\"";
  188. $sContent .= isset($aValue['value']) ? "><![CDATA[" . $aValue['value'] . "]]></" . $sTagName . ">" : " />";
  189. break;
  190. case "close":
  191. $sContent .= "</" . $sTagName . ">";
  192. break;
  193. }
  194. }
  195. return $sContent;
  196. }
  197. }
  198. /** @} */