示例#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 XML_Util::getDoctypeDeclaration()
  * @uses XML_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 = XML_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 = XML_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;
 }
示例#2
0
 /**
  * serialize data
  *
  * @access   public
  * @param    mixed    $data data to serialize
  * @return   boolean  true on success, pear error on failure
  */
 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;
     $this->_serializedData = '';
     // serialize an array
     if (is_array($data)) {
         if (isset($this->options['rootName'])) {
             $tagName = $this->options['rootName'];
         } else {
             $tagName = 'array';
         }
         $this->_serializedData .= $this->_serializeArray($data, $tagName, $this->options['rootAttributes']);
     }
     // add doctype declaration
     if ($this->options['addDoctype'] === true) {
         $this->_serializedData = XML_Util::getDoctypeDeclaration($tagName, $this->options['doctype']) . $this->options['linebreak'] . $this->_serializedData;
     }
     //  build xml declaration
     if ($this->options['addDecl']) {
         $atts = array();
         if (isset($this->options['encoding'])) {
             $encoding = $this->options['encoding'];
         } else {
             $encoding = null;
         }
         $this->_serializedData = XML_Util::getXMLDeclaration('1.0', $encoding) . $this->options['linebreak'] . $this->_serializedData;
     }
     if ($optionsBak !== null) {
         $this->options = $optionsBak;
     }
     return true;
 }
 /**
  * serialize data
  *
  * @access   public
  * @param    mixed    $data data to serialize
  * @return   boolean  true on success, pear error on failure
  */
 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;
     }
     // maintain BC
     if (isset($this->options["tagName"])) {
         $this->options["rootName"] = $this->options["tagName"];
     }
     //  start depth is zero
     $this->_tagDepth = 0;
     $this->_serializedData = "";
     // serialize an array
     if (is_array($data)) {
         if (isset($this->options["rootName"])) {
             $tagName = $this->options["rootName"];
         } else {
             $tagName = "array";
         }
         $this->_serializedData .= $this->_serializeArray($data, $tagName, $this->options["rootAttributes"]);
     } elseif (is_object($data)) {
         if (isset($this->options["rootName"])) {
             $tagName = $this->options["rootName"];
         } else {
             $tagName = get_class($data);
         }
         $this->_serializedData .= $this->_serializeObject($data, $tagName, $this->options["rootAttributes"]);
     }
     // add doctype declaration
     if ($this->options["addDoctype"] === true) {
         $this->_serializedData = XML_Util::getDoctypeDeclaration($tagName, $this->options["doctype"]) . $this->options["linebreak"] . $this->_serializedData;
     }
     //  build xml declaration
     if ($this->options["addDecl"]) {
         $atts = array();
         if (isset($this->options["encoding"])) {
             $encoding = $this->options["encoding"];
         } else {
             $encoding = null;
         }
         $this->_serializedData = XML_Util::getXMLDeclaration("1.0", $encoding) . $this->options["linebreak"] . $this->_serializedData;
     }
     if ($optionsBak !== null) {
         $this->options = $optionsBak;
     }
     return true;
 }