package.xml0000644000175000017500000006552011461374326013776 0ustar clockwerxclockwerx XML_Serializer pear.php.net Swiss-army knife for reading and writing XML files. Creates XML files from data structures and vice versa. XML_Serializer serializes complex data structures like arrays or object as XML documents. This class helps you generating any XML document you require without the need for DOM. Furthermore this package can be used as a replacement to serialize() and unserialize() as it comes with a matching XML_Unserializer that is able to create PHP data structures (like arrays and objects) from XML documents, if type hints are available. If you use the XML_Unserializer on standard XML files, it will try to guess how it has to be unserialized. In most cases it does exactly what you expect it to do. Try reading a RSS file with XML_Unserializer and you have the whole RSS file in a structured array or even a collection of objects, similar to XML_RSS. Since version 0.8.0 the package is able to treat XML documents similar to the simplexml extension of PHP 5. Stephan Schmidt schst schst@php-tools.net no Chuck Burgess ashnazg ashnazg@php.net yes 2010-10-26 0.20.2 0.20.0 beta beta BSD License Bug #17994 Installed into wrong path 4.2.0 1.4.0b1 XML_Util pear.php.net 1.1.1 XML_Parser pear.php.net 1.2.6 PEAR pear.php.net xml 0.2 0.2 beta beta 2003-08-03 BSD License inital release 0.4 0.4 beta beta 2003-08-12 BSD License added Unserializer removed bug with object typehint removed bug with unnameditem w/o _originalKey attribute uses XML_Util to validate tagname serializer does not break on resources anymore added support for __sleep() and __wakeup() 0.5 0.5 beta beta 2003-08-14 PHP License Unserializer is now able to unserialize XML files w/o type hints by automatically transforming multiple occurences of the same tag to an indexed array This is now similar to ext/simpleXML in PHP5 that means it can be used to parse any XML file, try it with your favorite RSS feed 0.6 0.6 beta beta 2003-08-21 PHP License Unserializer is able to return the name of the root tag, added 'complexType' option to XML_Unserializer, added possibility to create nested objects from any XML file, tagname is used as classname if no other class is specified, added 'overrideOptions' option that can be used to restore the default options for a serialization, added 'keyAttribute' option to XML_Unserializer, added Todo list 0.8 0.8 beta beta 2003-09-23 PHP License XML_Serializer: * added option: scalarAsAttribute to create tags with attributes from an array or object, * added option: prependAttributes to enable a prepend string for attributes (ensure they are different from type hints), * added option: attributeIndent to allow multiline tags, if a tag has several attributes; this makes it easier to read (requested by Yavor Shahpasov), * now requires XML_Util 0.4.1 or newer, * added option: mode => simplexml, allows you to serialize indexed arrays like simpleXML of PHP5 does: tags use name of the parent structure and are in the same depth (this is still in alpha state, not thoroughly tested) XML_Unserializer: * added option: parseAttributes which allows you to parse tags with attributes, * added option: attributesArray to specify the name of the array in which attributes should be stored, * added option: prependAttributes to specify a prepend string for the generated array keys, * added option: contentName to specify the key for the content of a tag that contains also attributes * added option: tagMap, to change the names of the tags while parsing Both: * removed bug that occured when instantiating w/o options (reported by Alan Knowles), * added options: keyAttribute, typeAttribute and classAtribute to allow user to specify attribute names for typehints, * added resetOptions(), * added setOption(), * added option: overrideOptions that allows you to reset to defaults for one call of serialize or unserialize, * added some very small docs to defaultOptions arrays 0.8.1 0.8.1 beta beta 2003-09-23 PHP License Bugfix release XML_Serializer: * removed bug when using mode => simplexml and indexed arrays contained only scalar values, * removed bug with missing _originalKey attribute when serializing objects XML_Unserializer: * added check for set{$propName} method to use methods to set properties in Unserializer instead of direct access 0.9 0.9 beta beta 2003-10-13 PHP License XML_Serializer: + added support for doctype declaration (requested by Lapo 'Raist' Luchini), * changed tagName option to rootName option (tagName is still supported but deprecated), + added rootAttributes option (thanks to Lapo 'Raist' Luchini), + added rootName option for serializing objects XML_Unserializer: * removed bug with unserializing aggregated objects 0.9.1 0.9.1 beta beta 2003-10-26 PHP License fixed bug #105 0.9.2 0.9.2 beta beta 2004-04-23 PHP License fixed invalid multiple call to XML_Parser constructor 0.10.0 0.10.0 beta beta 2004-05-24 PHP License - fixed bug #1478 (Array to string conversion) - fixed bug #170 (RDF examples produce incorrect code) - added new options to XML_Serializer: attributesArray and contentName to match XML_Unserializer 0.10.1 0.10.1 beta beta 2004-07-12 PHP License fixed bug #1846 (unserialization bug with empty arrays) 0.11.0 0.11.0 beta beta 2004-08-24 PHP License Serializer: - Request #2019 (use classname as tagname in indexed arrays) - replaced double quotes with single quotes - some CS fixes Unserializer: - added new option 'forceEnum' - replaced double quotes with single quotes 0.11.1 0.11.1 beta beta 2004-08-25 PHP License Serializer: - fixed bug with default line endings 0.12.0 0.12.0 beta beta 2004-11-03 PHP License Serializer: - added new option tagMap (requested by Toby) Unserializer: - added encoding option (thanks to mc[at]glouglou[dot]net) 0.13.0 0.13.0 beta beta 2004-11-06 PHP License Serializer: - fixed bug #2688: in simplexml mode $array needs to be non-empty (thanks to demian[at]phpkitchen[dot]com and derek[at]hostbaby[dot]com) Unserializer: - implemented Request #2650: Multiple key attributes 0.13.1 0.13.1 beta beta 2004-11-26 PHP License Serializer: - fixed error handling (suppress error handling, when XML_Util returns an error) (patch by Lukas Smith) - replaced some left-over tabs with spaces 0.14.0 0.14.0 beta beta 2004-12-22 PHP License Serializer: - new option 'encodeFunction' to apply a PHP function to character data and attributes before serializing the data Unserializer: - Does no longer extend XML_Parser (allows to change encoding at runtime), this could break some scripts - new option 'targetEncoding' to specify the target encoding for character data - new option 'decodeFunction' to apply a PHP function on character data and attributes while unserializing the document - some tiny whitespace fixes Global: - new requires XML_Parser 1.2.2 and XML_Util 1.1.0 0.14.1 0.14.1 beta beta 2004-12-27 PHP License Bugfix release: - XML_Unserializer now again extends PEAR to use PEAR error management 0.15.0 0.15.0 beta beta 2005-02-12 PHP License XML_Serializer: - added option to return the result directly from serialize() XML_Unserializer: - added option to return the result directly from unserialize(), - added defaultClass option - added tagAsClass option (fixes Bug #3303) - fixed bug (tag name was not used as classname when tag was empty) 0.16.0 0.16.0 beta beta 2005-06-05 PHP License XML_Serializer: - introduced constants for all options (this helps avoiding typos in the option names) - deprecated option 'tagName' is no longer supported, use XML_SERIALIZER_OPTION_ROOT_NAME (or rootName) instead - implement Request #3762: added new ignoreNull option to ignore properties that are set to null when serializing objects or arrays - fixed bug with encoding function - use new header comment blocks XML_Unserializer: - fix bug #4075 (allow tagMap option to influence any kind of value) 0.17.0 0.17.0 beta beta 2005-09-24 PHP License XML_Serializer: - fixed bug #4575: use the return values of __sleep() when serializing objects - allow XML_SERIALIZER_OPTION_DEFAULT_TAG to be set as array to set different default tags based on the surrounding tag - allow XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES to be set as array to selectively use scalar values as attributes - allow scalar values as first argument to serialize() - implemented request #3180: Add option to create cdata sections XML_Unserializer: - added constants for all options - fixed bug #5187: decode function should decode cdata instead of the element name - implemented request #3700: Treatment of whitespace can be influenced via an option 0.18.0 0.18.0 beta beta 2005-10-02 PHP License XML_Serializer: - Allow the use of '#default' whenever one wants to specify an option for any other key - fixed bug #4575 for real: XML_Serializer now expects __sleep() to return an array of property names XML_Unserializer: - Added new IGNORE_KEYS option to specify a list of tags which will not be unserialized using their names as keys - Added new GUESS_TYPE option to automatically convert the data to the best matching type - Allow COMPLEXTYPE to be an array to be able to unserialize to arrays or objects - Allow the use of '#default' whenever one wants to specify an option for any other tag 0.19.0a1 0.19.0a1 alpha alpha 2008-08-24 BSD License - switch to package.xml v2 - PEAR CS cleanup - Req #13856: License Change from PHP to BSD [ashnazg] - Bug #8048: Entities are not replaced in tags with attributes [schst] - Doc #12725: tuto link no more available 0.19.0 0.19.0 beta beta 2008-09-15 BSD License - switch to package.xml v2 - PEAR CS cleanup - Req #13856: License Change from PHP to BSD [ashnazg] - Bug #8048: Entities are not replaced in tags with attributes [schst] - Doc #12725: tuto link no more available 0.19.1 0.19.1 beta beta 2008-10-21 BSD License - Bug #14653: testNumberedObjects testcase fails [ashnazg] - Doc #8650 : Missing Information in the Manual [ashnazg] - Doc #13896: Bad info in the RSS feed tutorial [ashnazg] 0.19.2 0.19.2 beta beta 2009-01-24 BSD License - Bug #15602: attributes don't get escaped sometimes [lapo] 0.20.0 0.20.0 beta beta 2010-09-04 BSD License - Request #13564: bool(false) is converted to empty string 0.20.1 0.20.0 beta beta 2010-10-25 BSD License Automatically built QA release Bug #17848 XML_Serializer uses deprecated return new as reference - zigo 0.20.2 0.20.0 beta beta 2010-10-26 BSD License Bug #17994 Installed into wrong path XML_Serializer-0.20.2/doc/todo.txt0000600000175000017500000000106011461374326017457 0ustar clockwerxclockwerxTODO: ----- - introduce constants for options to avoid typos TODO XML_Serializer: -------------------- - find a way to detect references in the structure and add id/idref attributes - serialize some other structures, especially XPath return values - custom serialization of objects TODO XML_Unserializer: ---------------------- - support for xml:space attribute - set types for certain tags (like in patConfiguration) - namespace support - custom unserialization for objects (static unserialize() or fromXML() methods) - unserialize references using id/idrefXML_Serializer-0.20.2/examples/example.xml0000600000175000017500000000012211461374326021175 0ustar clockwerxclockwerx Test test XML_Serializer-0.20.2/examples/serializeAndEncode.php0000600000175000017500000000265211461374326023273 0ustar clockwerxclockwerx */ error_reporting(E_ALL); require_once 'XML/Serializer.php'; require_once 'XML/Unserializer.php'; $options = array( XML_SERIALIZER_OPTION_INDENT => ' ', XML_SERIALIZER_OPTION_LINEBREAKS => "\n", XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES => true, XML_SERIALIZER_OPTION_ENCODE_FUNC => 'strtoupper' ); $foo = new stdClass(); $foo->bar = new stdClass(); $foo->bar->test = 'This is a test.'; $foo->bar->value = 'This is a value.'; $serializer = &new XML_Serializer($options); $result = $serializer->serialize($foo); if ($result === true) { $xml = $serializer->getSerializedData(); } echo "
";
    print_r( htmlspecialchars($xml) );
    echo    "
"; $unserializer = &new XML_Unserializer(); $unserializer->setOption('parseAttributes', true); $unserializer->setOption('decodeFunction', 'strtolower'); $result = $unserializer->unserialize($xml); echo '
';
    print_r($unserializer->getUnserializedData());
    echo '
'; ?>XML_Serializer-0.20.2/examples/serializeAndReturn.php0000600000175000017500000000114111461374326023345 0ustar clockwerxclockwerx */ error_reporting(E_ALL); require_once 'XML/Serializer.php'; $options = array( XML_SERIALIZER_OPTION_INDENT => ' ', XML_SERIALIZER_OPTION_RETURN_RESULT => true ); $serializer = &new XML_Serializer($options); $foo = PEAR::raiseError('Just a test', 1234); $result = $serializer->serialize($foo); echo '
';
echo htmlspecialchars($result);
echo '
'; ?>XML_Serializer-0.20.2/examples/serializeCData.php0000600000175000017500000000150611461374326022424 0ustar clockwerxclockwerx */ error_reporting(E_ALL); require_once 'XML/Serializer.php'; $serializer = new XML_Serializer(); $serializer->setOption(XML_SERIALIZER_OPTION_INDENT, ' '); $serializer->setOption(XML_SERIALIZER_OPTION_DEFAULT_TAG, 'item'); $serializer->setOption(XML_SERIALIZER_OPTION_CDATA_SECTIONS, true); $data = array( 'foo' => 'This is some text...', 'bar' => '& even more text...', 'test' => array('Foo', 'Foo & bar') ); $result = $serializer->serialize($data); if( $result === true ) { $xml = $serializer->getSerializedData(); echo '
';
    echo htmlspecialchars($xml);
    echo '
'; } else { $result->getMessage(); exit(); } ?>XML_Serializer-0.20.2/examples/serializeIndexedArray.php0000600000175000017500000000476411461374326024040 0ustar clockwerxclockwerx simplexml * * It can be used to serialize an indexed array * like ext/simplexml does, by using the name * of the parent tag, while omitting this tag. * * @author Stephan Schmidt */ error_reporting(E_ALL); require_once 'XML/Serializer.php'; $options = array( XML_SERIALIZER_OPTION_INDENT => ' ', XML_SERIALIZER_OPTION_LINEBREAKS => "\n", XML_SERIALIZER_OPTION_ROOT_NAME => 'rdf:RDF', XML_SERIALIZER_OPTION_ROOT_ATTRIBS => array('version' => '0.91'), XML_SERIALIZER_OPTION_MODE => XML_SERIALIZER_MODE_SIMPLEXML ); $serializer = new XML_Serializer($options); $rdf = array( 'channel' => array( 'title' => 'Example RDF channel', 'link' => 'http://www.php-tools.de', 'image' => array( 'title' => 'Example image', 'url' => 'http://www.php-tools.de/image.gif', 'link' => 'http://www.php-tools.de' ), 'item' => array( array( 'title' => 'Example item', 'link' => 'http://example.com' ), array( 'title' => 'Another item', 'link' => 'http://example.com' ), array( 'title' => 'I think you get it...', 'link' => 'http://example.com' ) ) ) ); $result = $serializer->serialize($rdf); if ($result === true) { echo '
';
    echo htmlentities($serializer->getSerializedData());
    echo '
'; } ?>XML_Serializer-0.20.2/examples/serializeIndexedArrayWithContext.php0000600000175000017500000000201711461374326026226 0ustar clockwerxclockwerx simplexml * * It can be used to serialize an indexed array * like ext/simplexml does, by using the name * of the parent tag, while omitting this tag. * * @author Stephan Schmidt */ error_reporting(E_ALL); require_once 'XML/Serializer.php'; $options = array( XML_SERIALIZER_OPTION_INDENT => ' ', XML_SERIALIZER_OPTION_LINEBREAKS => "\n", XML_SERIALIZER_OPTION_DEFAULT_TAG => array(), XML_SERIALIZER_OPTION_DEFAULT_TAG => array('foos' => 'foo', 'bars' => 'bar') ); $serializer = new XML_Serializer($options); $data = array( 'foos' => array('one foo', 'two foos', 'three foos'), 'bars' => array('one bar', 'two bars', 'three bars'), ); $result = $serializer->serialize($data); if ($result === true) { echo '
';
    echo htmlentities($serializer->getSerializedData());
    echo '
'; } ?>XML_Serializer-0.20.2/examples/serializeNullProperties.php0000600000175000017500000000253311461374326024440 0ustar clockwerxclockwerx true * * It can be used to serialize an indexed array * like ext/simplexml does, by using the name * of the parent tag, while omitting this tag. * * @author Stephan Schmidt */ error_reporting(E_ALL); require_once 'XML/Serializer.php'; $options = array( "indent" => ' ', "linebreak" => "\n", ); $serializer = new XML_Serializer($options); $object = new stdClass(); $object->foo = 'bar'; $object->bar = null; $array = array( 'foo' => 'bar', 'bar' => null ); $result = $serializer->serialize($object); if ($result === true) { echo "
";
    echo htmlentities($serializer->getSerializedData());
    echo "
"; } $result = $serializer->serialize($array); if ($result === true) { echo "
";
    echo htmlentities($serializer->getSerializedData());
    echo "
"; } $serializer->setOption('ignoreNull', true); $result = $serializer->serialize($object); if ($result === true) { echo "
";
    echo htmlentities($serializer->getSerializedData());
    echo "
"; } $result = $serializer->serialize($array); if ($result === true) { echo "
";
    echo htmlentities($serializer->getSerializedData());
    echo "
