]> git.mxchange.org Git - friendica.git/blobdiff - include/xml.php
OStatus: Salmon now works with "likes"/The alias is stored now as well
[friendica.git] / include / xml.php
index c2313648cece89af0f171529a53286515d893009..76ad88cf48571002b60d9af868c565d6b31f857e 100644 (file)
@@ -1,10 +1,26 @@
 <?php
+/**
+ * @file include/xml.php
+ */
+
+
 /**
  * @brief This class contain functions to work with XML data
  *
  */
 class xml {
-       function from_array($array, &$xml, $remove_header = false, $namespaces = array(), $root = true) {
+       /**
+        * @brief 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 createdXML 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 The created XML
+        */
+       public static function from_array($array, &$xml, $remove_header = false, $namespaces = array(), $root = true) {
 
                if ($root) {
                        foreach($array as $key => $value) {
@@ -60,14 +76,56 @@ class xml {
                }
        }
 
-       function copy(&$source, &$target, $elementname) {
+       /**
+        * @brief Copies an XML object
+        *
+        * @param object $source The XML source
+        * @param object $target The XML target
+        * @param string $elementname Name of the XML element of the target
+        */
+       public static function copy(&$source, &$target, $elementname) {
                if (count($source->children()) == 0)
-                       $target->addChild($elementname, $source);
+                       $target->addChild($elementname, xmlify($source));
                else {
                        $child = $target->addChild($elementname);
                        foreach ($source->children() AS $childfield => $childentry)
                                self::copy($childentry, $child, $childfield);
                }
        }
+
+       /**
+        * @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
+        *
+        * @return object XML element object
+        */
+       public static function create_element($doc, $element, $value = "", $attributes = array()) {
+               $element = $doc->createElement($element, xmlify($value));
+
+               foreach ($attributes AS $key => $value) {
+                       $attribute = $doc->createAttribute($key);
+                       $attribute->value = xmlify($value);
+                       $element->appendChild($attribute);
+               }
+               return $element;
+       }
+
+       /**
+        * @brief Create an XML and append it to the parent object
+        *
+        * @param object $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
+        */
+       public static function add_element($doc, $parent, $element, $value = "", $attributes = array()) {
+               $element = self::create_element($doc, $element, $value, $attributes);
+               $parent->appendChild($element);
+       }
 }
 ?>