+
+ /**
+ * 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);
+ }