"; } ?>XML_Serializer-0.20.2/examples/serializeObject.php0000600000175000017500000000245511461374326022662 0ustar clockwerxclockwerx */ /** * Example class that implements __sleep() * * @package XML_Serializer * @subpackage Examples */ class MyClass { var $foo = 'This is foo.'; var $bar = 'This is bar.'; function __sleep() { return array('foo'); } } error_reporting(E_ALL); /** * Load XML_Serializer */ require_once 'XML/Serializer.php'; // this is just to get a nested object $pearError = PEAR::raiseError('This is just an error object',123); $options = array( XML_SERIALIZER_OPTION_INDENT => ' ', XML_SERIALIZER_OPTION_LINEBREAKS => "\n", XML_SERIALIZER_OPTION_DEFAULT_TAG => 'unnamedItem', XML_SERIALIZER_OPTION_TYPEHINTS => true ); $foo = new stdClass(); $foo->value = "My value"; $foo->error = $pearError; $foo->xml = "cool"; $foo->obj = new MyClass(); $foo->arr = array(); $foo->zero = 0; $serializer = &new XML_Serializer($options); $result = $serializer->serialize($foo); if( $result === true ) { $xml = $serializer->getSerializedData(); } echo '
';
echo htmlspecialchars($xml);
echo '
'; ?>XML_Serializer-0.20.2/examples/serializeRDF.php0000600000175000017500000000602411461374326022063 0ustar clockwerxclockwerx simplexml * * @author Stephan Schmidt * @see serializeIndexedArray.php */ error_reporting(E_ALL); require_once 'XML/Serializer.php'; $options = array( XML_SERIALIZER_OPTION_INDENT => ' ', XML_SERIALIZER_OPTION_LINEBREAKS => "\n", XML_SERIALIZER_OPTION_TYPEHINTS => false, XML_SERIALIZER_OPTION_XML_DECL_ENABLED => true, XML_SERIALIZER_OPTION_XML_ENCODING => 'UTF-8', XML_SERIALIZER_OPTION_ROOT_NAME => 'rdf:RDF', XML_SERIALIZER_OPTION_ROOT_ATTRIBS => array('version' => '0.91'), XML_SERIALIZER_OPTION_DEFAULT_TAG => 'item', XML_SERIALIZER_OPTION_ATTRIBUTES_KEY => '_attributes' ); $serializer = new XML_Serializer($options); $rdf = array( "channel" => array( "title" => "Example RDF channel", "link" => "http://www.php-tools.de", "image" => array( "title" => "Example image", "url" => "http://www.php-tools.de/image.gif", "link" => "http://www.php-tools.de" ), "_attributes" => array( "rdf:about" => "http://example.com/foobar.html" ), array( "title" => "Example item", "link" => "http://example.com", "_attributes" => array( "rdf:about" => "http://example.com/foobar.html" ) ), array( "title" => "Another item", "link" => "http://example.com", "_attributes" => array( "rdf:about" => "http://example.com/foobar.html" ) ), array( "title" => "I think you get it...", "link" => "http://example.com", "_attributes" => array( "rdf:about" => "http://example.com/foobar.html" ) ) ) ); $result = $serializer->serialize($rdf); if ($result === true) { echo "
";
    echo htmlentities($serializer->getSerializedData());
    echo "
"; } ?>XML_Serializer-0.20.2/examples/Serializer_Bug7112.php0000600000175000017500000000132411461374326022757 0ustar clockwerxclockwerx array( 'author' => array( array('John Doe', 'attributes' => array('id' => 1)), array('Bob Jones', 'attributes' =>array('id' => 2)) ), 'title' => 'Title of the book' ) ); $s = new XML_Serializer(); $s->setOption(XML_SERIALIZER_OPTION_INDENT, ' '); $s->setOption(XML_SERIALIZER_OPTION_ATTRIBUTES_KEY, 'attributes'); $s->setOption(XML_SERIALIZER_OPTION_MODE, XML_SERIALIZER_MODE_SIMPLEXML); $s->serialize($citation); echo '
';
echo htmlentities($s->getSerializedData());
echo '
'; ?>XML_Serializer-0.20.2/examples/serializeSelectiveAttributes.php0000600000175000017500000000315111461374326025440 0ustar clockwerxclockwerx */ error_reporting(E_ALL); require_once 'XML/Serializer.php'; $data = array( array('name' => 'Superman', 'age' => 34, 'realname' => 'Clark Kent'), array('name' => 'Batman', 'age' => 32, 'realname' => 'Bruce Wayne'), 'villain' => array('name' => 'Professor Zoom', 'age' => 'unknown', 'realname' => 'Hunter Zolomon') ); $serializer = new XML_Serializer(); $serializer->setOption(XML_SERIALIZER_OPTION_INDENT, ' '); $serializer->setOption(XML_SERIALIZER_OPTION_DEFAULT_TAG, 'hero'); $serializer->serialize($data); echo '
';
echo "Default behaviour:\n";
echo htmlspecialchars($serializer->getSerializedData());
echo '
'; $serializer->setOption(XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES, true); $serializer->serialize($data); echo '
';
echo "XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES = true:\n";
echo htmlspecialchars($serializer->getSerializedData());
echo '
'; $serializer->setOption(XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES, array( 'hero' => array('name', 'age'), 'villain' => array('realname') ) ); $serializer->serialize($data); echo '
';
echo "XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES is an array:\n";
echo htmlspecialchars($serializer->getSerializedData());
echo '
'; ?>XML_Serializer-0.20.2/examples/serializeWithAttributes.php0000600000175000017500000000224711461374326024435 0ustar clockwerxclockwerx */ error_reporting(E_ALL); require_once 'XML/Serializer.php'; $options = array( XML_SERIALIZER_OPTION_INDENT => ' ', XML_SERIALIZER_OPTION_LINEBREAKS => "\n", XML_SERIALIZER_OPTION_DEFAULT_TAG => 'unnamedItem', XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES => true, ); // this is just to get a nested object $pearError = PEAR::raiseError('This is just an error object',123); $foo = new stdClass; $foo->value = 'My value'; $foo->error = $pearError; $foo->xml = 'cool'; $serializer = new XML_Serializer($options); $result = $serializer->serialize($foo); if ($result === true) { $xml = $serializer->getSerializedData(); echo '
';
    echo htmlspecialchars($xml);
    echo '
'; } else { echo '
';
    print_r($result);
    echo '
'; } ?>XML_Serializer-0.20.2/examples/serializeWithAttributes2.php0000600000175000017500000000252211461374326024513 0ustar clockwerxclockwerx */ error_reporting(E_ALL); require_once 'XML/Serializer.php'; $options = array( XML_SERIALIZER_OPTION_INDENT => ' ', XML_SERIALIZER_OPTION_LINEBREAKS => "\n", XML_SERIALIZER_OPTION_DEFAULT_TAG => 'unnamedItem', XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES => false, XML_SERIALIZER_OPTION_ATTRIBUTES_KEY => '_attributes', XML_SERIALIZER_OPTION_CONTENT_KEY => '_content' ); $data = array( 'foo' => array( '_attributes' => array( 'version' => '1.0', 'foo' => 'bar' ), '_content' => 'test & test' ), 'schst' => 'Stephan Schmidt' ); $serializer = new XML_Serializer($options); $result = $serializer->serialize($data); if ($result === true) { $xml = $serializer->getSerializedData(); echo '
';
    echo htmlspecialchars($xml);
    echo '
'; } else { echo '
';
    print_r($result);
    echo '
'; } ?>XML_Serializer-0.20.2/examples/serializeWithComment.php0000600000175000017500000000171611461374326023711 0ustar clockwerxclockwerx */ 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 '
';
echo htmlspecialchars($xml);
echo '
'; ?>XML_Serializer-0.20.2/examples/serializeWithDtd.php0000600000175000017500000000163511461374326023022 0ustar clockwerxclockwerx */ error_reporting(E_ALL); require_once 'XML/Serializer.php'; $options = array( 'indent' => ' ', 'linebreak' => "\n", 'addDecl' => true, 'addDoctype' => true, 'doctype' => array( 'uri' => 'http://pear.php.net/dtd/package-1.0', 'id' => '-//PHP//PEAR/DTD PACKAGE 0.1' ) ); $serializer = new XML_Serializer($options); $foo = PEAR::raiseError('Just a test', 1234); $result = $serializer->serialize($foo); if( $result === true ) { echo '
';
    echo htmlentities($serializer->getSerializedData());
    echo '
'; } ?>XML_Serializer-0.20.2/examples/serializeWithIndentedAttributes.php0000600000175000017500000000267511461374326026115 0ustar clockwerxclockwerx */ error_reporting(E_ALL); require_once 'XML/Serializer.php'; $options = array( "indent" => " ", "linebreak" => "\n", "typeHints" => false, "defaultTagName" => "unnamedItem", "scalarAsAttributes" => true, "indentAttributes" => "_auto" ); // this is just to get a nested object $pearError = PEAR::raiseError('This is just an error object',123); $foo = new stdClass; $foo->value = "My value"; $foo->error = $pearError; $foo->xml = "cool"; $serializer = new XML_Serializer($options); $result = $serializer->serialize($foo); if( $result === true ) { $xml = $serializer->getSerializedData(); echo "
";
        print_r( htmlspecialchars($xml) );
        echo    "
"; } else { echo "
";
        print_r($result);
        echo    "
"; } ?>XML_Serializer-0.20.2/examples/serializeWithNamespace.php0000600000175000017500000000206011461374326024174 0ustar clockwerxclockwerx */ error_reporting(E_ALL); require_once 'XML/Serializer.php'; $options = array( 'indent' => ' ', 'linebreak' => "\n", 'defaultTagName' => 'item', 'namespace' => 'foo' ); $foo = new stdClass; $foo->value = 'My value'; $foo->xml = 'cool'; $foo->obj = new stdClass; $foo->arr = array(); $foo->zero = 0; $serializer = &new XML_Serializer($options); $result = $serializer->serialize($foo); if( $result === true ) { $xml = $serializer->getSerializedData(); } echo '
';
print_r( htmlspecialchars($xml) );
echo    '
'; // also pass the URI $serializer->setOption('namespace', array('bar', 'http://pear.php.net/package/XML_Serializer')); $result = $serializer->serialize($foo); if( $result === true ) { $xml = $serializer->getSerializedData(); } echo '
';
print_r( htmlspecialchars($xml) );
echo    '
'; ?>XML_Serializer-0.20.2/examples/serializeWithTagMap.php0000600000175000017500000000531611461374326023460 0ustar clockwerxclockwerx */ error_reporting(E_ALL); require_once 'XML/Serializer.php'; $options = array( 'indent' => ' ', 'linebreak' => "\n", 'mode' => 'simplexml', 'rootName' => 'items' ); $data = array( 'item' => array( array( 'title' => 'Foobar!', 'description' => 'This is some text....', 'link' => 'http://foobar.com' ), array( 'title' => 'Foobar2!', 'description' => 'This is some text.ü ü ä ö', 'link' => 'http://foobar.com' ) ) ); $serializer = new XML_Serializer($options); $result = $serializer->serialize($data); if( $result === true ) { $xml = $serializer->getSerializedData(); echo '
';
        print_r( htmlspecialchars($xml) );
        echo    '
'; } else { echo '
';
        print_r($result);
        echo    '
'; } $newOptions = array( 'rootName' => 'body', 'replaceEntities' => XML_SERIALIZER_ENTITIES_HTML, 'tagMap' => array( 'item' => 'div', 'title' => 'h1', 'description' => 'p', 'link' => 'tt' ) ); $result = $serializer->serialize($data, $newOptions); if( $result === true ) { $xml = $serializer->getSerializedData(); echo '
';
        print_r( htmlspecialchars($xml) );
        echo    '
'; } else { echo '
';
        print_r($result);
        echo    '
'; } ?>XML_Serializer-0.20.2/examples/unserializeAnyXML.php0000600000175000017500000000506111461374326023123 0ustar clockwerxclockwerx */ error_reporting(E_ALL); // this is a simple XML document $xml = '' . ' Stephan Schmidt' . ' Martin Jansen' . ' PEAR QA Team' . ' This is handled by the default keyAttribute' . ' Another foo tag' . ''; require_once 'XML/Unserializer.php'; // complex structures are arrays, the key is the attribute 'handle' or 'name', if handle is not present $options = array( XML_UNSERIALIZER_OPTION_COMPLEXTYPE => 'array', XML_UNSERIALIZER_OPTION_ATTRIBUTE_KEY => array( 'user' => 'handle', 'group' => 'name', '#default' => 'id' ) ); // be careful to always use the ampersand in front of the new operator $unserializer = &new XML_Unserializer($options); // userialize the document $status = $unserializer->unserialize($xml, false); if (PEAR::isError($status)) { echo 'Error: ' . $status->getMessage(); } else { $data = $unserializer->getUnserializedData(); echo '
';
    print_r($data);
    echo '
'; } // unserialize it again and change the complexType option // but leave other options untouched // now complex types will be an object, and the property name will be in the // attribute 'handle' $status = $unserializer->unserialize($xml, false, array(XML_UNSERIALIZER_OPTION_COMPLEXTYPE => 'object')); if (PEAR::isError($status)) { echo 'Error: ' . $status->getMessage(); } else { $data = $unserializer->getUnserializedData(); echo '
';
    print_r($data);
    echo '
