/package/app/app/infra/general/kXml.class.php

https://github.com/richhl/kalturaCE · PHP · 231 lines · 165 code · 18 blank · 48 comment · 22 complexity · 6cb23996e8198226a51305f0a700c353 MD5 · raw file

  1. <?php
  2. /**
  3. * @package infra
  4. * @subpackage utils
  5. */
  6. class kXml
  7. {
  8. public static function getLibXmlErrorDescription($xml)
  9. {
  10. $errors = libxml_get_errors();
  11. if(!count($errors))
  12. return null;
  13. $lines = explode("\r", $xml);
  14. $errorsMsg = array();
  15. foreach($errors as $error)
  16. {
  17. $lineNum = ($error->line) - 1;
  18. $line = htmlspecialchars(isset($lines[$lineNum]) ? '[' . $lines[$lineNum] . ']' : '');
  19. $msg = htmlspecialchars($error->message);
  20. $errorsMsg[] = "$msg at line $error->line $line";
  21. }
  22. return implode("\n", $errorsMsg);
  23. }
  24. public static function getFirstElement ( $xml_node , $element_name , $xpath_str = null )
  25. {
  26. if ( $xpath_str )
  27. {
  28. if ( isset ( $xml_node->my_xpath ) )
  29. {
  30. $xpath = $xml_node->my_xpath;
  31. }
  32. else
  33. {
  34. $xapth = new DOMXPath($xml_node);
  35. // store it for next time
  36. $xml_node->my_xpath = $xpath ;
  37. }
  38. $elem_list = $xpath->query($xpath_str );
  39. }
  40. else
  41. {
  42. $elem_list = $xml_node->getElementsByTagName( $element_name );
  43. }
  44. if ( $elem_list ) return $elem_list->item(0);
  45. else return null;
  46. }
  47. public static function getLastElement ( $xml_node , $element_name , $xpath_str = null )
  48. {
  49. if ( $xpath_str )
  50. {
  51. if ( isset ( $xml_node->my_xpath ) )
  52. {
  53. $xpath = $xml_node->my_xpath;
  54. }
  55. else
  56. {
  57. $xapth = new DOMXPath($xml_node);
  58. // store it for next time
  59. $xml_node->my_xpath = $xpath ;
  60. }
  61. $elem_list = $xpath->query($xpath_str );
  62. }
  63. else
  64. {
  65. $elem_list = $xml_node->getElementsByTagName( $element_name );
  66. }
  67. if ( $elem_list->length > 0 )
  68. return $elem_list->item($elem_list->length - 1);
  69. else
  70. return null;
  71. }
  72. public static function getFirstElementAsText ( DOMDocument $xml_doc , $element_name , $xpath_str = null )
  73. {
  74. $node = self::getFirstElement($xml_doc, $element_name, $xpath_str);
  75. return $node ? $node->nodeValue : "";
  76. }
  77. public static function getLastElementAsText ( DOMDocument $xml_doc , $element_name , $xpath_str = null )
  78. {
  79. $node = self::getLastElement($xml_doc, $element_name, $xpath_str);
  80. return $node ? $node->nodeValue : "";
  81. }
  82. // manipulate the xml_dom
  83. // @return if the xml_doc was modified
  84. public static function setChildElement ( DOMDocument &$xml_doc , $parent_element ,
  85. $element_name , $element_value, $remove_element_if_empty_value = false )
  86. {
  87. $modified = true;
  88. $elem = self::getFirstElement ( $xml_doc , $element_name );
  89. if ( $elem )
  90. {
  91. // element aleardy exists
  92. if ( empty ( $element_value ) && $remove_element_if_empty_value )
  93. {
  94. // new value is empty - and should remove - remove !
  95. $parent_element->removeChild ( $elem );
  96. }
  97. else
  98. {
  99. if( $elem->nodeValue != $element_value )
  100. {
  101. $elem->nodeValue = $element_value ;
  102. }
  103. else
  104. {
  105. $modified = false;
  106. }
  107. }
  108. }
  109. else
  110. {
  111. // element does not exist - and no reason to create it
  112. if ( empty ( $element_value ) && $remove_element_if_empty_value )
  113. {
  114. $modified = false;
  115. }
  116. else
  117. {
  118. if (!$parent_element)
  119. {
  120. debugUtils::st();
  121. return false;
  122. }
  123. // need to create and set the value
  124. $elem = $xml_doc->createElement( $element_name , $element_value );
  125. $parent_element->appendChild ( $elem );
  126. }
  127. }
  128. return $modified;
  129. }
  130. /**
  131. *
  132. * Sets the given object's given property value
  133. * @param unknown_type $objectInstace
  134. * @param $fieldName
  135. * @param unknown_type $fieldValue
  136. */
  137. private static function setPropertyValue(&$objectInstace, $fieldName, $fieldValue, $fieldValueType)
  138. {
  139. //set the object to this value
  140. if($objectInstace instanceof BaseObject)
  141. {
  142. $objectInstace->setByName($fieldName, $fieldValue);
  143. }
  144. else if($objectInstace instanceof KalturaObjectBase)
  145. {
  146. $objectInstace->$fieldName = $fieldValue;
  147. }
  148. else
  149. {
  150. //Set the attribute to its right type
  151. settype($fieldValue, $fieldValueType);
  152. $objectInstace = $fieldValue;
  153. }
  154. }
  155. /**
  156. *
  157. * Gets a xml attribute if one exists Safe method (no exception is thrown)
  158. * @param unknown_type $object
  159. * @param unknown_type $attribute
  160. * @return string - the attribute value if such exists
  161. */
  162. public static function getXmlAttributeAsString($object, $attribute)
  163. {
  164. if(isset($object[$attribute]))
  165. return (string) $object[$attribute];
  166. }
  167. /**
  168. *
  169. * Gets a xml attribute if one exists Safe method (no exception is thrown)
  170. * @param unknown_type $object
  171. * @param unknown_type $attribute
  172. * @return int - the attribute value if such exists
  173. */
  174. public static function getXmlAttributeAsInt($object, $attribute)
  175. {
  176. if(isset($object[$attribute]))
  177. return (string) $object[$attribute];
  178. }
  179. /**
  180. *
  181. * creates the additional data from the given xml object
  182. * @param SimpleXMLElement $xmlobject
  183. * @return array<key => value> - the additional data as key / value pair
  184. */
  185. public static function getAttributesAsArray(SimpleXMLElement $xmlobject)
  186. {
  187. $attributesArray = array();
  188. foreach ($xmlobject->attributes() as $attributeKey => $attributeValue)
  189. {
  190. $attributesArray[$attributeKey] = (string)$attributeValue;
  191. }
  192. return $attributesArray;
  193. }
  194. /**
  195. *
  196. * Opens a given xml and returns it as a simpleXMLElement
  197. * @param string $xmlFilePath - the xml file path
  198. * @return simpleXMLElement - the xml
  199. */
  200. public static function openXmlFile($xmlFilePath)
  201. {
  202. try
  203. {
  204. $simpleXML = simplexml_load_file($xmlFilePath);
  205. }
  206. catch(Exception $e)
  207. {
  208. //TODO: exception handling
  209. throw new Exception("Unable to load file : " . $xmlFilePath. " as xml.\n Error: " . $e->getMessage());
  210. }
  211. return $simpleXML;
  212. }
  213. }