Esempio n. 1
0
 /**
  * serialize data
  *
  * @param mixed $data    data to serialize
  * @param array $options options array
  *
  * @return boolean true on success, pear error on failure
  * @access public
  * @uses Util::getDoctypeDeclaration()
  * @uses Util::getXMLDeclaration()
  * @internal uses error suppression "@settype()"
  */
 function serialize($data, $options = null)
 {
     // if options have been specified, use them instead
     // of the previously defined ones
     if (is_array($options)) {
         $optionsBak = $this->options;
         if (isset($options['overrideOptions']) && $options['overrideOptions'] == true) {
             $this->options = array_merge($this->_defaultOptions, $options);
         } else {
             $this->options = array_merge($this->options, $options);
         }
     } else {
         $optionsBak = null;
     }
     //  start depth is zero
     $this->_tagDepth = 0;
     $rootAttributes = $this->options[XML_SERIALIZER_OPTION_ROOT_ATTRIBS];
     if (isset($this->options[XML_SERIALIZER_OPTION_NAMESPACE]) && is_array($this->options[XML_SERIALIZER_OPTION_NAMESPACE])) {
         $rootAttributes['xmlns:' . $this->options[XML_SERIALIZER_OPTION_NAMESPACE][0]] = $this->options[XML_SERIALIZER_OPTION_NAMESPACE][1];
     }
     $this->_serializedData = '';
     // serialize an array
     if (is_array($data)) {
         if (isset($this->options[XML_SERIALIZER_OPTION_ROOT_NAME])) {
             $tagName = $this->options[XML_SERIALIZER_OPTION_ROOT_NAME];
         } else {
             $tagName = 'array';
         }
         $this->_serializedData .= $this->_serializeArray($data, $tagName, $rootAttributes);
     } elseif (is_object($data)) {
         // serialize an object
         if (isset($this->options[XML_SERIALIZER_OPTION_ROOT_NAME])) {
             $tagName = $this->options[XML_SERIALIZER_OPTION_ROOT_NAME];
         } else {
             $tagName = get_class($data);
         }
         $this->_serializedData .= $this->_serializeObject($data, $tagName, $rootAttributes);
     } else {
         $tag = array();
         if (isset($this->options[XML_SERIALIZER_OPTION_ROOT_NAME])) {
             $tag['qname'] = $this->options[XML_SERIALIZER_OPTION_ROOT_NAME];
         } else {
             $tag['qname'] = gettype($data);
         }
         $tagName = $tag['qname'];
         if ($this->options[XML_SERIALIZER_OPTION_TYPEHINTS] === true) {
             $rootAttributes[$this->options[XML_SERIALIZER_OPTION_ATTRIBUTE_TYPE]] = gettype($data);
         }
         if (!is_bool($data)) {
             $tag['content'] = $data;
         } elseif ($data === false) {
             if ($this->options[XML_SERIALIZER_OPTION_FALSE_AS_STRING] === true) {
                 $tag['content'] = '0';
             } else {
                 $tag['content'] = '';
             }
         } else {
             $tag['content'] = $data;
         }
         @settype($data, 'string');
         $tag['attributes'] = $rootAttributes;
         $this->_serializedData = $this->_createXMLTag($tag);
     }
     // add doctype declaration
     if ($this->options[XML_SERIALIZER_OPTION_DOCTYPE_ENABLED] === true) {
         $this->_serializedData = Util::getDoctypeDeclaration($tagName, $this->options[XML_SERIALIZER_OPTION_DOCTYPE]) . $this->options[XML_SERIALIZER_OPTION_LINEBREAKS] . $this->_serializedData;
     }
     //  build xml declaration
     if ($this->options[XML_SERIALIZER_OPTION_XML_DECL_ENABLED]) {
         $atts = array();
         $this->_serializedData = Util::getXMLDeclaration('1.0', $this->options[XML_SERIALIZER_OPTION_XML_ENCODING]) . $this->options[XML_SERIALIZER_OPTION_LINEBREAKS] . $this->_serializedData;
     }
     if ($this->options[XML_SERIALIZER_OPTION_RETURN_RESULT] === true) {
         $result = $this->_serializedData;
     } else {
         $result = true;
     }
     if ($optionsBak !== null) {
         $this->options = $optionsBak;
     }
     return $result;
 }