Example #1
0
 /**
  * serialize an array
  *
  * @param array  &$array     array to serialize
  * @param string $tagName    name of the root tag
  * @param array  $attributes attributes for the root tag
  *
  * @return string $string serialized data
  * @access private
  * @uses XML_Util::isValidName() to check, whether key has to be substituted
  * @uses XML_Util::replaceEntities()
  * @uses XML_Util::createComment()
  * @uses PEAR::popExpect()
  * @uses PEAR::expectError()
  */
 function _serializeArray(&$array, $tagName = null, $attributes = array())
 {
     $_content = null;
     $_comment = null;
     // check for comment
     if ($this->options[XML_SERIALIZER_OPTION_COMMENT_KEY] !== null) {
         if (isset($array[$this->options[XML_SERIALIZER_OPTION_COMMENT_KEY]])) {
             $_comment = $array[$this->options[XML_SERIALIZER_OPTION_COMMENT_KEY]];
             unset($array[$this->options[XML_SERIALIZER_OPTION_COMMENT_KEY]]);
         }
     }
     /**
      * check for special attributes
      */
     if ($this->options[XML_SERIALIZER_OPTION_ATTRIBUTES_KEY] !== null) {
         if (isset($array[$this->options[XML_SERIALIZER_OPTION_ATTRIBUTES_KEY]])) {
             $attributes = $array[$this->options[XML_SERIALIZER_OPTION_ATTRIBUTES_KEY]];
             unset($array[$this->options[XML_SERIALIZER_OPTION_ATTRIBUTES_KEY]]);
         }
         /**
          * check for special content
          */
         if ($this->options[XML_SERIALIZER_OPTION_CONTENT_KEY] !== null) {
             if (isset($array[$this->options[XML_SERIALIZER_OPTION_CONTENT_KEY]])) {
                 $_content = XML_Util::replaceEntities($array[$this->options[XML_SERIALIZER_OPTION_CONTENT_KEY]]);
                 unset($array[$this->options[XML_SERIALIZER_OPTION_CONTENT_KEY]]);
             }
         }
     }
     if ($this->options[XML_SERIALIZER_OPTION_IGNORE_NULL] === true) {
         foreach (array_keys($array) as $key) {
             if (is_null($array[$key])) {
                 unset($array[$key]);
             }
         }
     }
     /*
      * if mode is set to simpleXML, check whether
      * the array is associative or indexed
      */
     if (is_array($array) && !empty($array) && $this->options[XML_SERIALIZER_OPTION_MODE] == XML_SERIALIZER_MODE_SIMPLEXML) {
         $indexed = true;
         foreach ($array as $key => $val) {
             if (!is_int($key)) {
                 $indexed = false;
                 break;
             }
         }
         if ($indexed && $this->options[XML_SERIALIZER_OPTION_MODE] == XML_SERIALIZER_MODE_SIMPLEXML) {
             $string = '';
             foreach ($array as $key => $val) {
                 $string .= $this->_serializeValue($val, $tagName, $attributes);
                 $string .= $this->options[XML_SERIALIZER_OPTION_LINEBREAKS];
                 // do indentation
                 if ($this->options[XML_SERIALIZER_OPTION_INDENT] !== null && $this->_tagDepth > 0) {
                     $string .= str_repeat($this->options[XML_SERIALIZER_OPTION_INDENT], $this->_tagDepth);
                 }
             }
             return rtrim($string);
         }
     }
     $scalarAsAttributes = false;
     if (is_array($this->options[XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES]) && isset($this->options[XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES][$tagName])) {
         $scalarAsAttributes = $this->options[XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES][$tagName];
     } elseif ($this->options[XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES] === true) {
         $scalarAsAttributes = true;
     }
     if ($scalarAsAttributes === true) {
         $this->expectError('*');
         foreach ($array as $key => $value) {
             if (is_scalar($value) && XML_Util::isValidName($key) === true) {
                 unset($array[$key]);
                 $attributes[$this->options[XML_SERIALIZER_OPTION_PREPEND_ATTRIBUTES] . $key] = $value;
             }
         }
         $this->popExpect();
     } elseif (is_array($scalarAsAttributes)) {
         $this->expectError('*');
         foreach ($scalarAsAttributes as $key) {
             if (!isset($array[$key])) {
                 continue;
             }
             $value = $array[$key];
             if (is_scalar($value) && XML_Util::isValidName($key) === true) {
                 unset($array[$key]);
                 $attributes[$this->options[XML_SERIALIZER_OPTION_PREPEND_ATTRIBUTES] . $key] = $value;
             }
         }
         $this->popExpect();
     }
     // check for empty array => create empty tag
     if (empty($array)) {
         $tag = array('qname' => $tagName, 'content' => $_content, 'attributes' => $attributes);
     } else {
         $this->_tagDepth++;
         $tmp = $_content . $this->options[XML_SERIALIZER_OPTION_LINEBREAKS];
         foreach ($array as $key => $value) {
             // do indentation
             if ($this->options[XML_SERIALIZER_OPTION_INDENT] !== null && $this->_tagDepth > 0) {
                 $tmp .= str_repeat($this->options[XML_SERIALIZER_OPTION_INDENT], $this->_tagDepth);
             }
             // copy key
             $origKey = $key;
             $this->expectError('*');
             // key cannot be used as tagname => use default tag
             $valid = XML_Util::isValidName($key);
             $this->popExpect();
             if (PEAR::isError($valid)) {
                 if ($this->options[XML_SERIALIZER_OPTION_CLASSNAME_AS_TAGNAME] && is_object($value)) {
                     $key = get_class($value);
                 } else {
                     $key = $this->_getDefaultTagname($tagName);
                 }
             }
             // once we've established the true $key, is there a tagmap for it?
             if (isset($this->options[XML_SERIALIZER_OPTION_TAGMAP][$key])) {
                 $key = $this->options[XML_SERIALIZER_OPTION_TAGMAP][$key];
             }
             $atts = array();
             if ($this->options[XML_SERIALIZER_OPTION_TYPEHINTS] === true) {
                 $atts[$this->options[XML_SERIALIZER_OPTION_ATTRIBUTE_TYPE]] = gettype($value);
                 if ($key !== $origKey) {
                     $atts[$this->options[XML_SERIALIZER_OPTION_ATTRIBUTE_KEY]] = (string) $origKey;
                 }
             }
             $tmp .= $this->_createXMLTag(array('qname' => $key, 'attributes' => $atts, 'content' => $value));
             $tmp .= $this->options[XML_SERIALIZER_OPTION_LINEBREAKS];
         }
         $this->_tagDepth--;
         if ($this->options[XML_SERIALIZER_OPTION_INDENT] !== null && $this->_tagDepth > 0) {
             $tmp .= str_repeat($this->options[XML_SERIALIZER_OPTION_INDENT], $this->_tagDepth);
         }
         if (trim($tmp) === '') {
             $tmp = null;
         }
         $tag = array('qname' => $tagName, 'content' => $tmp, 'attributes' => $attributes);
     }
     if ($this->options[XML_SERIALIZER_OPTION_TYPEHINTS] === true) {
         if (!isset($tag['attributes'][$this->options[XML_SERIALIZER_OPTION_ATTRIBUTE_TYPE]])) {
             $tag['attributes'][$this->options[XML_SERIALIZER_OPTION_ATTRIBUTE_TYPE]] = 'array';
         }
     }
     $string = '';
     if (!is_null($_comment)) {
         $string .= XML_Util::createComment($_comment);
         $string .= $this->options[XML_SERIALIZER_OPTION_LINEBREAKS];
         if ($this->options[XML_SERIALIZER_OPTION_INDENT] !== null && $this->_tagDepth > 0) {
             $string .= str_repeat($this->options[XML_SERIALIZER_OPTION_INDENT], $this->_tagDepth);
         }
     }
     $string .= $this->_createXMLTag($tag, false);
     return $string;
 }
