cvs: pear /XML_Serializer Serializer.php Unserializer.php

From: Date: Tue, 23 Sep 2003 16:05:37 +0000
Subject: cvs: pear /XML_Serializer Serializer.php Unserializer.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-13022@lists.php.net to get a copy of this message
schst Tue Sep 23 12:05:37 2003 EDT Modified files: /pear/XML_Serializer Serializer.php Unserializer.php Log: * removed bug when using mode => simplexml and indexed arrays contained only scalar values * removed bug with missing _originalKey attribute when serializing objects * added check for set{$propName} method to use methods to set properties in Unserializer instead of direct access Index: pear/XML_Serializer/Serializer.php diff -u pear/XML_Serializer/Serializer.php:1.7 pear/XML_Serializer/Serializer.php:1.8 --- pear/XML_Serializer/Serializer.php:1.7 Tue Sep 23 04:52:07 2003 +++ pear/XML_Serializer/Serializer.php Tue Sep 23 12:05:37 2003 @@ -16,7 +16,7 @@ // | Authors: Stephan Schmidt <schst@php-tools.net> | // +----------------------------------------------------------------------+ // -// $Id: Serializer.php,v 1.7 2003/09/23 08:52:07 schst Exp $ +// $Id: Serializer.php,v 1.8 2003/09/23 16:05:37 schst Exp $ /** * uses PEAR error management @@ -268,6 +268,34 @@ } /** + * serialize any value + * + * This method checks for the type of the value and calls the appropriate method + * + * @access private + * @param mixed $value + * @param string $tagName + * @param array $attributes + * @return string + */ + function _serializeValue($value, $tagName = null, $attributes = array()) + { + if (is_array($value)) { + $xml = $this->_serializeArray($value, $tagName, $attributes); + } elseif (is_object($value)) { + $xml = $this->_serializeObject($value, $tagName); + } else { + $tag = array( + "qname" => $tagName, + "attributes" => $attributes, + "content" => $value + ); + $xml = $this->_createXMLTag($tag); + } + return $xml; + } + + /** * serialize an array * * @access private @@ -279,7 +307,7 @@ */ function _serializeArray(&$array, $tagName = null, $attributes = array()) { - if ($this->options["mode"] == "simplexml") { + if (is_array($array) && $this->options["mode"] == "simplexml") { $indexed = true; foreach ($array as $key => $val) { if (!is_int($key)) { @@ -291,7 +319,7 @@ if ($indexed) { $string = ""; foreach ($array as $key => $val) { - $string .= $this->_serializeArray( $val, $tagName); + $string .= $this->_serializeValue( $val, $tagName, $attributes); $string .= $this->options["linebreak"]; // do indentation @@ -334,6 +362,7 @@ if ($key !== $origKey) { $atts[$this->options["keyAttribute"]] = (string)$origKey; } + } $tmp .= $this->_createXMLTag(array( @@ -376,7 +405,7 @@ * @param object $object object to serialize * @return string $string serialized data */ - function _serializeObject( &$object, $tagName = null ) + function _serializeObject(&$object, $tagName = null, $attributes = array()) { // check for magic function if (method_exists($object, "__sleep")) { @@ -389,7 +418,6 @@ $tagName = get_class($object); } - $attributes = array(); // typehints activated? if ($this->options["typeHints"] === true) { $attributes[$this->options["typeAttribute"]] = "object"; Index: pear/XML_Serializer/Unserializer.php diff -u pear/XML_Serializer/Unserializer.php:1.7 pear/XML_Serializer/Unserializer.php:1.8 --- pear/XML_Serializer/Unserializer.php:1.7 Tue Sep 23 04:52:07 2003 +++ pear/XML_Serializer/Unserializer.php Tue Sep 23 12:05:37 2003 @@ -16,7 +16,7 @@ // | Authors: Stephan Schmidt <schst@php-tools.net> | // +----------------------------------------------------------------------+ // -// $Id: Unserializer.php,v 1.7 2003/09/23 08:52:07 schst Exp $ +// $Id: Unserializer.php,v 1.8 2003/09/23 16:05:37 schst Exp $ /** * uses PEAR error managemt @@ -321,10 +321,6 @@ $this->_depth++; $this->_dataStack[$this->_depth] = null; - if (is_array($this->options["tagMap"]) && isset($this->options["tagMap"][$element])) { - $element = $this->options["tagMap"][$element]; - } - $val = array( "name" => $element, "value" => null, @@ -351,8 +347,8 @@ $val["name"] = $attribs[$this->options["keyAttribute"]]; } - if (isset($attribs[$this->options["typeAttribute"]])) { - $val["class"] = $attribs[$this->options["typeAttribute"]]; + if (isset($attribs[$this->options["classAttribute"]])) { + $val["class"] = $attribs[$this->options["classAttribute"]]; } array_push($this->_valStack, $val); @@ -370,7 +366,6 @@ { $value = array_pop($this->_valStack); $data = trim($this->_dataStack[$this->_depth]); - // adjust type of the value switch(strtolower($value["type"])) { @@ -379,11 +374,16 @@ */ case "object": $classname = $value["class"]; + if (is_array($this->options["tagMap"]) && isset($this->options["tagMap"][$classname])) { + $classname = $this->options["tagMap"][$classname]; + } + + // instantiate the class if (class_exists($classname)) { - $value["value"] = new $classname; + $value["value"] = &new $classname; } else { - $value["value"] = new stdClass; + $value["value"] = &new stdClass; } if ($data !== '') { @@ -392,9 +392,14 @@ // set properties foreach($value["children"] as $prop => $propVal) { - $value["value"]->$prop = $propVal; + // check whether there is a special method to set this property + $setMethod = "set".$prop; + if (method_exists($value["value"], $setMethod)) { + call_user_func(array(&$value["value"], $setMethod), $propVal); + } else { + $value["value"]->$prop = &$propVal; + } } - // check for magic function if (method_exists($value["value"], "__wakeup")) { $value["value"]->__wakeup(); @@ -437,8 +442,8 @@ } $parent = array_pop($this->_valStack); if ($parent === null) { - $this->_unserializedData = $value["value"]; - $this->_root = $value["name"]; + $this->_unserializedData = &$value["value"]; + $this->_root = &$value["name"]; return true; } else { // parent has to be an array @@ -462,7 +467,7 @@ } array_push($parent["children"][$value["name"]], $value["value"]); } else { - $parent["children"][$value["name"]] = $value["value"]; + $parent["children"][$value["name"]] = &$value["value"]; array_push($parent["childrenKeys"], $value["name"]); } } else {

« previous php.pear.cvs (#13022) next »