]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/XML.php
Don't create notifications for own posts
[friendica.git] / src / Util / XML.php
index d56b2311f3b746d8aac24fffcae7894c45461977..476ecb4b00e7f964325e647472afd8b6bcf345c6 100644 (file)
@@ -4,8 +4,9 @@
  */
 namespace Friendica\Util;
 
-use Friendica\Core\Logger;
 use DOMXPath;
+use Friendica\Core\Logger;
+use Friendica\Core\System;
 use SimpleXMLElement;
 
 /**
@@ -135,14 +136,14 @@ class XML
        /**
         * @brief Create an XML element
         *
-        * @param object $doc        XML root
-        * @param string $element    XML element name
-        * @param string $value      XML value
-        * @param array  $attributes array containing the attributes
+        * @param \DOMDocument $doc        XML root
+        * @param string       $element    XML element name
+        * @param string       $value      XML value
+        * @param array        $attributes array containing the attributes
         *
-        * @return object XML element object
+        * @return \DOMElement XML element object
         */
-       public static function createElement($doc, $element, $value = "", $attributes = [])
+       public static function createElement(\DOMDocument $doc, $element, $value = "", $attributes = [])
        {
                $element = $doc->createElement($element, self::escape($value));
 
@@ -157,14 +158,14 @@ class XML
        /**
         * @brief Create an XML and append it to the parent object
         *
-        * @param object $doc        XML root
+        * @param \DOMDocument $doc        XML root
         * @param object $parent     parent object
         * @param string $element    XML element name
         * @param string $value      XML value
         * @param array  $attributes array containing the attributes
         * @return void
         */
-       public static function addElement($doc, $parent, $element, $value = "", $attributes = [])
+       public static function addElement(\DOMDocument $doc, $parent, $element, $value = "", $attributes = [])
        {
                $element = self::createElement($doc, $element, $value, $attributes);
                $parent->appendChild($element);
@@ -402,11 +403,11 @@ class XML
        /**
         * @brief Delete a node in a XML object
         *
-        * @param object $doc  XML document
+        * @param \DOMDocument $doc  XML document
         * @param string $node Node name
         * @return void
         */
-       public static function deleteNode(&$doc, $node)
+       public static function deleteNode(\DOMDocument $doc, $node)
        {
                $xpath = new DOMXPath($doc);
                $list = $xpath->query("//".$node);
@@ -422,16 +423,17 @@ class XML
 
                $x = @simplexml_load_string($s);
                if (!$x) {
-                       Logger::log('libxml: parse: error: ' . $s, Logger::DATA);
+                       Logger::error('Error(s) while parsing XML string.', ['callstack' => System::callstack()]);
                        foreach (libxml_get_errors() as $err) {
-                               Logger::log('libxml: parse: ' . $err->code." at ".$err->line.":".$err->column." : ".$err->message, Logger::DATA);
+                               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;
        }
 
-       public static function getFirstNodeValue($xpath, $element, $context = null)
+       public static function getFirstNodeValue(DOMXPath $xpath, $element, $context = null)
        {
                $result = $xpath->evaluate($element, $context);
                if (!is_object($result)) {
@@ -446,7 +448,7 @@ class XML
                return $first_item->nodeValue;
        }
 
-       public static function getFirstAttributes($xpath, $element, $context = null)
+       public static function getFirstAttributes(DOMXPath $xpath, $element, $context = null)
        {
                $result = $xpath->query($element, $context);
                if (!is_object($result)) {
@@ -463,12 +465,13 @@ class XML
 
        /**
         * escape text ($str) for XML transport
+        *
         * @param string $str
         * @return string Escaped text.
         */
        public static function escape($str)
        {
-               $buffer = htmlspecialchars($str, ENT_QUOTES, "UTF-8");
+               $buffer = htmlspecialchars($str, ENT_QUOTES, 'UTF-8');
                $buffer = trim($buffer);
 
                return $buffer;
@@ -476,6 +479,7 @@ class XML
 
        /**
         * undo an escape
+        *
         * @param string $s xml escaped text
         * @return string unescaped text
         */
@@ -487,16 +491,18 @@ class XML
 
        /**
         * apply escape() to all values of array $val, recursively
+        *
         * @param array $val
-        * @return array
+        * @return array|string
         */
        public static function arrayEscape($val)
        {
                if (is_bool($val)) {
-                       return $val?"true":"false";
+                       return $val ? 'true' : 'false';
                } elseif (is_array($val)) {
                        return array_map('XML::arrayEscape', $val);
                }
+
                return self::escape((string) $val);
        }
 }