]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/XML.php
Update function / rearrange tab order
[friendica.git] / src / Util / XML.php
index 7933f4415b6f9a21677cffe7e090e24c7dd278f2..c35f19b8a8e584619b4b14ca9ea8349c09d1da0e 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -37,14 +37,15 @@ class XML
        /**
         * Creates an XML structure out of a given array
         *
-        * @param array  $array         The array of the XML structure that will be generated
-        * @param object $xml           The created XML will be returned by reference
-        * @param bool   $remove_header Should the XML header be removed or not?
-        * @param array  $namespaces    List of namespaces
-        * @param bool   $root          interally used parameter. Mustn't be used from outside.
+        * @param array       $array         The array of the XML structure that will be generated
+        * @param object|null $xml           The created XML will be returned by reference
+        * @param bool        $remove_header Should the XML header be removed or not?
+        * @param array       $namespaces    List of namespaces
+        * @param bool        $root          interally used parameter. Mustn't be used from outside.
         * @return string
+        * @throws \Exception
         */
-       public static function fromArray(array $array, &$xml, bool $remove_header = false, array $namespaces = [], bool $root = true): string
+       public static function fromArray(array $array, object &$xml = null, bool $remove_header = false, array $namespaces = [], bool $root = true): string
        {
                if ($root) {
                        foreach ($array as $key => $value) {
@@ -117,7 +118,7 @@ class XML
                                                $namespace = null;
                                        }
 
-                                       $element->addAttribute($attr_key, $attr_value, $namespace);
+                                       $element->addAttribute($attr_key, $attr_value ?? '', $namespace);
                                }
 
                                continue;
@@ -125,7 +126,7 @@ class XML
 
                        if (!is_array($value)) {
                                $element = $xml->addChild($key, self::escape($value ?? ''), $namespace);
-                       } elseif (is_array($value)) {
+                       } else {
                                $element = $xml->addChild($key, null, $namespace);
                                self::fromArray($value, $element, $remove_header, $namespaces, false);
                        }
@@ -258,7 +259,7 @@ class XML
         * @param integer $get_attributes   1 or 0. If this is 1 the function will get the attributes as well as the tag values -
         *                                  this results in a different array structure in the return value.
         * @param string  $priority         Can be 'tag' or 'attribute'. This will change the way the resulting
-        *                                  array sturcture. For 'tag', the tags are given more importance.
+        *                                  array structure. For 'tag', the tags are given more importance.
         *
         * @return array The parsed XML in an array form. Use print_r() to see the resulting array structure.
         * @throws \Exception
@@ -396,7 +397,7 @@ class XML
                                                }
                                                $repeated_tag_index[$tag . '_' . $level]++;
                                        } else { // If it is not an array...
-                                               $current[$tag] = [$current[$tag], $result]; //...Make it an array using using the existing value and the new value
+                                               $current[$tag] = [$current[$tag], $result]; //...Make it an array using the existing value and the new value
                                                $repeated_tag_index[$tag . '_' . $level] = 1;
                                                if ($priority == 'tag' and $get_attributes) {
                                                        if (isset($current[$tag.'_attr'])) { // The attribute of the last(0th) tag must be moved as well
@@ -440,7 +441,7 @@ class XML
         * Parse XML string
         *
         * @param string  $s XML string to parse into object
-        * @param boolean $suppress_log Whether to supressing logging
+        * @param boolean $suppress_log Whether to suppressing logging
         * @return SimpleXMLElement|bool SimpleXMLElement or false on failure
         */
        public static function parseString(string $s, bool $suppress_log = false)
@@ -535,7 +536,7 @@ class XML
         *
         * @param string $str
         * @return string Escaped text.
-        * @todo Move this generic method to Util\Strings and also rewrite all other findingd
+        * @todo Move this generic method to Util\Strings and also rewrite all other occurrences
         */
        public static function escape(string $str): string
        {
@@ -547,7 +548,7 @@ class XML
         *
         * @param string $s xml escaped text
         * @return string unescaped text
-        * @todo Move this generic method to Util\Strings and also rewrite all other findingd
+        * @todo Move this generic method to Util\Strings and also rewrite all other occurrences
         */
        public static function unescape(string $s): string
        {