Esempio n. 1
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     PEAR_PackageFile_Generator_v2_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) && PEAR_PackageFile_Generator_v2_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 = PEAR_PackageFile_Generator_v2_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;
 }