cvs: pear /XML_Serializer Serializer.php /XML_Serializer/examples serializeWithComment.php

From: Date: Mon, 11 Apr 2005 18:58:57 +0000
Subject: cvs: pear /XML_Serializer Serializer.php /XML_Serializer/examples serializeWithComment.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-31106@lists.php.net to get a copy of this message
schst Mon Apr 11 14:58:57 2005 EDT Added files: /pear/XML_Serializer/examples serializeWithComment.php Modified files: /pear/XML_Serializer Serializer.php Log: added new option XML_SERIALIZER_OPTION_COMMENT_KEY (implements request #4115) http://cvs.php.net/diff.php/pear/XML_Serializer/Serializer.php?r1=1.37&r2=1.38&ty=u Index: pear/XML_Serializer/Serializer.php diff -u pear/XML_Serializer/Serializer.php:1.37 pear/XML_Serializer/Serializer.php:1.38 --- pear/XML_Serializer/Serializer.php:1.37 Sat Mar 26 03:04:04 2005 +++ pear/XML_Serializer/Serializer.php Mon Apr 11 14:58:56 2005 @@ -19,7 +19,7 @@ * @author Stephan Schmidt <schst@php.net> * @copyright 1997-2005 The PHP Group * @license http://www.php.net/license/3_0.txt PHP License 3.0 - * @version CVS: $Id: Serializer.php,v 1.37 2005/03/26 08:04:04 schst Exp $ + * @version CVS: $Id: Serializer.php,v 1.38 2005/04/11 18:58:56 schst Exp $ * @link http://pear.php.net/package/XML_Serializer * @see XML_Unserializer */ @@ -206,6 +206,15 @@ define('XML_SERIALIZER_OPTION_CONTENT_KEY', 'contentName'); /** + * option: this value will be used in a comment, instead of creating a new tag + * + * Possible values: + * - string + * - null (default) + */ +define('XML_SERIALIZER_OPTION_COMMENT_KEY', 'commentName'); + +/** * option: tag names that will be changed * * Possible values: @@ -393,6 +402,7 @@ XML_SERIALIZER_OPTION_ROOT_ATTRIBS, XML_SERIALIZER_OPTION_ATTRIBUTES_KEY, XML_SERIALIZER_OPTION_CONTENT_KEY, + XML_SERIALIZER_OPTION_COMMENT_KEY, XML_SERIALIZER_OPTION_TAGMAP, XML_SERIALIZER_OPTION_ENCODE_FUNC, XML_SERIALIZER_OPTION_NAMESPACE, @@ -428,6 +438,7 @@ XML_SERIALIZER_OPTION_ROOT_ATTRIBS => array(), // attributes of the root tag XML_SERIALIZER_OPTION_ATTRIBUTES_KEY => null, // all values in this key will be treated as attributes XML_SERIALIZER_OPTION_CONTENT_KEY => null, // this value will be used directly as content, instead of creating a new tag, may only be used in conjuction with attributesArray + XML_SERIALIZER_OPTION_COMMENT_KEY => null, // this value will be used directly as comment, instead of creating a new tag, may only be used in conjuction with attributesArray XML_SERIALIZER_OPTION_TAGMAP => array(), // tag names that will be changed XML_SERIALIZER_OPTION_ENCODE_FUNC => null, // function that will be applied before serializing XML_SERIALIZER_OPTION_NAMESPACE => null, // namespace to use @@ -659,6 +670,15 @@ 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 @@ -706,7 +726,7 @@ $string .= $this->_serializeValue( $val, $tagName, $attributes); $string .= $this->options[XML_SERIALIZER_OPTION_LINEBREAKS]; - // do indentation + // do indentation if ($this->options[XML_SERIALIZER_OPTION_INDENT]!==null && $this->_tagDepth>0) { $string .= str_repeat($this->options[XML_SERIALIZER_OPTION_INDENT], $this->_tagDepth); } @@ -733,12 +753,11 @@ 'content' => $_content, 'attributes' => $attributes ); - } else { $this->_tagDepth++; $tmp = $this->options[XML_SERIALIZER_OPTION_LINEBREAKS]; foreach ($array as $key => $value) { - // do indentation + // do indentation if ($this->options[XML_SERIALIZER_OPTION_INDENT]!==null && $this->_tagDepth>0) { $tmp .= str_repeat($this->options[XML_SERIALIZER_OPTION_INDENT], $this->_tagDepth); } @@ -768,7 +787,7 @@ } } - + $tmp .= $this->_createXMLTag(array( 'qname' => $key, 'attributes' => $atts, @@ -798,7 +817,15 @@ } } - $string = $this->_createXMLTag($tag, false); + $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; } @@ -850,6 +877,7 @@ */ function _createXMLTag( $tag, $firstCall = true ) { + // build fully qualified tag name if ($this->options[XML_SERIALIZER_OPTION_NAMESPACE] !== null) { if (is_array($this->options[XML_SERIALIZER_OPTION_NAMESPACE])) { $tag['qname'] = $this->options[XML_SERIALIZER_OPTION_NAMESPACE][0] . ':' . $tag['qname']; @@ -858,6 +886,7 @@ } } + // attribute indentation if ($this->options[XML_SERIALIZER_OPTION_INDENT_ATTRIBUTES] !== false) { $multiline = true; $indent = str_repeat($this->options[XML_SERIALIZER_OPTION_INDENT], $this->_tagDepth); @@ -873,6 +902,7 @@ $indent = false; } + // replace XML entities (only needed, if this is not a nested call) if ($firstCall === true) { $replaceEntities = $this->options[XML_SERIALIZER_OPTION_ENTITIES]; } else { http://cvs.php.net/co.php/pear/XML_Serializer/examples/serializeWithComment.php?r=1.1&p=1 Index: pear/XML_Serializer/examples/serializeWithComment.php +++ pear/XML_Serializer/examples/serializeWithComment.php <?PHP /** * This is just a basic example that shows * how objects can be serialized so they can * be fully restored later. * * @author Stephan Schmidt <schst@php.net> */ error_reporting(E_ALL); require_once 'XML/Serializer.php'; $options = array( XML_SERIALIZER_OPTION_INDENT => ' ', XML_SERIALIZER_OPTION_LINEBREAKS => "\n", XML_SERIALIZER_OPTION_COMMENT_KEY => 'comment' ); $foo = new stdClass; $foo->comment = 'This is a comment'; $foo->value = 'My value'; $foo->bar = new stdClass(); $foo->bar->value = 'Another value'; $foo->bar->comment = 'Another comment'; $foo->tomato = new stdClass(); $foo->tomato->comment = 'And a last comment'; $serializer = &new XML_Serializer($options); $result = $serializer->serialize($foo); if ($result === true) { $xml = $serializer->getSerializedData(); } echo '<pre>'; print_r(htmlspecialchars($xml)); echo '</pre>'; ?>

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