+
+ /**
+ * Parse XML string
+ *
+ * @param string $s XML string to parse into object
+ * @param boolean $suppress_log Whether to supressing logging
+ * @return SimpleXMLElement|bool SimpleXMLElement or false on failure
+ */
+ public static function parseString(string $s, bool $suppress_log = false)
+ {
+ libxml_use_internal_errors(true);
+
+ $x = @simplexml_load_string($s);
+ if (!$x) {
+ if (!$suppress_log) {
+ Logger::error('Error(s) while parsing XML string.', ['callstack' => System::callstack()]);
+ foreach (libxml_get_errors() as $err) {
+ Logger::info('libxml error', ['code' => $err->code, 'position' => $err->line . ':' . $err->column, 'message' => $err->message]);
+ }
+ Logger::debug('Erroring XML string', ['xml' => $s]);
+ }
+ libxml_clear_errors();
+ }
+ return $x;
+ }
+
+ /**
+ * Gets first node value
+ *
+ * @param DOMXPath $xpath XPath object
+ * @param string $element Element name
+ * @param DOMNode $context Context object or NULL
+ * @return string XML node value or empty string on failure
+ */
+ public static function getFirstNodeValue(DOMXPath $xpath, string $element, DOMNode $context = null)
+ {
+ $result = @$xpath->evaluate($element, $context);
+ if (!is_object($result)) {
+ return '';
+ }
+
+ $first_item = $result->item(0);
+ if (!is_object($first_item)) {
+ return '';
+ }
+
+ return $first_item->nodeValue;
+ }
+
+ /**
+ * Gets first attributes
+ *
+ * @param DOMXPath $xpath XPath object
+ * @param string $element Element name
+ * @param DOMNode $context Context object or NULL
+ * @return ???|bool First element's attributes field or false on failure
+ */
+ public static function getFirstAttributes(DOMXPath $xpath, string $element, DOMNode $context = null)
+ {
+ $result = @$xpath->query($element, $context);
+ if (!is_object($result)) {
+ return false;
+ }
+
+ $first_item = $result->item(0);
+ if (!is_object($first_item)) {
+ return false;
+ }
+
+ return $first_item->attributes;
+ }
+
+ /**
+ * Gets first node's value
+ *
+ * @param DOMXPath $xpath XPath object
+ * @param string $element Element name
+ * @param DOMNode $context Context object or NULL
+ * @return string First value or empty string on failure
+ */
+ public static function getFirstValue(DOMXPath $xpath, string $element, DOMNode $context = null): string
+ {
+ $result = @$xpath->query($element, $context);
+ if (!is_object($result)) {
+ return '';
+ }
+
+ $first_item = $result->item(0);
+ if (!is_object($first_item)) {
+ return '';
+ }
+
+ return $first_item->nodeValue;
+ }
+
+ /**
+ * escape text ($str) for XML transport
+ *
+ * @param string $str
+ * @return string Escaped text.
+ * @todo Move this generic method to Util\Strings and also rewrite all other occurrences
+ */
+ public static function escape(string $str): string
+ {
+ return trim(htmlspecialchars($str, ENT_QUOTES, 'UTF-8'));
+ }
+
+ /**
+ * Undo an escape
+ *
+ * @param string $s xml escaped text
+ * @return string unescaped text
+ * @todo Move this generic method to Util\Strings and also rewrite all other occurrences
+ */
+ public static function unescape(string $s): string
+ {
+ return htmlspecialchars_decode($s, ENT_QUOTES);
+ }
+
+ /**
+ * Apply escape() to all values of array $val, recursively
+ *
+ * @param array|bool|string $val Value of type bool, array or string
+ * @return array|string Returns array if array provided or string in other cases
+ * @todo Move this generic method to Util\Strings
+ */
+ public static function arrayEscape($val)
+ {
+ if (is_bool($val)) {
+ return $val ? 'true' : 'false';
+ } elseif (is_array($val)) {
+ return array_map('XML::arrayEscape', $val);
+ }
+
+ return self::escape((string) $val);
+ }