'; } // unserialize it again and change the complexType option // and reset all other options // Now, there's no key so the tags are stored in an array $status = $unserializer->unserialize($xml, false, array(XML_UNSERIALIZER_OPTION_OVERRIDE_OPTIONS => true, XML_UNSERIALIZER_OPTION_COMPLEXTYPE => 'object')); if (PEAR::isError($status)) { echo 'Error: ' . $status->getMessage(); } else { $data = $unserializer->getUnserializedData(); echo '
';
    print_r($data);
    echo '
'; } ?>XML_Serializer-0.20.2/examples/unserializeClassNames.php0000600000175000017500000000230611461374326024043 0ustar clockwerxclockwerx * @category XML * @package XML_Serializer * @subpackage Examples */ error_reporting(E_ALL); /** * uses XML_Unserializer */ require_once 'XML/Unserializer.php'; $xml = << bar EOT; class foo { } class bar { } echo '
';
//  be careful to always use the ampersand in front of the new operator 
$unserializer = &new XML_Unserializer();
$unserializer->setOption(XML_UNSERIALIZER_OPTION_COMPLEXTYPE, 'object');
$unserializer->setOption(XML_UNSERIALIZER_OPTION_ATTRIBUTES_PARSE, true);
$unserializer->setOption(XML_UNSERIALIZER_OPTION_RETURN_RESULT, true);

$data = $unserializer->unserialize($xml);    
var_dump($data);

echo "Do not use tagname as class name\n";
$unserializer->setOption(XML_UNSERIALIZER_OPTION_TAG_AS_CLASSNAME, false);
$data = $unserializer->unserialize($xml);    
var_dump( $data );

echo "Use a different default class\n";
$unserializer->setOption(XML_UNSERIALIZER_OPTION_DEFAULT_CLASS, 'foo');
$data = $unserializer->unserialize($xml);    
var_dump( $data );

echo '
'; ?>XML_Serializer-0.20.2/examples/unserializeEncoded.php0000600000175000017500000000166511461374326023362 0ustar clockwerxclockwerx */ error_reporting(E_ALL); require_once 'XML/Unserializer.php'; $xml = ' chickenwings '.utf8_encode('Hähnchenflügel').' '; // specify different source and target encodings $options = array( XML_UNSERIALIZER_OPTION_ENCODING_SOURCE => 'UTF-8', XML_UNSERIALIZER_OPTION_ENCODING_TARGET => 'ISO-8859-1' ); // be careful to always use the ampersand in front of the new operator $unserializer = &new XML_Unserializer($options); // userialize the document $status = $unserializer->unserialize($xml); if (PEAR::isError($status)) { echo 'Error: ' . $status->getMessage(); } else { $data = $unserializer->getUnserializedData(); echo '
';
    print_r($data);
    echo '
'; } ?>XML_Serializer-0.20.2/examples/unserializeEnum.php0000600000175000017500000000224311461374326022716 0ustar clockwerxclockwerx */ error_reporting(E_ALL); require_once 'XML/Unserializer.php'; $xml1 = ' schst luckec '; $xml2 = ' schst '; $options = array( XML_UNSERIALIZER_OPTION_FORCE_ENUM => array('item') ); // be careful to always use the ampersand in front of the new operator $unserializer = &new XML_Unserializer($options); // userialize the document $status = $unserializer->unserialize($xml1); if (PEAR::isError($status)) { echo 'Error: ' . $status->getMessage(); } else { $data = $unserializer->getUnserializedData(); echo '
';
    print_r($data);
    echo '
'; } // userialize the document $status = $unserializer->unserialize($xml2); if (PEAR::isError($status)) { echo 'Error: ' . $status->getMessage(); } else { $data = $unserializer->getUnserializedData(); echo '
';
    print_r($data);
    echo '
'; } ?>XML_Serializer-0.20.2/examples/unserializeObject.php0000600000175000017500000000270011461374326023216 0ustar clockwerxclockwerx */ error_reporting(E_ALL); require_once 'XML/Serializer.php'; require_once 'XML/Unserializer.php'; // this is just to get a nested object $pearError = PEAR::raiseError('This is just an error object',123); $options = array( XML_SERIALIZER_OPTION_INDENT => ' ', XML_SERIALIZER_OPTION_LINEBREAKS => "\n", XML_SERIALIZER_OPTION_DEFAULT_TAG => 'unnamedItem', XML_SERIALIZER_OPTION_TYPEHINTS => true ); $foo = new stdClass(); $foo->value = 'My value'; $foo->error = $pearError; $foo->xml = array('This is' => 'cool'); $foo->resource = fopen(__FILE__, 'r'); $serializer = new XML_Serializer($options); $result = $serializer->serialize($foo); if ($result === true) { $xml = $serializer->getSerializedData(); } echo '
';
echo htmlspecialchars($xml);
echo '
'; // be careful to always use the ampersand in front of the new operator $unserializer = &new XML_Unserializer(); $status = $unserializer->unserialize($xml); if (PEAR::isError($status)) { echo 'Error: ' . $status->getMessage(); } else { $data = $unserializer->getUnserializedData(); echo '
';
    print_r($data);
    echo '
'; } ?>XML_Serializer-0.20.2/examples/unserializeRDF.php0000600000175000017500000000412211461374326022423 0ustar clockwerxclockwerx */ error_reporting(E_ALL); require_once 'XML/Unserializer.php'; /** * class for the RDF docuemnt * * */ class rdfDocument { var $channel; var $item; function getItems($amount) { return array_splice($this->item,0,$amount); } } /** * class that is used for a channel in the RSS file * * you could implement whatever you like in this class, * properties will be set from the XML document */ class channel { function getTitle() { return $this->title; } } /** * class that is used for an item in the RSS file * * you could implement whatever you like in this class, * properties will be set from the XML document */ class item { function getTitle() { return $this->title; } } $options = array( XML_UNSERIALIZER_OPTION_COMPLEXTYPE => 'object', XML_UNSERIALIZER_OPTION_TAG_MAP => array( 'rdf:RDF' => 'rdfDocument', // this is used to specify a classname for the root tag ) ); // be careful to always use the ampersand in front of the new operator $unserializer = &new XML_Unserializer($options); $status = $unserializer->unserialize('http://pear.php.net/feeds/latest.rss',true); if (PEAR::isError($status)) { echo 'Error: ' . $status->getMessage(); } else { $rss = $unserializer->getUnserializedData(); echo 'This has been returned by XML_Unserializer:
'; echo '
';
    print_r($rss);
    echo '
'; echo '

Root Tagname: '.$unserializer->getRootName().'
'; echo 'Title of the channel: '.$rss->channel->getTitle().'
'; $items = $rss->getItems(3); echo '
Titles of the last three releases:
'; foreach ($items as $item) { echo 'Title : '.$item->getTitle().'
'; } } ?>XML_Serializer-0.20.2/examples/unserializeWhitespace.php0000600000175000017500000000213011461374326024101 0ustar clockwerxclockwerx */ error_reporting(E_ALL); require_once 'XML/Unserializer.php'; $xml = ' This XML document contains line breaks. '; // be careful to always use the ampersand in front of the new operator $unserializer = &new XML_Unserializer(); $unserializer->setOption(XML_UNSERIALIZER_OPTION_RETURN_RESULT, true); echo '
';

echo "Default behavior (XML_UNSERIALIZER_WHITESPACE_TRIM)\n";
$data = $unserializer->unserialize($xml);
var_dump($data);
echo "\n\n";

echo "Normalize whitespace (XML_UNSERIALIZER_WHITESPACE_NORMALIZE)\n";
$unserializer->setOption(XML_UNSERIALIZER_OPTION_WHITESPACE, XML_UNSERIALIZER_WHITESPACE_NORMALIZE);
$data = $unserializer->unserialize($xml);
var_dump($data);
echo "\n\n";

echo "Keep whitespace (XML_UNSERIALIZER_WHITESPACE_KEEP)\n";
$unserializer->setOption(XML_UNSERIALIZER_OPTION_WHITESPACE, XML_UNSERIALIZER_WHITESPACE_KEEP);
$data = $unserializer->unserialize($xml);
var_dump($data);

echo '
'; ?>XML_Serializer-0.20.2/examples/unserializeWithAttributes.php0000600000175000017500000000143411461374326024775 0ustar clockwerxclockwerx * @uses example.xml */ error_reporting(E_ALL); require_once 'XML/Unserializer.php'; $options = array( XML_UNSERIALIZER_OPTION_ATTRIBUTES_PARSE => true, XML_UNSERIALIZER_OPTION_ATTRIBUTES_ARRAYKEY => false ); // be careful to always use the ampersand in front of the new operator $unserializer = &new XML_Unserializer($options); // userialize the document $status = $unserializer->unserialize('example.xml', true); if (PEAR::isError($status)) { echo 'Error: ' . $status->getMessage(); } else { $data = $unserializer->getUnserializedData(); echo '
';
    print_r($data);
    echo '
'; } ?>XML_Serializer-0.20.2/examples/unserializeWithTagMap.php0000600000175000017500000000317511461374326024024 0ustar clockwerxclockwerx */ error_reporting(E_ALL); // this is a simple XML document $xml = '' . ' FOO' . ' BAR' . ''; require_once 'XML/Unserializer.php'; // complex structures are arrays, the key is the attribute 'handle' or 'name', if handle is not present $options = array( XML_UNSERIALIZER_OPTION_COMPLEXTYPE => 'array', XML_UNSERIALIZER_OPTION_TAG_MAP => array( 'foo' => 'bar', 'bar' => 'foo' ) ); // be careful to always use the ampersand in front of the new operator $unserializer = &new XML_Unserializer($options); // userialize the document $status = $unserializer->unserialize($xml, false); if (PEAR::isError($status)) { echo 'Error: ' . $status->getMessage(); } else { $data = $unserializer->getUnserializedData(); echo '
';
    print_r($data);
    echo '
'; } // a tad more complex $xml = '' . ' '. ' 45'. ' '. ' '. ' 31'. ' '. ''; // userialize the document $status = $unserializer->unserialize($xml, false); if (PEAR::isError($status)) { echo 'Error: ' . $status->getMessage(); } else { $data = $unserializer->getUnserializedData(); echo '
';
    print_r($data);
    echo '
'; } ?>XML_Serializer-0.20.2/examples/unserializeWithTypeGuessing.php0000600000175000017500000000166611461374326025304 0ustar clockwerxclockwerx * @category XML * @package XML_Serializer * @subpackage Examples */ error_reporting(E_ALL); /** * uses XML_Unserializer */ require_once '../Unserializer.php'; $xml = << Just a string... true -563 4.73736 true EOT; $unserializer = &new XML_Unserializer(); $unserializer->setOption(XML_UNSERIALIZER_OPTION_COMPLEXTYPE, 'array'); $unserializer->setOption(XML_UNSERIALIZER_OPTION_ATTRIBUTES_PARSE, true); $unserializer->setOption(XML_UNSERIALIZER_OPTION_RETURN_RESULT, true); $unserializer->setOption(XML_UNSERIALIZER_OPTION_GUESS_TYPES, true); $data = $unserializer->unserialize($xml); echo '
';
var_dump($data);
echo '
'; ?>XML_Serializer-0.20.2/tests/AllTests.php0000600000175000017500000001554311461374326020625 0ustar clockwerxclockwerx * @license http://www.opensource.org/licenses/bsd-license.php New BSD License * @version CVS: $Id: AllTests.php 266449 2008-09-17 16:57:07Z ashnazg $ * @link http://pear.php.net/package/XML_Serializer * @since 0.19.1 */ /** * Check PHP version... PhpUnit v3+ requires at least PHP v5.1.4 */ if (version_compare(PHP_VERSION, '5.1.4') < 0) { // Cannnot run test suites echo 'Cannot run test suite via PhpUnit... requires at least PHP v5.1.4.' . PHP_EOL; echo 'Use "pear run-tests -p xml_util" to run the PHPT tests directly.' . PHP_EOL ; exit(1); } /** * Derive the "main" method name * @internal PhpUnit would have to rename PHPUnit_MAIN_METHOD to PHPUNIT_MAIN_METHOD * to make this usage meet the PEAR CS... we cannot rename it here. */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Serializer_AllTests::main'); } /* * Files needed by PhpUnit */ require_once 'PHPUnit/Framework.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'PHPUnit/Extensions/PhptTestSuite.php'; /* * You must add each additional class-level test suite file here */ require_once 'Serializer_Arrays_TestCase.php'; require_once 'Serializer_Objects_TestCase.php'; require_once 'Serializer_Option_AttributesContent_TestCase.php'; require_once 'Serializer_Option_CDataSections_TestCase.php'; require_once 'Serializer_Option_ClassName_TestCase.php'; require_once 'Serializer_Option_Comment_TestCase.php'; require_once 'Serializer_Option_DefaultTag_TestCase.php'; require_once 'Serializer_Option_DocType_TestCase.php'; require_once 'Serializer_Option_EncodeFunc_TestCase.php'; require_once 'Serializer_Option_IgnoreNull_TestCase.php'; require_once 'Serializer_Option_Indent_TestCase.php'; require_once 'Serializer_Option_Linebreaks_TestCase.php'; require_once 'Serializer_Option_Mode_TestCase.php'; require_once 'Serializer_Option_Namespace_TestCase.php'; require_once 'Serializer_Option_ReturnResult_TestCase.php'; require_once 'Serializer_Option_RootAttributes_TestCase.php'; require_once 'Serializer_Option_RootName_TestCase.php'; require_once 'Serializer_Option_TagMap_TestCase.php'; require_once 'Serializer_Option_TypeHints_TestCase.php'; require_once 'Serializer_Option_XmlDeclaration_TestCase.php'; require_once 'Serializer_Scalars_TestCase.php'; require_once 'Unserializer_Arrays_TestCase.php'; require_once 'Unserializer_Objects_TestCase.php'; require_once 'Unserializer_Option_Encodings_TestCase.php'; require_once 'Unserializer_Option_GuessTypes_TestCase.php'; require_once 'Unserializer_Option_Whitespace_TestCase.php'; require_once 'Unserializer_Scalars_TestCase.php'; /** * directory where PHPT tests are located */ define('XML_SERIALIZER_DIR_PHPT', dirname(__FILE__)); /** * Master Unit Test Suite class for XML_Serializer * * This top-level test suite class organizes * all class test suite files, * so that the full suite can be run * by PhpUnit or via "pear run-tests -up xml_util". * * @category XML * @package XML_Serializer * @subpackage tests * @author Chuck Burgess * @license http://www.opensource.org/licenses/bsd-license.php New BSD License * @version Release: @package_version@ * @link http://pear.php.net/package/XML_Serializer * @since 0.19.1 */ class XML_Serializer_AllTests { /** * Launches the TextUI test runner * * @return void * @uses PHPUnit_TextUI_TestRunner */ public static function main() { PHPUnit_TextUI_TestRunner::run(self::suite()); } /** * Adds all class test suites into the master suite * * @return PHPUnit_Framework_TestSuite a master test suite * containing all class test suites * @uses PHPUnit_Framework_TestSuite */ public static function suite() { $suite = new PHPUnit_Framework_TestSuite( 'XML_Serializer Full Suite of Unit Tests'); /* * You must add each additional class-level test suite name here */ $suite->addTestSuite('XML_Serializer_Arrays_TestCase'); $suite->addTestSuite('XML_Serializer_Objects_TestCase'); $suite->addTestSuite('XML_Serializer_Option_AttributesContent_TestCase'); $suite->addTestSuite('XML_Serializer_Option_CDataSections_TestCase'); $suite->addTestSuite('XML_Serializer_Option_ClassName_TestCase'); $suite->addTestSuite('XML_Serializer_Option_Comment_TestCase'); $suite->addTestSuite('XML_Serializer_Option_DefaultTag_TestCase'); $suite->addTestSuite('XML_Serializer_Option_DocType_TestCase'); $suite->addTestSuite('XML_Serializer_Option_EncodeFunc_TestCase'); $suite->addTestSuite('XML_Serializer_Option_IgnoreNull_TestCase'); $suite->addTestSuite('XML_Serializer_Option_Indent_TestCase'); $suite->addTestSuite('XML_Serializer_Option_Linebreaks_TestCase'); $suite->addTestSuite('XML_Serializer_Option_Mode_TestCase'); $suite->addTestSuite('XML_Serializer_Option_Namespace_TestCase'); $suite->addTestSuite('XML_Serializer_Option_ReturnResult_TestCase'); $suite->addTestSuite('XML_Serializer_Option_RootAttributes_TestCase'); $suite->addTestSuite('XML_Serializer_Option_RootName_TestCase'); $suite->addTestSuite('XML_Serializer_Option_TagMap_TestCase'); $suite->addTestSuite('XML_Serializer_Option_TypeHints_TestCase'); $suite->addTestSuite('XML_Serializer_Option_XmlDeclaration_TestCase'); $suite->addTestSuite('XML_Serializer_Scalars_TestCase'); $suite->addTestSuite('XML_Unserializer_Arrays_TestCase'); $suite->addTestSuite('XML_Unserializer_Objects_TestCase'); $suite->addTestSuite('XML_Unserializer_Option_Encodings_TestCase'); $suite->addTestSuite('XML_Unserializer_Option_GuessTypes_TestCase'); $suite->addTestSuite('XML_Unserializer_Option_Whitespace_TestCase'); $suite->addTestSuite('XML_Unserializer_Scalars_TestCase'); /* * add PHPT tests */ $phpt = new PHPUnit_Extensions_PhptTestSuite(XML_SERIALIZER_DIR_PHPT); $suite->addTestSuite($phpt); return $suite; } } /** * Call the main method if this file is executed directly * @internal PhpUnit would have to rename PHPUnit_MAIN_METHOD to PHPUNIT_MAIN_METHOD * to make this usage meet the PEAR CS... we cannot rename it here. */ if (PHPUnit_MAIN_METHOD == 'XML_Serializer_AllTests::main') { XML_Serializer_AllTests::main(); } /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ ?> XML_Serializer-0.20.2/tests/bug-9799.phpt0000600000175000017500000000217211461374326020444 0ustar clockwerxclockwerx--TEST-- XML Serializer - Bug #9799: attributesArray is poorly named --FILE-- array( array('id'=>'blue', '-' => 'red'), array('id'=>'qqq', '-' => 'green') ) ); $options = array( 'addDecl' => true, 'rootName'=>'truecolor', 'indent'=>' ', 'mode'=>'simplexml', 'scalarAsAttributes'=>array('f'=>array('id')), 'attributesArray'=>'+', 'contentName'=>'-', ); $s = new XML_Serializer($options); $status = $s->serialize($color); echo $s->getSerializedData(); ?> --EXPECT-- red green XML_Serializer-0.20.2/tests/bug-12139.phpt0000600000175000017500000000102211461374326020473 0ustar clockwerxclockwerx--TEST-- XML Serializer - test for Bug #12139: XML_Parser read '#' in data as Invalid document end --FILE-- Some text with #sign in it EOF; $unserializer = &new XML_Unserializer(); $status = $unserializer->unserialize($xml); if (PEAR::isError($status)) { echo $status->getMessage(); } $plan = $unserializer->getUnserializedData(); var_dump($plan); ?> --EXPECT-- array(1) { ["foo"]=> string(26) "Some text with #sign in it" } XML_Serializer-0.20.2/tests/bug-13896.phpt0000600000175000017500000000402311461374326020512 0ustar clockwerxclockwerx--TEST-- XML Serializer - Bug #13896: Bad info in the RSS feed tutorial --FILE-- " ", "linebreak" => "\n", "typeHints" => false, "addDecl" => true, "encoding" => "UTF-8", "rootName" => "rdf:RDF", "defaultTagName" => "item" ); $stories[] = array( 'title' => 'First Article', 'link' => 'http://freedomink.org/node/view/55', 'description' => 'Short blurb about article........' ); $stories[] = array( 'title' => 'Second Article', 'link' => 'http://freedomink.org/node/view/11', 'description' => 'This article shows you how ......' ); $data['channel'] = array( "title" => "Freedom Ink", "link" => "http://freedomink.org/", $stories ); /** * might later considering rewriting it * to show the option setting this way $ser= new XML_Serializer(); $ser->setOption(XML_SERIALIZER_OPTION_INDENT, ' '); $ser->setOption(XML_SERIALIZER_OPTION_XML_DECL_ENABLED, true); $ser->setOption(XML_SERIALIZER_OPTION_XML_ENCODING, 'UTF-8'); $ser->setOption(XML_SERIALIZER_OPTION_ROOT_NAME, 'rdf:RDF'); $ser->setOption(XML_SERIALIZER_OPTION_DEFAULT_TAG, 'item'); **/ $ser = new XML_Serializer($options); if ($ser->serialize($data)) { header('Content-type: text/xml'); echo $ser->getSerializedData(); } ?> --EXPECT-- Freedom Ink http://freedomink.org/ First Article http://freedomink.org/node/view/55 Short blurb about article........ Second Article http://freedomink.org/node/view/11 This article shows you how ...... XML_Serializer-0.20.2/tests/bug-15602.phpt0000600000175000017500000000253011461374326020476 0ustar clockwerxclockwerx--TEST-- XML Serializer - Bug #15602: attributes don't get escaped sometimes --FILE-- ' ', XML_SERIALIZER_OPTION_LINEBREAKS => "\n", XML_SERIALIZER_OPTION_ROOT_NAME => 'FooTag', XML_SERIALIZER_OPTION_MODE => XML_SERIALIZER_MODE_SIMPLEXML, XML_SERIALIZER_OPTION_ATTRIBUTES_KEY => XML_ATTR, XML_SERIALIZER_OPTION_XML_ENCODING => 'UTF-8', XML_SERIALIZER_OPTION_XML_DECL_ENABLED => true, XML_SERIALIZER_OPTION_ENTITIES => XML_SERIALIZER_ENTITIES_XML, ); $v = 'I say: "A", B & C, \'d\'!'; $a = array('attr1' => $v); $xml = array( XML_ATTR => $a, 'tag1' => array(XML_ATTR => $a, 'tag2' => $v), 'tag3' => array(XML_ATTR => $a, $v), ); $serializer = new XML_Serializer($options); $serializer->serialize($xml); echo $serializer->getSerializedData(); ?> --EXPECT-- I say: "A", B & C, 'd'! I say: "A", B & C, 'd'! XML_Serializer-0.20.2/tests/req-13564.phpt0000600000175000017500000000222211461374326020513 0ustar clockwerxclockwerx--TEST-- XML_Serializer - Req #13564: bool(false) is converted to empty string --FILE-- setOption('rootName', 'BooleanTest'); if ($serializer->serialize(false)) { var_dump($serializer->getSerializedData()); } if ($serializer->serialize(true)) { var_dump($serializer->getSerializedData()); } echo PHP_EOL; // modified to XML_SERIALIZER_OPTION_FALSE_AS_STRING = true echo 'New behavior when enabled:' . PHP_EOL; $serializer = new XML_Serializer(); $serializer->setOption(XML_SERIALIZER_OPTION_FALSE_AS_STRING, true); $serializer->setOption('rootName', 'BooleanTest'); if ($serializer->serialize(false)) { var_dump($serializer->getSerializedData()); } if ($serializer->serialize(true)) { var_dump($serializer->getSerializedData()); } ?> --EXPECT-- Default (original) behavior: string(15) "" string(28) "1" New behavior when enabled: string(28) "0" string(28) "1" XML_Serializer-0.20.2/tests/Serializer_Arrays_TestCase.php0000600000175000017500000000520311461374326024307 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Serializer_Arrays_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Serializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Serializer_Arrays_TestCase extends PHPUnit_Framework_TestCase { private $options = array( XML_SERIALIZER_OPTION_INDENT => '', XML_SERIALIZER_OPTION_LINEBREAKS => '', ); public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Serializer_Arrays_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Test serializing a numbered array */ public function testNumberedArray() { $s = new XML_Serializer($this->options); $s->serialize(array('one', 'two', 'three')); $this->assertEquals( 'onetwothree' , $s->getSerializedData() ); } /** * Test serializing an assoc array */ public function testAssocArray() { $s = new XML_Serializer($this->options); $s->serialize(array('one' => 'foo', 'two' => 'bar')); $this->assertEquals( 'foobar' , $s->getSerializedData() ); } /** * Test serializing an mixed array */ public function testMixedArray() { $s = new XML_Serializer($this->options); $s->serialize(array('one' => 'foo', 'two' => 'bar', 'three')); $this->assertEquals( 'foobarthree' , $s->getSerializedData() ); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Serializer_Arrays_TestCase::main') { XML_Serializer_Arrays_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Serializer_Objects_TestCase.php0000600000175000017500000000575011461374326024446 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Serializer_Objects_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Serializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Serializer_Objects_TestCase extends PHPUnit_Framework_TestCase { private $options = array( XML_SERIALIZER_OPTION_INDENT => '', XML_SERIALIZER_OPTION_LINEBREAKS => '', ); public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Serializer_Objects_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Test serializing an object without any properties */ public function testEmptyObject() { $s = new XML_Serializer($this->options); $s->serialize(new stdClass()); $this->assertEquals('', $s->getSerializedData()); } /** * Test serializing a simple object */ public function testSimpleObject() { $obj = new stdClass(); $obj->foo = 'bar'; $s = new XML_Serializer($this->options); $s->serialize($obj); $this->assertEquals('bar', $s->getSerializedData()); } /** * Test serializing a nested object */ public function testNestedObject() { $obj = new stdClass(); $obj->foo = new stdClass(); $obj->foo->bar = 'nested'; $s = new XML_Serializer($this->options); $s->serialize($obj); $this->assertEquals('nested', $s->getSerializedData()); } /** * Test serializing an object, that supports __sleep */ public function testSleep() { $obj = new MyClass('foo', 'bar'); $s = new XML_Serializer($this->options); $s->serialize($obj); $this->assertEquals('foo', $s->getSerializedData()); } } class MyClass { var $foo; var $bar; public function MyClass($foo, $bar) { $this->foo = $foo; $this->bar = $bar; } public function __sleep() { return array('foo'); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Serializer_Objects_TestCase::main') { XML_Serializer_Objects_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Serializer_Option_AttributesContent_TestCase.php0000600000175000017500000000716411461374326030067 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Serializer_Option_AttributesContent_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Serializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Serializer_Option_AttributesContent_TestCase extends PHPUnit_Framework_TestCase { private $options = array( XML_SERIALIZER_OPTION_INDENT => '', XML_SERIALIZER_OPTION_LINEBREAKS => '', XML_SERIALIZER_OPTION_ATTRIBUTES_KEY => 'atts', XML_SERIALIZER_OPTION_CONTENT_KEY => 'content' ); public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Serializer_Option_AttributesContent_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Test attributes */ public function testAttribs() { $s = new XML_Serializer($this->options); $data = array( 'foo' => array( 'atts' => array('one' => 1), 'bar' => 'bar' ) ); $s->serialize($data); $this->assertEquals( 'bar' , $s->getSerializedData() ); } /** * Test content */ public function testContent() { $s = new XML_Serializer($this->options); $data = array( 'foo' => array( 'atts' => array('one' => 1), 'content' => 'some data', ) ); $s->serialize($data); $this->assertEquals( 'some data' , $s->getSerializedData() ); } /** * Test both */ public function testMixed() { $s = new XML_Serializer($this->options); $data = array( 'foo' => array( 'atts' => array('one' => 1), 'content' => 'some data', 'bar' => 'bar' ) ); $s->serialize($data); $this->assertEquals( 'some databar' , $s->getSerializedData() ); } /** * Test indexed */ public function testNumbered() { $s = new XML_Serializer($this->options); $data = array( 'foo' => array( 'atts' => array('one' => 1), 'content' => 'some data', 'bar', 'foo' ) ); $s->serialize($data); $this->assertEquals( 'some databarfoo' , $s->getSerializedData() ); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Serializer_Option_AttributesContent_TestCase::main') { XML_Serializer_Option_AttributesContent_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Serializer_Option_CDataSections_TestCase.php0000600000175000017500000000426311461374326027067 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Serializer_Option_CDataSections_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Serializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Serializer_Option_CDataSections_TestCase extends PHPUnit_Framework_TestCase { private $options = array( XML_SERIALIZER_OPTION_INDENT => '', XML_SERIALIZER_OPTION_LINEBREAKS => '', XML_SERIALIZER_OPTION_CDATA_SECTIONS => true ); public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Serializer_Option_CDataSections_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Test a simple string */ public function testString() { $s = new XML_Serializer($this->options); $s->serialize('a string'); $this->assertEquals( '' , $s->getSerializedData() ); } /** * Test a string with entities */ public function testEntities() { $s = new XML_Serializer($this->options); $s->serialize('& < > "'); $this->assertEquals( ' "]]>' , $s->getSerializedData() ); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Serializer_Option_CDataSections_TestCase::main') { XML_Serializer_Option_CDataSections_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Serializer_Option_ClassName_TestCase.php0000600000175000017500000000366211461374326026253 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Serializer_Option_ClassName_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Serializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Serializer_Option_ClassName_TestCase extends PHPUnit_Framework_TestCase { private $options = array( XML_SERIALIZER_OPTION_INDENT => '', XML_SERIALIZER_OPTION_LINEBREAKS => '', XML_SERIALIZER_OPTION_CLASSNAME_AS_TAGNAME => true ); public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Serializer_Option_ClassName_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Test setting a global default tag */ public function testSimple() { $s = new XML_Serializer($this->options); $s->serialize(array(new stdClass(), new stdClass())); $this->assertEquals( '' , strtolower($s->getSerializedData()) ); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Serializer_Option_ClassName_TestCase::main') { XML_Serializer_Option_ClassName_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Serializer_Option_Comment_TestCase.php0000600000175000017500000000365511461374326026011 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Serializer_Option_Comment_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Serializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Serializer_Option_Comment_TestCase extends PHPUnit_Framework_TestCase { private $options = array( XML_SERIALIZER_OPTION_INDENT => '', XML_SERIALIZER_OPTION_LINEBREAKS => '', XML_SERIALIZER_OPTION_COMMENT_KEY => 'comment' ); public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Serializer_Option_Comment_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Test comment */ public function testComment() { $s = new XML_Serializer($this->options); $s->serialize(array('test' => array('comment' => 'This is a test', 'foo' => 'bar'))); $this->assertEquals( 'bar' , $s->getSerializedData() ); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Serializer_Option_Comment_TestCase::main') { XML_Serializer_Option_Comment_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Serializer_Option_DefaultTag_TestCase.php0000600000175000017500000000607211461374326026423 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Serializer_Option_DefaultTag_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Serializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Serializer_Option_DefaultTag_TestCase extends PHPUnit_Framework_TestCase { private $options = array( XML_SERIALIZER_OPTION_INDENT => '', XML_SERIALIZER_OPTION_LINEBREAKS => '', ); public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Serializer_Option_DefaultTag_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Test setting a global default tag */ public function testSimple() { $s = new XML_Serializer($this->options); $s->setOption(XML_SERIALIZER_OPTION_DEFAULT_TAG, 'tag'); $s->serialize(array('one', 'two', 'three')); $this->assertEquals( 'onetwothree' , $s->getSerializedData() ); } /** * Test setting context sensitive default tags */ public function testContext() { $s = new XML_Serializer($this->options); $data = array( 'foos' => array(1,2), 'bars' => array(1,2), ); $s->setOption(XML_SERIALIZER_OPTION_DEFAULT_TAG, array('foos' => 'foo', 'bars' => 'bar')); $s->serialize($data); $this->assertEquals( '1212' , $s->getSerializedData() ); } /** * Test setting mixed default tags */ public function testMixed() { $s = new XML_Serializer($this->options); $data = array( 'foos' => array(1,2), 'bars' => array(1,2), 'test' ); $s->setOption(XML_SERIALIZER_OPTION_DEFAULT_TAG, array('foos' => 'foo', '#default' => 'tag')); $s->serialize($data); $this->assertEquals( '1212test' , $s->getSerializedData() ); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Serializer_Option_DefaultTag_TestCase::main') { XML_Serializer_Option_DefaultTag_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Serializer_Option_DocType_TestCase.php0000600000175000017500000000600311461374326025744 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Serializer_Option_DocType_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Serializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Serializer_Option_DocType_TestCase extends PHPUnit_Framework_TestCase { private $options = array( XML_SERIALIZER_OPTION_INDENT => '', XML_SERIALIZER_OPTION_LINEBREAKS => '', ); public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Serializer_Option_DocType_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Declaration */ public function testDeclaration() { $s = new XML_Serializer($this->options); $s->setOption(XML_SERIALIZER_OPTION_DOCTYPE_ENABLED, true); $s->serialize('string'); $this->assertEquals( 'string' , $s->getSerializedData() ); } /** * Declaration with System reference */ public function testSystem() { $s = new XML_Serializer($this->options); $s->setOption(XML_SERIALIZER_OPTION_DOCTYPE_ENABLED, true); $s->setOption(XML_SERIALIZER_OPTION_DOCTYPE, '/path/to/doctype.dtd'); $s->serialize('string'); $this->assertEquals( 'string' , $s->getSerializedData() ); } /** * Declaration and ID and system reference */ public function testId() { $s = new XML_Serializer($this->options); $s->setOption(XML_SERIALIZER_OPTION_DOCTYPE_ENABLED, true); $s->setOption( XML_SERIALIZER_OPTION_DOCTYPE , array( 'uri' => 'http://pear.php.net/dtd/package-1.0', 'id' => '-//PHP//PEAR/DTD PACKAGE 1.0' ) ); $s->serialize('string'); $this->assertEquals( 'string' , $s->getSerializedData() ); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Serializer_Option_DocType_TestCase::main') { XML_Serializer_Option_DocType_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Serializer_Option_EncodeFunc_TestCase.php0000600000175000017500000000500311461374326026405 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Serializer_Option_EncodeFunc_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Serializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Serializer_Option_EncodeFunc_TestCase extends PHPUnit_Framework_TestCase { private $options = array( XML_SERIALIZER_OPTION_INDENT => '', XML_SERIALIZER_OPTION_LINEBREAKS => '', XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES => true, XML_SERIALIZER_OPTION_ENCODE_FUNC => 'strtoupper' ); public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Serializer_Option_EncodeFunc_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Test encode function with cdata */ public function testCData() { $s = new XML_Serializer($this->options); $s->serialize('a string'); $this->assertEquals('A STRING', $s->getSerializedData()); } /** * Test encode function with attributes */ public function testAttributes() { $s = new XML_Serializer($this->options); $s->serialize(array('foo' => 'bar')); $this->assertEquals('', $s->getSerializedData()); } /** * Test encode function with cdata */ public function testMixed() { $s = new XML_Serializer($this->options); $s->serialize(array('foo' => 'bar', 'tomato')); $this->assertEquals('TOMATO', $s->getSerializedData()); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Serializer_Option_EncodeFunc_TestCase::main') { XML_Serializer_Option_EncodeFunc_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Serializer_Option_IgnoreNull_TestCase.php0000600000175000017500000000425711461374326026464 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Serializer_Option_IgnoreNull_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Serializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Serializer_Option_IgnoreNull_TestCase extends PHPUnit_Framework_TestCase { private $options = array( XML_SERIALIZER_OPTION_INDENT => '', XML_SERIALIZER_OPTION_LINEBREAKS => '', XML_SERIALIZER_OPTION_IGNORE_NULL => true ); public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Serializer_Option_IgnoreNull_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Array with null value */ public function testArray() { $s = new XML_Serializer($this->options); $s->serialize(array('foo' => 'bar', 'null' => null)); $this->assertEquals('bar', $s->getSerializedData()); } /** * Object with null value */ public function testObject() { $obj = new stdClass(); $obj->foo = 'bar'; $obj->null = null; $s = new XML_Serializer($this->options); $s->serialize($obj); $this->assertEquals('bar', $s->getSerializedData()); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Serializer_Option_IgnoreNull_TestCase::main') { XML_Serializer_Option_IgnoreNull_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Serializer_Option_Indent_TestCase.php0000600000175000017500000000412611461374326025622 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Serializer_Option_Indent_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Serializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Serializer_Option_Indent_TestCase extends PHPUnit_Framework_TestCase { private $options = array( XML_SERIALIZER_OPTION_LINEBREAKS => "\n", ); public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Serializer_Option_Indent_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Indent with spaces */ public function testSpaces() { $s = new XML_Serializer($this->options); $s->setOption(XML_SERIALIZER_OPTION_INDENT, ' '); $s->serialize(array('foo' => 'bar')); $this->assertEquals(' bar ', $s->getSerializedData()); } /** * Indent with tabs */ public function testTabs() { $s = new XML_Serializer($this->options); $s->setOption(XML_SERIALIZER_OPTION_INDENT, "\t"); $s->serialize(array('foo' => 'bar')); $this->assertEquals(' bar ', $s->getSerializedData()); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Serializer_Option_Indent_TestCase::main') { XML_Serializer_Option_Indent_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Serializer_Option_Linebreaks_TestCase.php0000600000175000017500000000343511461374326026462 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Serializer_Option_Linebreaks_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Serializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Serializer_Option_Linebreaks_TestCase extends PHPUnit_Framework_TestCase { private $options = array( XML_SERIALIZER_OPTION_INDENT => '', XML_SERIALIZER_OPTION_LINEBREAKS => "\n", ); public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Serializer_Option_Linebreaks_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Test a simple string */ public function testUnixLinebreak() { $s = new XML_Serializer($this->options); $s->serialize(array('foo' => 'bar')); $this->assertEquals(' bar ', $s->getSerializedData()); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Serializer_Option_Linebreaks_TestCase::main') { XML_Serializer_Option_Linebreaks_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Serializer_Option_Mode_TestCase.php0000600000175000017500000000470611461374326025271 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Serializer_Option_Mode_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Serializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Serializer_Option_Mode_TestCase extends PHPUnit_Framework_TestCase { private $options = array( XML_SERIALIZER_OPTION_INDENT => '', XML_SERIALIZER_OPTION_LINEBREAKS => '', ); public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Serializer_Option_Mode_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Default mode */ public function testDefault() { $s = new XML_Serializer($this->options); $s->serialize(array('foo' => array(1, 2, 3), 'bar' => array(1, 2, 3))); $this->assertEquals('123123', $s->getSerializedData()); } /** * SimpleXML */ public function testSimpleXML() { $s = new XML_Serializer($this->options); $s->setOption(XML_SERIALIZER_OPTION_MODE, XML_SERIALIZER_MODE_SIMPLEXML); $s->serialize(array('foo' => array(1, 2, 3), 'bar' => array(1, 2, 3))); $this->assertEquals('123123', $s->getSerializedData()); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Serializer_Option_Mode_TestCase::main') { XML_Serializer_Option_Mode_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Serializer_Option_Namespace_TestCase.php0000600000175000017500000000442211461374326026274 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Serializer_Option_Namespace_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Serializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Serializer_Option_Namespace_TestCase extends PHPUnit_Framework_TestCase { private $options = array( XML_SERIALIZER_OPTION_INDENT => '', XML_SERIALIZER_OPTION_LINEBREAKS => '', ); public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Serializer_Option_Namespace_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Simple namespace */ public function testSimple() { $s = new XML_Serializer($this->options); $s->setOption(XML_SERIALIZER_OPTION_NAMESPACE, 'foo'); $s->serialize(array('foo' => 'bar')); $this->assertEquals('bar', $s->getSerializedData()); } /** * Simple namespace */ public function testUri() { $s = new XML_Serializer($this->options); $s->setOption(XML_SERIALIZER_OPTION_NAMESPACE, array('foo', 'http://pear.php.net/XML_Serializer/foo')); $s->serialize(array('foo' => 'bar')); $this->assertEquals('bar', $s->getSerializedData()); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Serializer_Option_Namespace_TestCase::main') { XML_Serializer_Option_Namespace_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Serializer_Option_ReturnResult_TestCase.php0000600000175000017500000000346711461374326027066 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Serializer_Option_ReturnResult_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Serializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Serializer_Option_ReturnResult_TestCase extends PHPUnit_Framework_TestCase { private $options = array( XML_SERIALIZER_OPTION_INDENT => '', XML_SERIALIZER_OPTION_LINEBREAKS => '', XML_SERIALIZER_OPTION_RETURN_RESULT => true ); public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Serializer_Option_ReturnResult_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Test encode function with cdata */ public function testReturnResult() { $s = new XML_Serializer($this->options); $this->assertEquals('a string', $s->serialize('a string')); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Serializer_Option_ReturnResult_TestCase::main') { XML_Serializer_Option_ReturnResult_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Serializer_Option_RootAttributes_TestCase.php0000600000175000017500000000413511461374326027373 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Serializer_Option_RootAttributes_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Serializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Serializer_Option_RootAttributes_TestCase extends PHPUnit_Framework_TestCase { private $options = array( XML_SERIALIZER_OPTION_INDENT => '', XML_SERIALIZER_OPTION_LINEBREAKS => '', XML_SERIALIZER_OPTION_ROOT_ATTRIBS => array('foo' => 'bar') ); public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Serializer_Option_RootAttributes_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Array */ public function testString() { $s = new XML_Serializer($this->options); $s->serialize('string'); $this->assertEquals('string', $s->getSerializedData()); } /** * Array */ public function testArray() { $s = new XML_Serializer($this->options); $s->serialize(array('foo' => 'bar')); $this->assertEquals('bar', $s->getSerializedData()); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Serializer_Option_RootAttributes_TestCase::main') { XML_Serializer_Option_RootAttributes_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Serializer_Option_RootName_TestCase.php0000600000175000017500000000402011461374326026116 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Serializer_Option_RootName_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Serializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Serializer_Option_RootName_TestCase extends PHPUnit_Framework_TestCase { private $options = array( XML_SERIALIZER_OPTION_INDENT => '', XML_SERIALIZER_OPTION_LINEBREAKS => '', XML_SERIALIZER_OPTION_ROOT_NAME => 'root' ); public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Serializer_Option_RootName_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Array */ public function testString() { $s = new XML_Serializer($this->options); $s->serialize('string'); $this->assertEquals('string', $s->getSerializedData()); } /** * Array */ public function testArray() { $s = new XML_Serializer($this->options); $s->serialize(array('foo' => 'bar')); $this->assertEquals('bar', $s->getSerializedData()); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Serializer_Option_RootName_TestCase::main') { XML_Serializer_Option_RootName_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Serializer_Option_TagMap_TestCase.php0000600000175000017500000000517011461374326025552 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Serializer_Option_TagMap_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Serializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Serializer_Option_TagMap_TestCase extends PHPUnit_Framework_TestCase { private $options = array( XML_SERIALIZER_OPTION_INDENT => '', XML_SERIALIZER_OPTION_LINEBREAKS => '', XML_SERIALIZER_OPTION_TAGMAP => array('foo' => 'bar') ); public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Serializer_Option_TagMap_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Test array */ public function testArray() { $s = new XML_Serializer($this->options); $data = array( 'foo' => 'test' ); $s->serialize($data); $this->assertEquals('test', $s->getSerializedData()); } /** * Test object */ public function testObject() { $s = new XML_Serializer($this->options); $obj = new stdClass(); $obj->foo = 'test'; $s->serialize($obj); $this->assertEquals('test', strtolower($s->getSerializedData())); } /** * Test object */ public function testNumberedObjects() { $s = new XML_Serializer($this->options); $s->setOption(XML_SERIALIZER_OPTION_CLASSNAME_AS_TAGNAME, true); $s->setOption(XML_SERIALIZER_OPTION_TAGMAP, array('stdClass' => 'foo')); $s->serialize(array(new stdClass(), new stdClass())); $this->assertEquals('', strtolower($s->getSerializedData())); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Serializer_Option_TagMap_TestCase::main') { XML_Serializer_Option_TagMap_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Serializer_Option_TypeHints_TestCase.php0000600000175000017500000000475211461374326026335 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Serializer_Option_TypeHints_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Serializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Serializer_Option_TypeHints_TestCase extends PHPUnit_Framework_TestCase { private $options = array( XML_SERIALIZER_OPTION_INDENT => '', XML_SERIALIZER_OPTION_LINEBREAKS => '', XML_SERIALIZER_OPTION_TYPEHINTS => true ); public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Serializer_Option_TypeHints_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Test type * * @todo add more types */ public function testType() { $s = new XML_Serializer($this->options); $s->serialize('string'); $this->assertEquals('string', $s->getSerializedData()); } /** * Test original key */ public function testKey() { $s = new XML_Serializer($this->options); $s->serialize(array('foo bar' => 'bar')); $this->assertEquals('bar', $s->getSerializedData()); } /** * Test class */ public function testClass() { $s = new XML_Serializer($this->options); $s->serialize(array('foo' => new stdClass())); $this->assertEquals('', strtolower($s->getSerializedData())); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Serializer_Option_TypeHints_TestCase::main') { XML_Serializer_Option_TypeHints_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Serializer_Option_XmlDeclaration_TestCase.php0000600000175000017500000000442611461374326027312 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Serializer_Option_XmlDeclaration_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Serializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Serializer_Option_XmlDeclaration_TestCase extends PHPUnit_Framework_TestCase { private $options = array( XML_SERIALIZER_OPTION_INDENT => '', XML_SERIALIZER_OPTION_LINEBREAKS => '', ); public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Serializer_Option_XmlDeclaration_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Declaration */ public function testDeclaration() { $s = new XML_Serializer($this->options); $s->setOption(XML_SERIALIZER_OPTION_XML_DECL_ENABLED, true); $s->serialize('string'); $this->assertEquals('string', $s->getSerializedData()); } /** * Add encoding */ public function testEncoding() { $s = new XML_Serializer($this->options); $s->setOption(XML_SERIALIZER_OPTION_XML_DECL_ENABLED, true); $s->setOption(XML_SERIALIZER_OPTION_XML_ENCODING, 'ISO-8859-1'); $s->serialize('string'); $this->assertEquals('string', $s->getSerializedData()); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Serializer_Option_XmlDeclaration_TestCase::main') { XML_Serializer_Option_XmlDeclaration_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Serializer_Scalars_TestCase.php0000600000175000017500000000641611461374326024445 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Serializer_Scalars_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Serializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Serializer_Scalars_TestCase extends PHPUnit_Framework_TestCase { public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Serializer_Scalars_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Test serializing a string */ public function testString() { $s = new XML_Serializer(); $s->serialize('a string'); $this->assertEquals('a string', $s->getSerializedData()); $s->serialize(''); $this->assertEquals('', $s->getSerializedData()); } /** * Test serializing a integer */ public function testInteger() { $s = new XML_Serializer(); $s->serialize(456223); $this->assertEquals('456223', $s->getSerializedData()); $s->serialize(-34); $this->assertEquals('-34', $s->getSerializedData()); $s->serialize(0); $this->assertEquals('0', $s->getSerializedData()); } /** * Test serializing a float */ public function testDouble() { $s = new XML_Serializer(); $s->serialize(455.65); $this->assertEquals('455.65', $s->getSerializedData()); $s->serialize(-1.65451); $this->assertEquals('-1.65451', $s->getSerializedData()); } /** * Test serializing a boolean */ public function testBoolean() { $s = new XML_Serializer(); $s->serialize(true); $this->assertEquals('1', $s->getSerializedData()); $s->serialize(false); $this->assertEquals('', $s->getSerializedData()); } /** * Test serializing a null value */ public function testNull() { $s = new XML_Serializer(); $s->serialize(null); $this->assertEquals('', $s->getSerializedData()); } /** * Test serializing a resource */ public function testResource() { $s = new XML_Serializer(); $s->serialize(fopen(__FILE__, 'r')); $this->assertRegExp('/Resource id #[0-9]+<\/resource>/', $s->getSerializedData()); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Serializer_Scalars_TestCase::main') { XML_Serializer_Scalars_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/show_ScalarAsAttributes_usage.phpt0000600000175000017500000000446211461374326025240 0ustar clockwerxclockwerx--TEST-- XML Serializer - show proper usages of SCALAR_AS_ATTRIBUTES option --FILE-- setOption(XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES, true); $result = $ser->serialize( array( 'tagA' => array( 'tag2' => 2, 'tag3' => 'hi' ), 'tagB' => array( 'tag4' => 4, 'tag5' => 'bye' ) ) ); echo $ser->getSerializedData() . PHP_EOL . PHP_EOL; /** * all subtags under tagA will become attributes on tagA, * but subtags under tagB will remain as subtags. */ echo 'TEST: SCALAR_AS_ATTRIBUTES set to array(\'tagA\' => true)' . PHP_EOL; $ser->setOption(XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES, array( 'tagA' => true ) ); $result = $ser->serialize( array( 'tagA' => array( 'tag2' => 2, 'tag3' => 'hi' ), 'tagB' => array( 'tag4' => 4, 'tag5' => 'bye' ) ) ); echo $ser->getSerializedData() . PHP_EOL . PHP_EOL; /** * only subtag tag3 will become an attribute on tagA... * tag2 will remain a subtag under tagA... * nothing affects tagB... tag4 and tag5 remain subtags under it. */ echo 'TEST: SCALAR_AS_ATTRIBUTES set to array(\'tagA\' => array(\'tag3\'))' . PHP_EOL; $ser->setOption(XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES, array( 'tagA' => array('tag3') ) ); $result = $ser->serialize( array( 'tagA' => array( 'tag2' => 2, 'tag3' => 'hi' ), 'tagB' => array( 'tag4' => 4, 'tag5' => 'bye' ) ) ); echo $ser->getSerializedData() . PHP_EOL . PHP_EOL; ?> --EXPECT-- TEST: SCALAR_AS_ATTRIBUTES set TRUE TEST: SCALAR_AS_ATTRIBUTES set to array('tagA' => true) 4 bye TEST: SCALAR_AS_ATTRIBUTES set to array('tagA' => array('tag3')) 2 4 bye XML_Serializer-0.20.2/tests/Unserializer_Arrays_TestCase.php0000600000175000017500000000411011461374326024646 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Unserializer_Arrays_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Unserializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Unserializer_Arrays_TestCase extends PHPUnit_Framework_TestCase { public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Unserializer_Arrays_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Test unserializing a simple array */ public function testAssoc() { $u = new XML_Unserializer(); $u->setOption(XML_UNSERIALIZER_OPTION_COMPLEXTYPE, 'array'); $xml = 'bar'; $u->unserialize($xml); $this->assertEquals(array('foo' => 'bar'), $u->getUnserializedData()); } /** * Test unserializing an indexed array */ public function testIndexed() { $u = new XML_Unserializer(); $u->setOption(XML_UNSERIALIZER_OPTION_COMPLEXTYPE, 'array'); $xml = 'bartomato'; $u->unserialize($xml); $this->assertEquals(array('foo' => array('bar', 'tomato')), $u->getUnserializedData()); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Unserializer_Arrays_TestCase::main') { XML_Unserializer_Arrays_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Unserializer_Objects_TestCase.php0000600000175000017500000000667111461374326025014 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Unserializer_Objects_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Unserializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Unserializer_Objects_TestCase extends PHPUnit_Framework_TestCase { public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Unserializer_Objects_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Test unserializing to a stdClass object */ public function testStdClass() { $u = new XML_Unserializer(); $u->setOption(XML_UNSERIALIZER_OPTION_COMPLEXTYPE, 'object'); $xml = 'bar'; $u->unserialize($xml); $result = new stdClass(); $result->foo = 'bar'; $this->assertEquals($result, $u->getUnserializedData()); } /** * Test unserializing to a custom class */ public function testDefaultClass() { $u = new XML_Unserializer(); $u->setOption(XML_UNSERIALIZER_OPTION_COMPLEXTYPE, 'object'); $u->setOption(XML_UNSERIALIZER_OPTION_DEFAULT_CLASS, 'Foo'); $xml = 'bar'; $u->unserialize($xml); $result = new Foo(); $result->foo = 'bar'; $this->assertEquals($result, $u->getUnserializedData()); } /** * Test unserializing to a class based on the tag name */ public function testTagnameAsClass() { $u = new XML_Unserializer(); $u->setOption(XML_UNSERIALIZER_OPTION_COMPLEXTYPE, 'object'); $u->setOption(XML_UNSERIALIZER_OPTION_DEFAULT_CLASS, 'Foo'); $xml = 'tomato'; $u->unserialize($xml); $result = new Foo(); $result->bar = new Bar(); $result->bar->boo = 'tomato'; $this->assertEquals($result, $u->getUnserializedData()); } /** * Test unserializing with a setter method */ public function testSetterMethod() { $u = new XML_Unserializer(); $u->setOption(XML_UNSERIALIZER_OPTION_COMPLEXTYPE, 'object'); $u->setOption(XML_UNSERIALIZER_OPTION_DEFAULT_CLASS, 'Foo'); $xml = 'tomato'; $u->unserialize($xml); $result = new SetterExample(); $result->setFoo('tomato'); $this->assertEquals($result, $u->getUnserializedData()); } } class Foo {} class Bar {} class SetterExample { private $_hidden = null; public function setFoo($foo) { $this->_hidden = $foo; } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Unserializer_Objects_TestCase::main') { XML_Unserializer_Objects_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Unserializer_Option_Encodings_TestCase.php0000600000175000017500000000357311461374326026662 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Unserializer_Option_Encodings_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Unserializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Unserializer_Option_Encodings_TestCase extends PHPUnit_Framework_TestCase { public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Unserializer_Option_Encodings_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Test unserializing from UTF-8 to ISO-8859-1 */ public function testUtf8ToIso() { $u = new XML_Unserializer(); $u->setOption(XML_UNSERIALIZER_OPTION_ENCODING_SOURCE, 'UTF-8'); $u->setOption(XML_UNSERIALIZER_OPTION_ENCODING_TARGET, 'ISO-8859-1'); $xml = ''.utf8_encode('A string containing ü ä Ãê').''; $u->unserialize($xml); $this->assertEquals('A string containing ü ä Ãê', $u->getUnserializedData()); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Unserializer_Option_Encodings_TestCase::main') { XML_Unserializer_Option_Encodings_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Unserializer_Option_GuessTypes_TestCase.php0000600000175000017500000000533711461374326027064 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Unserializer_Option_GuessTypes_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Unserializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Unserializer_Option_GuessTypes_TestCase extends PHPUnit_Framework_TestCase { public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Unserializer_Option_GuessTypes_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Test unserializing a boolean */ public function testBoolean() { $u = new XML_Unserializer(); $u->setOption(XML_UNSERIALIZER_OPTION_GUESS_TYPES, true); $xml = 'true'; $u->unserialize($xml); $this->assertEquals(true, $u->getUnserializedData()); $xml = 'false'; $u->unserialize($xml); $this->assertEquals(false, $u->getUnserializedData()); } /** * Test unserializing an integer */ public function testInteger() { $u = new XML_Unserializer(); $u->setOption(XML_UNSERIALIZER_OPTION_GUESS_TYPES, true); $xml = '453'; $u->unserialize($xml); $this->assertEquals(453, $u->getUnserializedData()); $xml = '-1'; $u->unserialize($xml); $this->assertEquals(-1, $u->getUnserializedData()); } /** * Test unserializing a float */ public function testFloat() { $u = new XML_Unserializer(); $u->setOption(XML_UNSERIALIZER_OPTION_GUESS_TYPES, true); $xml = '453.54553'; $u->unserialize($xml); $this->assertEquals(453.54553, $u->getUnserializedData()); $xml = '-1.47'; $u->unserialize($xml); $this->assertEquals(-1.47, $u->getUnserializedData()); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Unserializer_Option_GuessTypes_TestCase::main') { XML_Unserializer_Option_GuessTypes_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Unserializer_Option_Whitespace_TestCase.php0000600000175000017500000000534511461374326027044 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Unserializer_Option_Whitespace_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Unserializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Unserializer_Option_Whitespace_TestCase extends PHPUnit_Framework_TestCase { private $xml = ' This XML document contains line breaks. '; public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Unserializer_Option_Whitespace_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Test trim behaviour */ public function testTrim() { $u = new XML_Unserializer(); $u->setOption(XML_UNSERIALIZER_OPTION_WHITESPACE, XML_UNSERIALIZER_WHITESPACE_TRIM); $u->unserialize($this->xml); $expected = array('string' => 'This XML document contains line breaks.'); $this->assertEquals($expected, $u->getUnserializedData()); } /** * Test normalize behaviour */ public function testNormalize() { $u = new XML_Unserializer(); $u->setOption(XML_UNSERIALIZER_OPTION_WHITESPACE, XML_UNSERIALIZER_WHITESPACE_NORMALIZE); $u->unserialize($this->xml); $expected = array('string' => 'This XML document contains line breaks.'); $this->assertEquals($expected, $u->getUnserializedData()); } /** * Test keep behaviour */ public function testKeep() { $u = new XML_Unserializer(); $u->setOption(XML_UNSERIALIZER_OPTION_WHITESPACE, XML_UNSERIALIZER_WHITESPACE_KEEP); $u->unserialize($this->xml); $expected = array('string' => ' This XML document contains line breaks. '); $this->assertEquals($expected, $u->getUnserializedData()); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Unserializer_Option_Whitespace_TestCase::main') { XML_Unserializer_Option_Whitespace_TestCase::main(); } ?> XML_Serializer-0.20.2/tests/Unserializer_Scalars_TestCase.php0000600000175000017500000000353511461374326025007 0ustar clockwerxclockwerx * @author Chuck Burgess */ /** * PHPUnit main() hack * * "Call class::main() if this source file is executed directly." */ if (!defined('PHPUnit_MAIN_METHOD')) { define('PHPUnit_MAIN_METHOD', 'XML_Unserializer_Scalars_TestCase::main'); } require_once 'PHPUnit/Framework/TestCase.php'; require_once 'PHPUnit/Framework/TestSuite.php'; require_once 'PHPUnit/TextUI/TestRunner.php'; require_once 'XML/Unserializer.php'; /** * Unit Tests for serializing arrays * * @package XML_Serializer * @subpackage tests * @author Stephan Schmidt * @author Chuck Burgess */ class XML_Unserializer_Scalars_TestCase extends PHPUnit_Framework_TestCase { public static function main() { $suite = new PHPUnit_Framework_TestSuite('XML_Unserializer_Scalars_TestCase'); $result = PHPUnit_TextUI_TestRunner::run($suite); } protected function setUp() {} protected function tearDown() {} /** * Test unserializing simple data */ public function testData() { $u = new XML_Unserializer(); $xml = 'data'; $u->unserialize($xml); $this->assertEquals('data', $u->getUnserializedData()); } /** * Test extracting the root name */ public function testRootName() { $u = new XML_Unserializer(); $xml = 'data'; $u->unserialize($xml); $this->assertEquals('xml', $u->getRootName()); } } /** * PHPUnit main() hack * "Call class::main() if this source file is executed directly." */ if (PHPUnit_MAIN_METHOD == 'XML_Unserializer_Scalars_TestCase::main') { XML_Unserializer_Scalars_TestCase::main(); } ?> XML_Serializer-0.20.2/XML/Serializer.php0000600000175000017500000011760411461374326020502 0ustar clockwerxclockwerx * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @category XML * @package XML_Serializer * @author Stephan Schmidt * @copyright 2003-2008 Stephan Schmidt * @license http://opensource.org/licenses/bsd-license New BSD License * @version CVS: $Id: Serializer.php 294967 2010-02-12 03:10:19Z clockwerx $ * @link http://pear.php.net/package/XML_Serializer * @see XML_Unserializer */ /** * uses PEAR error management */ require_once 'PEAR.php'; /** * uses XML_Util to create XML tags */ require_once 'XML/Util.php'; /** * option: string used for indentation * * Possible values: * - any string (default is any string) */ define('XML_SERIALIZER_OPTION_INDENT', 'indent'); /** * option: string used for linebreaks * * Possible values: * - any string (default is \n) */ define('XML_SERIALIZER_OPTION_LINEBREAKS', 'linebreak'); /** * option: enable type hints * * Possible values: * - true * - false */ define('XML_SERIALIZER_OPTION_TYPEHINTS', 'typeHints'); /** * option: add an XML declaration * * Possible values: * - true * - false */ define('XML_SERIALIZER_OPTION_XML_DECL_ENABLED', 'addDecl'); /** * option: encoding of the document * * Possible values: * - any valid encoding * - null (default) */ define('XML_SERIALIZER_OPTION_XML_ENCODING', 'encoding'); /** * option: default name for tags * * Possible values: * - any string (XML_Serializer_Tag is default) */ define('XML_SERIALIZER_OPTION_DEFAULT_TAG', 'defaultTagName'); /** * option: use classname for objects in indexed arrays * * Possible values: * - true * - false (default) */ define('XML_SERIALIZER_OPTION_CLASSNAME_AS_TAGNAME', 'classAsTagName'); /** * option: attribute where original key is stored * * Possible values: * - any string (default is _originalKey) */ define('XML_SERIALIZER_OPTION_ATTRIBUTE_KEY', 'keyAttribute'); /** * option: attribute for type (only if typeHints => true) * * Possible values: * - any string (default is _type) */ define('XML_SERIALIZER_OPTION_ATTRIBUTE_TYPE', 'typeAttribute'); /** * option: attribute for class (only if typeHints => true) * * Possible values: * - any string (default is _class) */ define('XML_SERIALIZER_OPTION_ATTRIBUTE_CLASS', 'classAttribute'); /** * option: scalar values (strings, ints,..) will be serialized as attribute * * Possible values: * - true * - false (default) * - array which sets this option on a per-tag basis */ define('XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES', 'scalarAsAttributes'); /** * option: prepend string for attributes * * Possible values: * - any string (default is any string) */ define('XML_SERIALIZER_OPTION_PREPEND_ATTRIBUTES', 'prependAttributes'); /** * option: indent the attributes, if set to '_auto', * it will indent attributes so they all start at the same column * * Possible values: * - true * - false (default) * - '_auto' */ define('XML_SERIALIZER_OPTION_INDENT_ATTRIBUTES', 'indentAttributes'); /** * option: use 'simplexml' to use parent name as tagname * if transforming an indexed array * * Possible values: * - XML_SERIALIZER_MODE_DEFAULT (default) * - XML_SERIALIZER_MODE_SIMPLEXML */ define('XML_SERIALIZER_OPTION_MODE', 'mode'); /** * option: add a doctype declaration * * Possible values: * - true * - false (default) */ define('XML_SERIALIZER_OPTION_DOCTYPE_ENABLED', 'addDoctype'); /** * option: supply a string or an array with id and uri * ({@see XML_Util::getDoctypeDeclaration()} * * Possible values: * - string * - array */ define('XML_SERIALIZER_OPTION_DOCTYPE', 'doctype'); /** * option: name of the root tag * * Possible values: * - string * - null (default) */ define('XML_SERIALIZER_OPTION_ROOT_NAME', 'rootName'); /** * option: attributes of the root tag * * Possible values: * - array */ define('XML_SERIALIZER_OPTION_ROOT_ATTRIBS', 'rootAttributes'); /** * option: all values in this key will be treated as attributes * * Possible values: * - string */ define('XML_SERIALIZER_OPTION_ATTRIBUTES_KEY', 'attributesArray'); /** * option: this value will be used directly as content, * instead of creating a new tag, may only be used * in conjuction with attributesArray * * Possible values: * - string * - null (default) */ 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: * - array */ define('XML_SERIALIZER_OPTION_TAGMAP', 'tagMap'); /** * option: function that will be applied before serializing * * Possible values: * - any valid PHP callback */ define('XML_SERIALIZER_OPTION_ENCODE_FUNC', 'encodeFunction'); /** * option: namespace to use for the document * * Possible values: * - string * - null (default) */ define('XML_SERIALIZER_OPTION_NAMESPACE', 'namespace'); /** * option: type of entities to replace * * Possible values: * - XML_SERIALIZER_ENTITIES_NONE * - XML_SERIALIZER_ENTITIES_XML (default) * - XML_SERIALIZER_ENTITIES_XML_REQUIRED * - XML_SERIALIZER_ENTITIES_HTML */ define('XML_SERIALIZER_OPTION_ENTITIES', 'replaceEntities'); /** * option: whether to return the result of the serialization from serialize() * * Possible values: * - true * - false (default) */ define('XML_SERIALIZER_OPTION_RETURN_RESULT', 'returnResult'); /** * option: whether to ignore properties that are set to null * * Possible values: * - true * - false (default) */ define('XML_SERIALIZER_OPTION_IGNORE_NULL', 'ignoreNull'); /** * option: whether to use cdata sections for character data * * Possible values: * - true * - false (default) */ define('XML_SERIALIZER_OPTION_CDATA_SECTIONS', 'cdata'); /** * option: whether a boolean FALSE value should become a string * * Possible values: * - true * - false (default) * * @since 0.20.0 */ define('XML_SERIALIZER_OPTION_FALSE_AS_STRING', 'falseAsString'); /** * default mode */ define('XML_SERIALIZER_MODE_DEFAULT', 'default'); /** * SimpleXML mode * * When serializing indexed arrays, the key of the parent value is used as a tagname. */ define('XML_SERIALIZER_MODE_SIMPLEXML', 'simplexml'); /** * error code for no serialization done */ define('XML_SERIALIZER_ERROR_NO_SERIALIZATION', 51); /** * do not replace entitites */ define('XML_SERIALIZER_ENTITIES_NONE', XML_UTIL_ENTITIES_NONE); /** * replace all XML entitites * This setting will replace <, >, ", ' and & */ define('XML_SERIALIZER_ENTITIES_XML', XML_UTIL_ENTITIES_XML); /** * replace only required XML entitites * This setting will replace <, " and & */ define('XML_SERIALIZER_ENTITIES_XML_REQUIRED', XML_UTIL_ENTITIES_XML_REQUIRED); /** * replace HTML entitites * @link http://www.php.net/htmlentities */ define('XML_SERIALIZER_ENTITIES_HTML', XML_UTIL_ENTITIES_HTML); /** * Creates XML documents from PHP data structures like arrays, objects or scalars. * * This class can be used in two modes: * * 1. Create an XML document from an array or object that is processed by other * applications. That means you can create an RDF document from an array in the * following format: * * $data = array( * 'channel' => array( * 'title' => 'Example RDF channel', * 'link' => 'http://www.php-tools.de', * 'image' => array( * 'title' => 'Example image', * 'url' => 'http://www.php-tools.de/image.gif', * 'link' => 'http://www.php-tools.de' * ), * array( * 'title' => 'Example item', * 'link' => 'http://example.com' * ), * array( * 'title' => 'Another Example item', * 'link' => 'http://example.org' * ) * ) * ); * * * To create an RDF document from this array, do the following: * * * require_once 'XML/Serializer.php'; * $options = array( * XML_SERIALIZER_OPTION_INDENT => "\t", // indent with tabs * XML_SERIALIZER_OPTION_LINEBREAKS => "\n", // use UNIX line breaks * XML_SERIALIZER_OPTION_ROOT_NAME => 'rdf:RDF',// root tag * XML_SERIALIZER_OPTION_DEFAULT_TAG => 'item' // tag for values * // with numeric keys * ); * $serializer = new XML_Serializer($options); * $rdf = $serializer->serialize($data); * * * You will get a complete XML document that can be processed like any RDF document. * * 2. This class can be used to serialize any data structure in a way that it can * later be unserialized again. XML_Serializer will store the type of the value * and additional meta information in attributes of the surrounding tag. This * meta information can later be used to restore the original data structure * in PHP. If you want XML_Serializer to add meta information to the tags, add * * XML_SERIALIZER_OPTION_TYPEHINTS => true * * to the options array in the constructor. * * @category XML * @package XML_Serializer * @author Stephan Schmidt * @copyright 2003-2008 Stephan Schmidt * @license http://opensource.org/licenses/bsd-license New BSD License * @version Release: @package_version@ * @link http://pear.php.net/package/XML_Serializer * @see XML_Unserializer * @todo replace extending PEAR class with instead using a PEAR_Error object */ class XML_Serializer extends PEAR { /** * List of all available options * * @access private * @var array */ var $_knownOptions = array( XML_SERIALIZER_OPTION_INDENT, XML_SERIALIZER_OPTION_LINEBREAKS, XML_SERIALIZER_OPTION_TYPEHINTS, XML_SERIALIZER_OPTION_XML_DECL_ENABLED, XML_SERIALIZER_OPTION_XML_ENCODING, XML_SERIALIZER_OPTION_DEFAULT_TAG, XML_SERIALIZER_OPTION_CLASSNAME_AS_TAGNAME, XML_SERIALIZER_OPTION_ATTRIBUTE_KEY, XML_SERIALIZER_OPTION_ATTRIBUTE_TYPE, XML_SERIALIZER_OPTION_ATTRIBUTE_CLASS, XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES, XML_SERIALIZER_OPTION_PREPEND_ATTRIBUTES, XML_SERIALIZER_OPTION_INDENT_ATTRIBUTES, XML_SERIALIZER_OPTION_MODE, XML_SERIALIZER_OPTION_DOCTYPE_ENABLED, XML_SERIALIZER_OPTION_DOCTYPE, XML_SERIALIZER_OPTION_ROOT_NAME, 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, XML_SERIALIZER_OPTION_ENTITIES, XML_SERIALIZER_OPTION_RETURN_RESULT, XML_SERIALIZER_OPTION_IGNORE_NULL, XML_SERIALIZER_OPTION_CDATA_SECTIONS, ); /** * Default options for the serialization * * @access private * @var array */ var $_defaultOptions = array( // string used for indentation XML_SERIALIZER_OPTION_INDENT => '', // string used for newlines XML_SERIALIZER_OPTION_LINEBREAKS => "\n", // automatically add type hin attributes XML_SERIALIZER_OPTION_TYPEHINTS => false, // add an XML declaration XML_SERIALIZER_OPTION_XML_DECL_ENABLED => false, // encoding specified in the XML declaration XML_SERIALIZER_OPTION_XML_ENCODING => null, // tag used for indexed arrays or invalid names XML_SERIALIZER_OPTION_DEFAULT_TAG => 'XML_Serializer_Tag', // use classname for objects in indexed arrays XML_SERIALIZER_OPTION_CLASSNAME_AS_TAGNAME => false, // attribute where original key is stored XML_SERIALIZER_OPTION_ATTRIBUTE_KEY => '_originalKey', // attribute for type (only if typeHints => true) XML_SERIALIZER_OPTION_ATTRIBUTE_TYPE => '_type', // attribute for class of objects (only if typeHints => true) XML_SERIALIZER_OPTION_ATTRIBUTE_CLASS => '_class', // scalar values (strings, ints,..) will be serialized as attribute XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES => false, // prepend string for attributes XML_SERIALIZER_OPTION_PREPEND_ATTRIBUTES => '', // indent the attributes, if set to '_auto', // it will indent attributes so they all start at the same column XML_SERIALIZER_OPTION_INDENT_ATTRIBUTES => false, // use XML_SERIALIZER_MODE_SIMPLEXML to use parent name as tagname // if transforming an indexed array XML_SERIALIZER_OPTION_MODE => XML_SERIALIZER_MODE_DEFAULT, // add a doctype declaration XML_SERIALIZER_OPTION_DOCTYPE_ENABLED => false, // supply a string or an array with id and uri // ({@see XML_Util::getDoctypeDeclaration()} XML_SERIALIZER_OPTION_DOCTYPE => null, // name of the root tag XML_SERIALIZER_OPTION_ROOT_NAME => null, // attributes of the root tag XML_SERIALIZER_OPTION_ROOT_ATTRIBS => array(), // all values in this key will be treated as attributes XML_SERIALIZER_OPTION_ATTRIBUTES_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_CONTENT_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_COMMENT_KEY => null, // tag names that will be changed XML_SERIALIZER_OPTION_TAGMAP => array(), // function that will be applied before serializing XML_SERIALIZER_OPTION_ENCODE_FUNC => null, // namespace to use XML_SERIALIZER_OPTION_NAMESPACE => null, // type of entities to replace, XML_SERIALIZER_OPTION_ENTITIES => XML_SERIALIZER_ENTITIES_XML, // serialize() returns the result of the serialization instead of true XML_SERIALIZER_OPTION_RETURN_RESULT => false, // ignore properties that are set to null XML_SERIALIZER_OPTION_IGNORE_NULL => false, // Whether to use cdata sections for plain character data XML_SERIALIZER_OPTION_CDATA_SECTIONS => false, // Whether to convert a boolean FALSE into a string XML_SERIALIZER_OPTION_FALSE_AS_STRING => false, ); /** * Options for the serialization * * @access public * @var array */ var $options = array(); /** * Current tag depth * * @access private * @var integer */ var $_tagDepth = 0; /** * Serialized representation of the data * * @access private * @var string */ var $_serializedData = null; /** * Constructor * * @param mixed $options array containing options for the serialization * * @access public */ function XML_Serializer( $options = null ) { $this->PEAR(); if (is_array($options)) { $this->options = array_merge($this->_defaultOptions, $options); } else { $this->options = $this->_defaultOptions; } } /** * Return the package version number * * @access public * @static * @return string the version number of XML_Serializer */ function apiVersion() { return '@package_version@'; } /** * Reset all options to default options * * @return void * @access public */ function resetOptions() { $this->options = $this->_defaultOptions; } /** * Set an option * * You can use this method if you do not want * to set all options in the constructor. * * @param string $name option name * @param mixed $value option value * * @return void * @access public */ function setOption($name, $value) { $this->options[$name] = $value; } /** * Sets several options at once * * You can use this method if you do not want * to set all options in the constructor. * * @param array $options options array * * @return void * @access public */ function setOptions($options) { $this->options = array_merge($this->options, $options); } /** * 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; } /** * get the result of the serialization * * @access public * @return string serialized XML */ function getSerializedData() { if ($this->_serializedData == null) { return $this->raiseError('No serialized data available. ' . 'Use XML_Serializer::serialize() first.', XML_SERIALIZER_ERROR_NO_SERIALIZATION); } return $this->_serializedData; } /** * serialize any value * * This method checks for the type of the value and calls the appropriate method * * @param mixed $value tag value * @param string $tagName tag name * @param array $attributes attributes * * @return string * @access private */ 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 * * @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 * @access private * @uses XML_Util::isValidName() to check, whether key has to be substituted * @uses XML_Util::replaceEntities() * @uses XML_Util::createComment() * @uses PEAR::popExpect() * @uses PEAR::expectError() */ 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 */ if ($this->options[XML_SERIALIZER_OPTION_ATTRIBUTES_KEY] !== null) { if (isset($array[$this->options[XML_SERIALIZER_OPTION_ATTRIBUTES_KEY]]) ) { $attributes = $array[$this->options[XML_SERIALIZER_OPTION_ATTRIBUTES_KEY]]; unset($array[$this->options[XML_SERIALIZER_OPTION_ATTRIBUTES_KEY]]); } /** * check for special content */ if ($this->options[XML_SERIALIZER_OPTION_CONTENT_KEY] !== null) { if (isset($array[$this->options[XML_SERIALIZER_OPTION_CONTENT_KEY]]) ) { $_content = XML_Util::replaceEntities($array [$this->options[XML_SERIALIZER_OPTION_CONTENT_KEY]]); unset($array[$this->options[XML_SERIALIZER_OPTION_CONTENT_KEY]]); } } } if ($this->options[XML_SERIALIZER_OPTION_IGNORE_NULL] === true) { foreach (array_keys($array) as $key) { if (is_null($array[$key])) { unset($array[$key]); } } } /* * if mode is set to simpleXML, check whether * the array is associative or indexed */ if (is_array($array) && !empty($array) && $this->options[XML_SERIALIZER_OPTION_MODE] == XML_SERIALIZER_MODE_SIMPLEXML ) { $indexed = true; foreach ($array as $key => $val) { if (!is_int($key)) { $indexed = false; break; } } if ($indexed && $this->options[XML_SERIALIZER_OPTION_MODE] == XML_SERIALIZER_MODE_SIMPLEXML ) { $string = ''; foreach ($array as $key => $val) { $string .= $this->_serializeValue($val, $tagName, $attributes); $string .= $this->options[XML_SERIALIZER_OPTION_LINEBREAKS]; // do indentation if ($this->options[XML_SERIALIZER_OPTION_INDENT]!==null && $this->_tagDepth>0 ) { $string .= str_repeat($this->options[XML_SERIALIZER_OPTION_INDENT], $this->_tagDepth); } } return rtrim($string); } } $scalarAsAttributes = false; if (is_array($this->options[XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES]) && isset($this->options[XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES] [$tagName]) ) { $scalarAsAttributes = $this->options[XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES][$tagName]; } elseif ($this->options[XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES] === true ) { $scalarAsAttributes = true; } if ($scalarAsAttributes === true) { $this->expectError('*'); foreach ($array as $key => $value) { if (is_scalar($value) && (XML_Util::isValidName($key) === true)) { unset($array[$key]); $attributes[$this->options [XML_SERIALIZER_OPTION_PREPEND_ATTRIBUTES].$key] = $value; } } $this->popExpect(); } elseif (is_array($scalarAsAttributes)) { $this->expectError('*'); foreach ($scalarAsAttributes as $key) { if (!isset($array[$key])) { continue; } $value = $array[$key]; if (is_scalar($value) && (XML_Util::isValidName($key) === true)) { unset($array[$key]); $attributes[$this->options [XML_SERIALIZER_OPTION_PREPEND_ATTRIBUTES].$key] = $value; } } $this->popExpect(); } // check for empty array => create empty tag if (empty($array)) { $tag = array( 'qname' => $tagName, 'content' => $_content, 'attributes' => $attributes ); } else { $this->_tagDepth++; $tmp = $_content . $this->options[XML_SERIALIZER_OPTION_LINEBREAKS]; foreach ($array as $key => $value) { // do indentation if ($this->options[XML_SERIALIZER_OPTION_INDENT]!==null && $this->_tagDepth>0 ) { $tmp .= str_repeat($this->options[XML_SERIALIZER_OPTION_INDENT], $this->_tagDepth); } // copy key $origKey = $key; $this->expectError('*'); // key cannot be used as tagname => use default tag $valid = XML_Util::isValidName($key); $this->popExpect(); if (PEAR::isError($valid)) { if ($this->options[XML_SERIALIZER_OPTION_CLASSNAME_AS_TAGNAME] && is_object($value) ) { $key = get_class($value); } else { $key = $this->_getDefaultTagname($tagName); } } // once we've established the true $key, is there a tagmap for it? if (isset($this->options[XML_SERIALIZER_OPTION_TAGMAP][$key])) { $key = $this->options[XML_SERIALIZER_OPTION_TAGMAP][$key]; } $atts = array(); if ($this->options[XML_SERIALIZER_OPTION_TYPEHINTS] === true) { $atts[$this->options[XML_SERIALIZER_OPTION_ATTRIBUTE_TYPE]] = gettype($value); if ($key !== $origKey) { $atts[$this->options[XML_SERIALIZER_OPTION_ATTRIBUTE_KEY]] = (string)$origKey; } } $tmp .= $this->_createXMLTag(array( 'qname' => $key, 'attributes' => $atts, 'content' => $value )); $tmp .= $this->options[XML_SERIALIZER_OPTION_LINEBREAKS]; } $this->_tagDepth--; if ($this->options[XML_SERIALIZER_OPTION_INDENT]!==null && $this->_tagDepth>0 ) { $tmp .= str_repeat($this->options[XML_SERIALIZER_OPTION_INDENT], $this->_tagDepth); } if (trim($tmp) === '') { $tmp = null; } $tag = array( 'qname' => $tagName, 'content' => $tmp, 'attributes' => $attributes ); } if ($this->options[XML_SERIALIZER_OPTION_TYPEHINTS] === true) { if (!isset($tag['attributes'] [$this->options[XML_SERIALIZER_OPTION_ATTRIBUTE_TYPE]]) ) { $tag['attributes'] [$this->options[XML_SERIALIZER_OPTION_ATTRIBUTE_TYPE]] = 'array'; } } $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; } /** * get the name of the default tag. * * The name of the parent tag needs to be passed as the * default name can depend on the context. * * @param string $parent name of the parent tag * * @return string default tag name */ function _getDefaultTagname($parent) { if (is_string($this->options[XML_SERIALIZER_OPTION_DEFAULT_TAG])) { return $this->options[XML_SERIALIZER_OPTION_DEFAULT_TAG]; } if (isset($this->options[XML_SERIALIZER_OPTION_DEFAULT_TAG][$parent])) { return $this->options[XML_SERIALIZER_OPTION_DEFAULT_TAG][$parent]; } elseif (isset($this->options[XML_SERIALIZER_OPTION_DEFAULT_TAG] ['#default']) ) { return $this->options[XML_SERIALIZER_OPTION_DEFAULT_TAG]['#default']; } elseif (isset($this->options[XML_SERIALIZER_OPTION_DEFAULT_TAG] ['__default']) ) { // keep this for BC return $this->options[XML_SERIALIZER_OPTION_DEFAULT_TAG]['__default']; } return 'XML_Serializer_Tag'; } /** * serialize an object * * @param object &$object object to serialize * @param string $tagName tag name * @param array $attributes attributes * * @return string $string serialized data * @access private */ function _serializeObject(&$object, $tagName = null, $attributes = array()) { // check for magic function if (method_exists($object, '__sleep')) { $propNames = $object->__sleep(); if (is_array($propNames)) { $properties = array(); foreach ($propNames as $propName) { $properties[$propName] = $object->$propName; } } else { $properties = get_object_vars($object); } } else { $properties = get_object_vars($object); } if (empty($tagName)) { $tagName = get_class($object); } // typehints activated? if ($this->options[XML_SERIALIZER_OPTION_TYPEHINTS] === true) { $attributes[$this->options[XML_SERIALIZER_OPTION_ATTRIBUTE_TYPE]] = 'object'; $attributes[$this->options[XML_SERIALIZER_OPTION_ATTRIBUTE_CLASS]] = get_class($object); } $string = $this->_serializeArray($properties, $tagName, $attributes); return $string; } /** * create a tag from an array * this method awaits an array in the following format * array( * 'qname' => $tagName, * 'attributes' => array(), * 'content' => $content, // optional * 'namespace' => $namespace // optional * 'namespaceUri' => $namespaceUri // optional * ) * * @param array $tag tag definition * @param boolean $firstCall whether or not this is the first call * * @return string $string XML tag * @access private */ 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']; } else { $tag['qname'] = $this->options[XML_SERIALIZER_OPTION_NAMESPACE] . ':' . $tag['qname']; } } // attribute indentation if ($this->options[XML_SERIALIZER_OPTION_INDENT_ATTRIBUTES] !== false) { $multiline = true; $indent = str_repeat($this->options[XML_SERIALIZER_OPTION_INDENT], $this->_tagDepth); if ($this->options[XML_SERIALIZER_OPTION_INDENT_ATTRIBUTES] == '_auto') { $indent .= str_repeat(' ', (strlen($tag['qname'])+2)); } else { $indent .= $this->options[XML_SERIALIZER_OPTION_INDENT_ATTRIBUTES]; } } else { $multiline = false; $indent = false; } if (is_array($tag['content'])) { if (empty($tag['content'])) { $tag['content'] = ''; } } elseif (XML_SERIALIZER_OPTION_FALSE_AS_STRING && $tag['content'] === false) { $tag['content'] = '0'; } elseif (is_scalar($tag['content']) && (string)$tag['content'] == '') { $tag['content'] = ''; } // replace XML entities if ($firstCall === true) { if ($this->options[XML_SERIALIZER_OPTION_CDATA_SECTIONS] === true) { $replaceEntities = XML_UTIL_CDATA_SECTION; } else { $replaceEntities = $this->options[XML_SERIALIZER_OPTION_ENTITIES]; } } else { // this is a nested call, so value is already encoded // and must not be encoded again $replaceEntities = XML_SERIALIZER_ENTITIES_NONE; // but attributes need to be encoded anyways // (done here because the rest of the code assumes the same encoding // can be used both for attributes and content) foreach ($tag['attributes'] as $k => $v) { $v = XML_Util::replaceEntities($v, $this->options[XML_SERIALIZER_OPTION_ENTITIES]); $tag['attributes'][$k] = $v; } } if (is_scalar($tag['content']) || is_null($tag['content'])) { if ($this->options[XML_SERIALIZER_OPTION_ENCODE_FUNC]) { if ($firstCall === true) { $tag['content'] = call_user_func($this-> options[XML_SERIALIZER_OPTION_ENCODE_FUNC], $tag['content']); } $tag['attributes'] = array_map($this-> options[XML_SERIALIZER_OPTION_ENCODE_FUNC], $tag['attributes']); } $tag = XML_Util::createTagFromArray($tag, $replaceEntities, $multiline, $indent, $this->options[XML_SERIALIZER_OPTION_LINEBREAKS]); } elseif (is_array($tag['content'])) { $tag = $this->_serializeArray($tag['content'], $tag['qname'], $tag['attributes']); } elseif (is_object($tag['content'])) { $tag = $this->_serializeObject($tag['content'], $tag['qname'], $tag['attributes']); } elseif (is_resource($tag['content'])) { settype($tag['content'], 'string'); if ($this->options[XML_SERIALIZER_OPTION_ENCODE_FUNC]) { if ($replaceEntities === true) { $tag['content'] = call_user_func($this-> options[XML_SERIALIZER_OPTION_ENCODE_FUNC], $tag['content']); } $tag['attributes'] = array_map($this-> options[XML_SERIALIZER_OPTION_ENCODE_FUNC], $tag['attributes']); } $tag = XML_Util::createTagFromArray($tag, $replaceEntities); } return $tag; } } ?> XML_Serializer-0.20.2/XML/Unserializer.php0000600000175000017500000007244511461374326021050 0ustar clockwerxclockwerx * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * @category XML * @package XML_Serializer * @author Stephan Schmidt * @copyright 2003-2008 Stephan Schmidt * @license http://opensource.org/licenses/bsd-license New BSD License * @version CVS: $Id: Unserializer.php 303099 2010-09-06 16:23:06Z clockwerx $ * @link http://pear.php.net/package/XML_Serializer * @see XML_Unserializer */ /** * uses PEAR error managemt */ require_once 'PEAR.php'; /** * uses XML_Parser to unserialize document */ require_once 'XML/Parser.php'; /** * option: Convert nested tags to array or object * * Possible values: * - array * - object * - associative array to define this option per tag name */ define('XML_UNSERIALIZER_OPTION_COMPLEXTYPE', 'complexType'); /** * option: Name of the attribute that stores the original key * * Possible values: * - any string */ define('XML_UNSERIALIZER_OPTION_ATTRIBUTE_KEY', 'keyAttribute'); /** * option: Name of the attribute that stores the type * * Possible values: * - any string */ define('XML_UNSERIALIZER_OPTION_ATTRIBUTE_TYPE', 'typeAttribute'); /** * option: Name of the attribute that stores the class name * * Possible values: * - any string */ define('XML_UNSERIALIZER_OPTION_ATTRIBUTE_CLASS', 'classAttribute'); /** * option: Whether to use the tag name as a class name * * Possible values: * - true or false */ define('XML_UNSERIALIZER_OPTION_TAG_AS_CLASSNAME', 'tagAsClass'); /** * option: Name of the default class * * Possible values: * - any string */ define('XML_UNSERIALIZER_OPTION_DEFAULT_CLASS', 'defaultClass'); /** * option: Whether to parse attributes * * Possible values: * - true or false */ define('XML_UNSERIALIZER_OPTION_ATTRIBUTES_PARSE', 'parseAttributes'); /** * option: Key of the array to store attributes (if any) * * Possible values: * - any string * - false (disabled) */ define('XML_UNSERIALIZER_OPTION_ATTRIBUTES_ARRAYKEY', 'attributesArray'); /** * option: string to prepend attribute name (if any) * * Possible values: * - any string * - false (disabled) */ define('XML_UNSERIALIZER_OPTION_ATTRIBUTES_PREPEND', 'prependAttributes'); /** * option: key to store the content, * if XML_UNSERIALIZER_OPTION_ATTRIBUTES_PARSE is used * * Possible values: * - any string */ define('XML_UNSERIALIZER_OPTION_CONTENT_KEY', 'contentName'); /** * option: map tag names * * Possible values: * - associative array */ define('XML_UNSERIALIZER_OPTION_TAG_MAP', 'tagMap'); /** * option: list of tags that will always be enumerated * * Possible values: * - indexed array */ define('XML_UNSERIALIZER_OPTION_FORCE_ENUM', 'forceEnum'); /** * option: Encoding of the XML document * * Possible values: * - UTF-8 * - ISO-8859-1 */ define('XML_UNSERIALIZER_OPTION_ENCODING_SOURCE', 'encoding'); /** * option: Desired target encoding of the data * * Possible values: * - UTF-8 * - ISO-8859-1 */ define('XML_UNSERIALIZER_OPTION_ENCODING_TARGET', 'targetEncoding'); /** * option: Callback that will be applied to textual data * * Possible values: * - any valid PHP callback */ define('XML_UNSERIALIZER_OPTION_DECODE_FUNC', 'decodeFunction'); /** * option: whether to return the result of the unserialization from unserialize() * * Possible values: * - true * - false (default) */ define('XML_UNSERIALIZER_OPTION_RETURN_RESULT', 'returnResult'); /** * option: set the whitespace behaviour * * Possible values: * - XML_UNSERIALIZER_WHITESPACE_KEEP * - XML_UNSERIALIZER_WHITESPACE_TRIM * - XML_UNSERIALIZER_WHITESPACE_NORMALIZE */ define('XML_UNSERIALIZER_OPTION_WHITESPACE', 'whitespace'); /** * Keep all whitespace */ define('XML_UNSERIALIZER_WHITESPACE_KEEP', 'keep'); /** * remove whitespace from start and end of the data */ define('XML_UNSERIALIZER_WHITESPACE_TRIM', 'trim'); /** * normalize whitespace */ define('XML_UNSERIALIZER_WHITESPACE_NORMALIZE', 'normalize'); /** * option: whether to ovverride all options that have been set before * * Possible values: * - true * - false (default) */ define('XML_UNSERIALIZER_OPTION_OVERRIDE_OPTIONS', 'overrideOptions'); /** * option: list of tags, that will not be used as keys */ define('XML_UNSERIALIZER_OPTION_IGNORE_KEYS', 'ignoreKeys'); /** * option: whether to use type guessing for scalar values */ define('XML_UNSERIALIZER_OPTION_GUESS_TYPES', 'guessTypes'); /** * error code for no serialization done */ define('XML_UNSERIALIZER_ERROR_NO_UNSERIALIZATION', 151); /** * XML_Unserializer * * class to unserialize XML documents that have been created with * XML_Serializer. To unserialize an XML document you have to add * type hints to the XML_Serializer options. * * If no type hints are available, XML_Unserializer will guess how * the tags should be treated, that means complex structures will be * arrays and tags with only CData in them will be strings. * * * require_once 'XML/Unserializer.php'; * * // be careful to always use the ampersand in front of the new operator * $unserializer = &new XML_Unserializer(); * * $unserializer->unserialize($xml); * * $data = $unserializer->getUnserializedData(); * * * @category XML * @package XML_Serializer * @author Stephan Schmidt * @copyright 2003-2008 Stephan Schmidt * @license http://opensource.org/licenses/bsd-license New BSD License * @version Release: @package_version@ * @link http://pear.php.net/package/XML_Serializer * @see XML_Serializer */ class XML_Unserializer extends PEAR { /** * list of all available options * * @access private * @var array */ var $_knownOptions = array( XML_UNSERIALIZER_OPTION_COMPLEXTYPE, XML_UNSERIALIZER_OPTION_ATTRIBUTE_KEY, XML_UNSERIALIZER_OPTION_ATTRIBUTE_TYPE, XML_UNSERIALIZER_OPTION_ATTRIBUTE_CLASS, XML_UNSERIALIZER_OPTION_TAG_AS_CLASSNAME, XML_UNSERIALIZER_OPTION_DEFAULT_CLASS, XML_UNSERIALIZER_OPTION_ATTRIBUTES_PARSE, XML_UNSERIALIZER_OPTION_ATTRIBUTES_ARRAYKEY, XML_UNSERIALIZER_OPTION_ATTRIBUTES_PREPEND, XML_UNSERIALIZER_OPTION_CONTENT_KEY, XML_UNSERIALIZER_OPTION_TAG_MAP, XML_UNSERIALIZER_OPTION_FORCE_ENUM, XML_UNSERIALIZER_OPTION_ENCODING_SOURCE, XML_UNSERIALIZER_OPTION_ENCODING_TARGET, XML_UNSERIALIZER_OPTION_DECODE_FUNC, XML_UNSERIALIZER_OPTION_RETURN_RESULT, XML_UNSERIALIZER_OPTION_WHITESPACE, XML_UNSERIALIZER_OPTION_IGNORE_KEYS, XML_UNSERIALIZER_OPTION_GUESS_TYPES ); /** * default options for the serialization * * @access private * @var array */ var $_defaultOptions = array( // complex types will be converted to arrays, if no type hint is given XML_UNSERIALIZER_OPTION_COMPLEXTYPE => 'array', // get array key/property name from this attribute XML_UNSERIALIZER_OPTION_ATTRIBUTE_KEY => '_originalKey', // get type from this attribute XML_UNSERIALIZER_OPTION_ATTRIBUTE_TYPE => '_type', // get class from this attribute (if not given, use tag name) XML_UNSERIALIZER_OPTION_ATTRIBUTE_CLASS => '_class', // use the tagname as the classname XML_UNSERIALIZER_OPTION_TAG_AS_CLASSNAME => true, // name of the class that is used to create objects XML_UNSERIALIZER_OPTION_DEFAULT_CLASS => 'stdClass', // parse the attributes of the tag into an array XML_UNSERIALIZER_OPTION_ATTRIBUTES_PARSE => false, // parse them into sperate array (specify name of array here) XML_UNSERIALIZER_OPTION_ATTRIBUTES_ARRAYKEY => false, // prepend attribute names with this string XML_UNSERIALIZER_OPTION_ATTRIBUTES_PREPEND => '', // put cdata found in a tag that has been converted // to a complex type in this key XML_UNSERIALIZER_OPTION_CONTENT_KEY => '_content', // use this to map tagnames XML_UNSERIALIZER_OPTION_TAG_MAP => array(), // these tags will always be an indexed array XML_UNSERIALIZER_OPTION_FORCE_ENUM => array(), // specify the encoding character of the document to parse XML_UNSERIALIZER_OPTION_ENCODING_SOURCE => null, // specify the target encoding XML_UNSERIALIZER_OPTION_ENCODING_TARGET => null, // function used to decode data XML_UNSERIALIZER_OPTION_DECODE_FUNC => null, // unserialize() returns the result of the unserialization instead of true XML_UNSERIALIZER_OPTION_RETURN_RESULT => false, // remove whitespace around data XML_UNSERIALIZER_OPTION_WHITESPACE => XML_UNSERIALIZER_WHITESPACE_TRIM, // List of tags that will automatically be added to the parent, // instead of adding a new key XML_UNSERIALIZER_OPTION_IGNORE_KEYS => array(), // Whether to use type guessing XML_UNSERIALIZER_OPTION_GUESS_TYPES => false ); /** * current options for the serialization * * @access public * @var array */ var $options = array(); /** * unserialized data * * @access private * @var string */ var $_unserializedData = null; /** * name of the root tag * * @access private * @var string */ var $_root = null; /** * stack for all data that is found * * @access private * @var array */ var $_dataStack = array(); /** * stack for all values that are generated * * @access private * @var array */ var $_valStack = array(); /** * current tag depth * * @access private * @var int */ var $_depth = 0; /** * XML_Parser instance * * @access private * @var object XML_Parser */ var $_parser = null; /** * constructor * * @param mixed $options array containing options for the unserialization * * @access public */ function XML_Unserializer($options = null) { if (is_array($options)) { $this->options = array_merge($this->_defaultOptions, $options); } else { $this->options = $this->_defaultOptions; } } /** * return API version * * @access public * @return string $version API version * @static */ function apiVersion() { return '@package_version@'; } /** * reset all options to default options * * @return void * @access public * @see setOption(), XML_Unserializer(), setOptions() */ function resetOptions() { $this->options = $this->_defaultOptions; } /** * set an option * * You can use this method if you do not want * to set all options in the constructor * * @param string $name name of option * @param mixed $value value of option * * @return void * @access public * @see resetOption(), XML_Unserializer(), setOptions() */ function setOption($name, $value) { $this->options[$name] = $value; } /** * sets several options at once * * You can use this method if you do not want * to set all options in the constructor * * @param array $options options array * * @return void * @access public * @see resetOption(), XML_Unserializer(), setOption() */ function setOptions($options) { $this->options = array_merge($this->options, $options); } /** * unserialize data * * @param mixed $data data to unserialize (string, filename or resource) * @param boolean $isFile data should be treated as a file * @param array $options options that will override * the global options for this call * * @return boolean $success * @access public */ function unserialize($data, $isFile = false, $options = null) { $this->_unserializedData = null; $this->_root = null; // if options have been specified, use them instead // of the previously defined ones if (is_array($options)) { $optionsBak = $this->options; if (isset($options[XML_UNSERIALIZER_OPTION_OVERRIDE_OPTIONS]) && $options[XML_UNSERIALIZER_OPTION_OVERRIDE_OPTIONS] == true ) { $this->options = array_merge($this->_defaultOptions, $options); } else { $this->options = array_merge($this->options, $options); } } else { $optionsBak = null; } $this->_valStack = array(); $this->_dataStack = array(); $this->_depth = 0; $this->_createParser(); if (is_string($data)) { if ($isFile) { $result = $this->_parser->setInputFile($data); if (PEAR::isError($result)) { return $result; } $result = $this->_parser->parse(); } else { $result = $this->_parser->parseString($data, true); } } else { $this->_parser->setInput($data); $result = $this->_parser->parse(); } if ($this->options[XML_UNSERIALIZER_OPTION_RETURN_RESULT] === true) { $return = $this->_unserializedData; } else { $return = true; } if ($optionsBak !== null) { $this->options = $optionsBak; } if (PEAR::isError($result)) { return $result; } return $return; } /** * get the result of the serialization * * @access public * @return string $serializedData */ function getUnserializedData() { if ($this->_root === null) { return $this->raiseError('No unserialized data available. ' . 'Use XML_Unserializer::unserialize() first.', XML_UNSERIALIZER_ERROR_NO_UNSERIALIZATION); } return $this->_unserializedData; } /** * get the name of the root tag * * @access public * @return string $rootName */ function getRootName() { if ($this->_root === null) { return $this->raiseError('No unserialized data available. ' . 'Use XML_Unserializer::unserialize() first.', XML_UNSERIALIZER_ERROR_NO_UNSERIALIZATION); } return $this->_root; } /** * Start element handler for XML parser * * @param object $parser XML parser object * @param string $element XML element * @param array $attribs attributes of XML tag * * @return void * @access private */ function startHandler($parser, $element, $attribs) { if (isset($attribs[$this->options[XML_UNSERIALIZER_OPTION_ATTRIBUTE_TYPE]]) ) { $type = $attribs[$this->options[XML_UNSERIALIZER_OPTION_ATTRIBUTE_TYPE]]; $guessType = false; } else { $type = 'string'; if ($this->options[XML_UNSERIALIZER_OPTION_GUESS_TYPES] === true) { $guessType = true; } else { $guessType = false; } } if ($this->options[XML_UNSERIALIZER_OPTION_DECODE_FUNC] !== null) { $attribs = array_map($this->options[XML_UNSERIALIZER_OPTION_DECODE_FUNC], $attribs); } $this->_depth++; $this->_dataStack[$this->_depth] = null; if (is_array($this->options[XML_UNSERIALIZER_OPTION_TAG_MAP]) && isset($this->options[XML_UNSERIALIZER_OPTION_TAG_MAP][$element]) ) { $element = $this->options[XML_UNSERIALIZER_OPTION_TAG_MAP][$element]; } $val = array( 'name' => $element, 'value' => null, 'type' => $type, 'guessType' => $guessType, 'childrenKeys' => array(), 'aggregKeys' => array() ); if ($this->options[XML_UNSERIALIZER_OPTION_ATTRIBUTES_PARSE] == true && (count($attribs) > 0) ) { $val['children'] = array(); $val['type'] = $this->_getComplexType($element); $val['class'] = $element; if ($this->options[XML_UNSERIALIZER_OPTION_GUESS_TYPES] === true) { $attribs = $this->_guessAndSetTypes($attribs); } if ($this->options[XML_UNSERIALIZER_OPTION_ATTRIBUTES_ARRAYKEY] != false ) { $val['children'][$this-> options[XML_UNSERIALIZER_OPTION_ATTRIBUTES_ARRAYKEY]] = $attribs; } else { foreach ($attribs as $attrib => $value) { $val['children'][$this-> options[XML_UNSERIALIZER_OPTION_ATTRIBUTES_PREPEND] . $attrib] = $value; } } } $keyAttr = false; if (is_string($this->options[XML_UNSERIALIZER_OPTION_ATTRIBUTE_KEY])) { $keyAttr = $this->options[XML_UNSERIALIZER_OPTION_ATTRIBUTE_KEY]; } elseif (is_array($this->options[XML_UNSERIALIZER_OPTION_ATTRIBUTE_KEY])) { if (isset($this->options[XML_UNSERIALIZER_OPTION_ATTRIBUTE_KEY] [$element]) ) { $keyAttr = $this->options[XML_UNSERIALIZER_OPTION_ATTRIBUTE_KEY][$element]; } elseif (isset($this->options[XML_UNSERIALIZER_OPTION_ATTRIBUTE_KEY] ['#default']) ) { $keyAttr = $this->options[XML_UNSERIALIZER_OPTION_ATTRIBUTE_KEY] ['#default']; } elseif (isset($this->options[XML_UNSERIALIZER_OPTION_ATTRIBUTE_KEY] ['__default']) ) { // keep this for BC $keyAttr = $this->options[XML_UNSERIALIZER_OPTION_ATTRIBUTE_KEY] ['__default']; } } if ($keyAttr !== false && isset($attribs[$keyAttr])) { $val['name'] = $attribs[$keyAttr]; } if (isset($attribs[$this-> options[XML_UNSERIALIZER_OPTION_ATTRIBUTE_CLASS]]) ) { $val['class'] = $attribs[$this->options[XML_UNSERIALIZER_OPTION_ATTRIBUTE_CLASS]]; } array_push($this->_valStack, $val); } /** * Try to guess the type of several values and * set them accordingly * * @param array $array array containing the values * * @return array array, containing the values with their correct types * @access private */ function _guessAndSetTypes($array) { foreach ($array as $key => $value) { $array[$key] = $this->_guessAndSetType($value); } return $array; } /** * Try to guess the type of a value and * set it accordingly * * @param string $value character data * * @return mixed value with the best matching type * @access private */ function _guessAndSetType($value) { if ($value === 'true') { return true; } if ($value === 'false') { return false; } if ($value === 'NULL') { return null; } if (preg_match('/^[-+]?[0-9]{1,}\\z/', $value)) { return intval($value); } if (preg_match('/^[-+]?[0-9]{1,}\.[0-9]{1,}\\z/', $value)) { return doubleval($value); } return (string)$value; } /** * End element handler for XML parser * * @param object $parser XML parser object * @param string $element element * * @return void * @access private */ function endHandler($parser, $element) { $value = array_pop($this->_valStack); switch ($this->options[XML_UNSERIALIZER_OPTION_WHITESPACE]) { case XML_UNSERIALIZER_WHITESPACE_KEEP: $data = $this->_dataStack[$this->_depth]; break; case XML_UNSERIALIZER_WHITESPACE_NORMALIZE: $data = trim(preg_replace('/\s\s+/m', ' ', $this->_dataStack[$this->_depth])); break; case XML_UNSERIALIZER_WHITESPACE_TRIM: default: $data = trim($this->_dataStack[$this->_depth]); break; } // adjust type of the value switch(strtolower($value['type'])) { // unserialize an object case 'object': if (isset($value['class'])) { $classname = $value['class']; } else { $classname = ''; } // instantiate the class if ($this->options[XML_UNSERIALIZER_OPTION_TAG_AS_CLASSNAME] === true && class_exists($classname) ) { $value['value'] = new $classname; } else { $value['value'] = new $this->options[XML_UNSERIALIZER_OPTION_DEFAULT_CLASS]; } if (trim($data) !== '') { if ($value['guessType'] === true) { $data = $this->_guessAndSetType($data); } $value['children'][$this-> options[XML_UNSERIALIZER_OPTION_CONTENT_KEY]] = $data; } // set properties foreach ($value['children'] as $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(); } break; // unserialize an array case 'array': if (trim($data) !== '') { if ($value['guessType'] === true) { $data = $this->_guessAndSetType($data); } $value['children'][$this-> options[XML_UNSERIALIZER_OPTION_CONTENT_KEY]] = $data; } if (isset($value['children'])) { $value['value'] = $value['children']; } else { $value['value'] = array(); } break; // unserialize a null value case 'null': $data = null; break; // unserialize a resource => this is not possible :-( case 'resource': $value['value'] = $data; break; // unserialize any scalar value default: if ($value['guessType'] === true) { $data = $this->_guessAndSetType($data); } else { settype($data, $value['type']); } $value['value'] = $data; break; } $parent = array_pop($this->_valStack); if ($parent === null) { $this->_unserializedData = &$value['value']; $this->_root = &$value['name']; return true; } else { // parent has to be an array if (!isset($parent['children']) || !is_array($parent['children'])) { $parent['children'] = array(); if (!in_array($parent['type'], array('array', 'object'))) { $parent['type'] = $this->_getComplexType($parent['name']); if ($parent['type'] == 'object') { $parent['class'] = $parent['name']; } } } if (in_array($element, $this->options[XML_UNSERIALIZER_OPTION_IGNORE_KEYS]) ) { $ignoreKey = true; } else { $ignoreKey = false; } if (!empty($value['name']) && $ignoreKey === false) { // there already has been a tag with this name if (in_array($value['name'], $parent['childrenKeys']) || in_array($value['name'], $this->options[XML_UNSERIALIZER_OPTION_FORCE_ENUM]) ) { // no aggregate has been created for this tag if (!in_array($value['name'], $parent['aggregKeys'])) { if (isset($parent['children'][$value['name']])) { $parent['children'][$value['name']] = array($parent['children'][$value['name']]); } else { $parent['children'][$value['name']] = array(); } array_push($parent['aggregKeys'], $value['name']); } array_push($parent['children'][$value['name']], $value['value']); } else { $parent['children'][$value['name']] = &$value['value']; array_push($parent['childrenKeys'], $value['name']); } } else { array_push($parent['children'], $value['value']); } array_push($this->_valStack, $parent); } $this->_depth--; } /** * Handler for character data * * @param object $parser XML parser object * @param string $cdata CDATA * * @return void * @access private */ function cdataHandler($parser, $cdata) { if ($this->options[XML_UNSERIALIZER_OPTION_DECODE_FUNC] !== null) { $cdata = call_user_func($this-> options[XML_UNSERIALIZER_OPTION_DECODE_FUNC], $cdata); } $this->_dataStack[$this->_depth] .= $cdata; } /** * get the complex type, that should be used for a specified tag * * @param string $tagname name of the tag * * @return string complex type ('array' or 'object') * @access private */ function _getComplexType($tagname) { if (is_string($this->options[XML_UNSERIALIZER_OPTION_COMPLEXTYPE])) { return $this->options[XML_UNSERIALIZER_OPTION_COMPLEXTYPE]; } if (isset($this->options[XML_UNSERIALIZER_OPTION_COMPLEXTYPE][$tagname])) { return $this->options[XML_UNSERIALIZER_OPTION_COMPLEXTYPE][$tagname]; } if (isset($this->options[XML_UNSERIALIZER_OPTION_COMPLEXTYPE]['#default'])) { return $this->options[XML_UNSERIALIZER_OPTION_COMPLEXTYPE]['#default']; } return 'array'; } /** * create the XML_Parser instance * * @return boolean * @access private */ function _createParser() { if (is_object($this->_parser)) { $this->_parser->free(); unset($this->_parser); } $this->_parser = new XML_Parser($this-> options[XML_UNSERIALIZER_OPTION_ENCODING_SOURCE], 'event', $this->options[XML_UNSERIALIZER_OPTION_ENCODING_TARGET]); $this->_parser->folding = false; $this->_parser->setHandlerObj($this); return true; } } ?>