Example #2
0
 /**
  * serialize an array
  *
  * @access   private
  * @param    array   $array       array to serialize
  * @param    string  $tagName     name of the root tag
  * @param    array   $attributes  attributes for the root tag
  * @return   string  $string      serialized data
  * @uses     XML_Util::isValidName() to check, whether key has to be substituted
  */
 function _serializeArray(&$array, $tagName = null, $attributes = array())
 {
     $_content = null;
     /**
      * check for special attributes
      */
     if ($this->options['attributesArray'] !== null) {
         if (isset($array[$this->options['attributesArray']])) {
             $attributes = $array[$this->options['attributesArray']];
             unset($array[$this->options['attributesArray']]);
         }
         /**
          * check for special content
          */
         if ($this->options['contentName'] !== null) {
             if (isset($array[$this->options['contentName']])) {
                 $_content = $array[$this->options['contentName']];
                 unset($array[$this->options['contentName']]);
             }
         }
     }
     /*
      * if mode is set to simpleXML, check whether
      * the array is associative or indexed
      */
     if (is_array($array) && $this->options['mode'] == 'simplexml') {
         $indexed = true;
         if (!count($array)) {
             $indexed = false;
         }
         foreach ($array as $key => $val) {
             if (!is_int($key)) {
                 $indexed = false;
                 break;
             }
         }
         if ($indexed && $this->options['mode'] == 'simplexml') {
             $string = '';
             foreach ($array as $key => $val) {
                 if ($this->options['beautifyFilelist'] && $tagName == 'dir') {
                     if (!isset($this->_curdir)) {
                         $this->_curdir = '';
                     }
                     $savedir = $this->_curdir;
                     if (isset($val['attribs'])) {
                         if ($val['attribs']['name'] == '/') {
                             $this->_curdir = '/';
                         } else {
                             if ($this->_curdir == '/') {
                                 $this->_curdir = '';
                             }
                             $this->_curdir .= '/' . $val['attribs']['name'];
                         }
                     }
                 }
                 $string .= $this->_serializeValue($val, $tagName, $attributes);
                 if ($this->options['beautifyFilelist'] && $tagName == 'dir') {
                     $string .= ' <!-- ' . $this->_curdir . ' -->';
                     if (empty($savedir)) {
                         unset($this->_curdir);
                     } else {
                         $this->_curdir = $savedir;
                     }
                 }
                 $string .= $this->options['linebreak'];
                 //	do indentation
                 if ($this->options['indent'] !== null && $this->_tagDepth > 0) {
                     $string .= str_repeat($this->options['indent'], $this->_tagDepth);
                 }
             }
             return rtrim($string);
         }
     }
     if ($this->options['scalarAsAttributes'] === true) {
         foreach ($array as $key => $value) {
             if (is_scalar($value) && XML_Util::isValidName($key) === true) {
                 unset($array[$key]);
                 $attributes[$this->options['prependAttributes'] . $key] = $value;
             }
         }
     }
     // check for empty array => create empty tag
     if (empty($array)) {
         $tag = array('qname' => $tagName, 'content' => $_content, 'attributes' => $attributes);
     } else {
         $this->_tagDepth++;
         $tmp = $this->options['linebreak'];
         foreach ($array as $key => $value) {
             //	do indentation
             if ($this->options['indent'] !== null && $this->_tagDepth > 0) {
                 $tmp .= str_repeat($this->options['indent'], $this->_tagDepth);
             }
             //	copy key
             $origKey = $key;
             //	key cannot be used as tagname => use default tag
             $valid = XML_Util::isValidName($key);
             if (PEAR::isError($valid)) {
                 if ($this->options['classAsTagName'] && is_object($value)) {
                     $key = get_class($value);
                 } else {
                     $key = $this->options['defaultTagName'];
                 }
             }
             $atts = array();
             if ($this->options['typeHints'] === true) {
                 $atts[$this->options['typeAttribute']] = gettype($value);
                 if ($key !== $origKey) {
                     $atts[$this->options['keyAttribute']] = (string) $origKey;
                 }
             }
             if ($this->options['beautifyFilelist'] && $key == 'dir') {
                 if (!isset($this->_curdir)) {
                     $this->_curdir = '';
                 }
                 $savedir = $this->_curdir;
                 if (isset($value['attribs'])) {
                     if ($value['attribs']['name'] == '/') {
                         $this->_curdir = '/';
                     } else {
                         $this->_curdir .= '/' . $value['attribs']['name'];
                     }
                 }
             }
             if (is_string($value) && $value && $value[strlen($value) - 1] == "\n") {
                 $value .= str_repeat($this->options['indent'], $this->_tagDepth);
             }
             $tmp .= $this->_createXMLTag(array('qname' => $key, 'attributes' => $atts, 'content' => $value));
             if ($this->options['beautifyFilelist'] && $key == 'dir') {
                 if (isset($value['attribs'])) {
                     $tmp .= ' <!-- ' . $this->_curdir . ' -->';
                     if (empty($savedir)) {
                         unset($this->_curdir);
                     } else {
                         $this->_curdir = $savedir;
                     }
                 }
             }
             $tmp .= $this->options['linebreak'];
         }
         $this->_tagDepth--;
         if ($this->options['indent'] !== null && $this->_tagDepth > 0) {
             $tmp .= str_repeat($this->options['indent'], $this->_tagDepth);
         }
         if (trim($tmp) === '') {
             $tmp = null;
         }
         $tag = array('qname' => $tagName, 'content' => $tmp, 'attributes' => $attributes);
     }
     if ($this->options['typeHints'] === true) {
         if (!isset($tag['attributes'][$this->options['typeAttribute']])) {
             $tag['attributes'][$this->options['typeAttribute']] = 'array';
         }
     }
     $string = $this->_createXMLTag($tag, false);
     return $string;
 }
 /**
  * serialize an array
  *
  * @access   private
  * @param    array   $array       array to serialize
  * @param    string  $tagName     name of the root tag
  * @param    array   $attributes  attributes for the root tag
  * @return   string  $string      serialized data
  * @uses     XML_Util::isValidName() to check, whether key has to be substituted
  */
 function _serializeArray(&$array, $tagName = null, $attributes = array())
 {
     /**
      * check for special attributes
      */
     if ($this->options['attributesArray'] !== null) {
         $_content = null;
         if (isset($array[$this->options['attributesArray']])) {
             $attributes = $array[$this->options['attributesArray']];
             unset($array[$this->options['attributesArray']]);
         }
         /**
          * check for special content
          */
         if ($this->options['contentName'] !== null) {
             if (isset($array[$this->options['contentName']])) {
                 $_content = $array[$this->options['contentName']];
                 unset($array[$this->options['contentName']]);
             }
         }
     }
     /*
      * if mode is set to simpleXML, check whether
      * the array is associative or indexed
      */
     if (is_array($array) && $this->options["mode"] == "simplexml") {
         $indexed = true;
         foreach ($array as $key => $val) {
             if (!is_int($key)) {
                 $indexed = false;
                 break;
             }
         }
         if ($indexed) {
             $string = "";
             foreach ($array as $key => $val) {
                 $string .= $this->_serializeValue($val, $tagName, $attributes);
                 $string .= $this->options["linebreak"];
                 //	do indentation
                 if ($this->options["indent"] !== null && $this->_tagDepth > 0) {
                     $string .= str_repeat($this->options["indent"], $this->_tagDepth);
                 }
             }
             return rtrim($string);
         }
     }
     if ($this->options["scalarAsAttributes"] === true) {
         foreach ($array as $key => $value) {
             if (is_scalar($value) && XML_Util::isValidName($key) === true) {
                 unset($array[$key]);
                 $attributes[$this->options["prependAttributes"] . $key] = $value;
             }
         }
     }
     // check for empty array => create empty tag
     if (empty($array)) {
         $tag = array("qname" => $tagName, "content" => $_content, "attributes" => $attributes);
     } else {
         $this->_tagDepth++;
         $tmp = $this->options["linebreak"];
         foreach ($array as $key => $value) {
             //	do indentation
             if ($this->options["indent"] !== null && $this->_tagDepth > 0) {
                 $tmp .= str_repeat($this->options["indent"], $this->_tagDepth);
             }
             //	copy key
             $origKey = $key;
             //	key cannot be used as tagname => use default tag
             $valid = XML_Util::isValidName($key);
             if (PEAR::isError($valid)) {
                 $key = $this->options["defaultTagName"];
             }
             $atts = array();
             if ($this->options["typeHints"] === true) {
                 $atts[$this->options["typeAttribute"]] = gettype($value);
                 if ($key !== $origKey) {
                     $atts[$this->options["keyAttribute"]] = (string) $origKey;
                 }
             }
             $tmp .= $this->_createXMLTag(array("qname" => $key, "attributes" => $atts, "content" => $value));
             $tmp .= $this->options["linebreak"];
         }
         $this->_tagDepth--;
         if ($this->options["indent"] !== null && $this->_tagDepth > 0) {
             $tmp .= str_repeat($this->options["indent"], $this->_tagDepth);
         }
         if (trim($tmp) === '') {
             $tmp = null;
         }
         $tag = array("qname" => $tagName, "content" => $tmp, "attributes" => $attributes);
     }
     if ($this->options["typeHints"] === true) {
         if (!isset($tag["attributes"][$this->options["typeAttribute"]])) {
             $tag["attributes"][$this->options["typeAttribute"]] = "array";
         }
     }
     $string = $this->_createXMLTag($tag, false);
     return $string;
 }