BxDolXmlParser.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. $aTagIndexes = $aIndexes[strtoupper($sXmlTagName)];
  91. if(count($aTagIndexes) <= 0) return NULL;
  92. foreach($aTagIndexes as $iTagIndex)
  93. $aResult[] = $aValues[$iTagIndex];
  94. return $aResult;
  95. } else {
  96. $iTagIndex = $aIndexes[strtoupper($sXmlTagName)][$iXmlTagIndex];
  97. return $aValues[$iTagIndex];
  98. }
  99. }
  100. /**
  101. * Gets the values of the given tag.
  102. */
  103. function getValues($sXmlContent, $sXmlTagName)
  104. {
  105. $aValues = $aIndexes = array();
  106. $rParser = xml_parser_create("UTF-8");
  107. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  108. xml_parser_free($rParser);
  109. $sTag = strtoupper($sXmlTagName);
  110. $aTagIndexes = isset($aIndexes[$sTag]) ? $aIndexes[$sTag] : array();
  111. $aReturnValues = array();
  112. foreach($aTagIndexes as $iTagIndex) {
  113. $aReturnValues[$aValues[$iTagIndex]['attributes']['NAME']] =
  114. isset($aValues[$iTagIndex]['value']) ? $aValues[$iTagIndex]['value'] : NULL;
  115. }
  116. return $aReturnValues;
  117. }
  118. /**
  119. * Sets the values of tag where attribute "key" equals to specified.
  120. */
  121. function setValues($sXmlContent, $sXmlTagName, $aKeyValues)
  122. {
  123. $aValues = $aIndexes = array();
  124. $rParser = xml_parser_create("UTF-8");
  125. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  126. xml_parser_free($rParser);
  127. $aTagIndexes = $aIndexes[strtoupper($sXmlTagName)];
  128. if(count($aTagIndexes) == 0) return $this->getContent();
  129. foreach($aTagIndexes as $iTagIndex)
  130. foreach($aKeyValues as $sKey => $sValue)
  131. if($aValues[$iTagIndex]['attributes']['KEY'] == $sKey) {
  132. $aValues[$iTagIndex]['value'] = $sValue;
  133. break;
  134. }
  135. return $this->getContent($aValues);
  136. }
  137. /**
  138. * Adds given values to XML content.
  139. */
  140. function addValues($sXmlContent, $sXmlTagName, $aKeyValues)
  141. {
  142. $aValues = $aIndexes = array();
  143. $rParser = xml_parser_create("UTF-8");
  144. xml_parse_into_struct($rParser, $sXmlContent, $aValues, $aIndexes);
  145. xml_parser_free($rParser);
  146. $aTagIndexes = $aIndexes[strtoupper($sXmlTagName)];
  147. $iLastTagIndex = $aTagIndexes[count($aTagIndexes) - 1];
  148. $iAddsCount = count($aKeyValues);
  149. $iLevel = $aValues[$iLastTagIndex]["level"];
  150. for($i=count($aValues)-1; $i>$iLastTagIndex; $i--)
  151. $aValues[$i+$iAddsCount] = $aValues[$i];
  152. $i = $iLastTagIndex;
  153. foreach($aKeyValues as $sKey => $sValue) {
  154. $i++;
  155. $aValues[$i] = Array("tag" => $sXmlTagName, "type" => "complete", "level" => $iLevel, "attributes" => Array("KEY" => $sKey), "value" => $sValue);
  156. }
  157. return $this->getContent($aValues);
  158. }
  159. /**
  160. * get content in XML format from given values array
  161. */
  162. function getContent($aValues = array())
  163. {
  164. $sContent = "";
  165. foreach($aValues as $aValue) {
  166. $sTagName = strtolower($aValue['tag']);
  167. switch($aValue['type']) {
  168. case "open":
  169. $sContent .= "<" . $sTagName . ">";
  170. break;
  171. case "complete":
  172. $sContent .= "<" . $sTagName;
  173. if(isset($aValue['attributes']))
  174. foreach($aValue['attributes'] as $sAttrKey => $sAttrValue)
  175. $sContent .= " " . strtolower($sAttrKey) . "=\"" . $sAttrValue . "\"";
  176. $sContent .= isset($aValue['value']) ? "><![CDATA[" . $aValue['value'] . "]]></" . $sTagName . ">" : " />";
  177. break;
  178. case "close":
  179. $sContent .= "</" . $sTagName . ">";
  180. break;
  181. }
  182. }
  183. return $sContent;
  184. }
  185. }
  186. /** @} */