/***************************** * Creating An XML Document. * ******************************/ $book = new FluidXml('book'); // or $book = new FluidXml(null, ['root' => 'book']); // $book is an XML document with 'book' as root node. // The default options are: // [ 'root' => 'doc', // The root node of the document. // 'version' => '1.0', // The version for the XML header. // 'encoding' => 'UTF-8', // The encoding for the XML header. // 'stylesheet' => null ]; // An url pointing to an XSL file. $booksheet = new FluidXml('book', ['stylesheet' => 'http://domain.com/style.xsl']); // With PHP 7 this is valid too: // $booksheet = FluidXml::new('book', ['stylesheet' => 'http://domain.com/style.xsl']); $book->setAttribute('type', 'science')->addChild(['title' => 'The Theory Of Everything', 'author' => 'S. Hawking']); // It creates two nodes, each one with some text inside. echo $book->xml(); // Exports the xml document as a string. echo "————————————————————————————————————————————————————————————————————————————————\n"; /********************** * Context Switching. * ***********************/ /* * Passing a 'true' boolean value to any method that performs an insertion of a node, * returns the newly created node instead of the parent. * This operation is called Context Switch. * Methods that support this context switch are: * - addChild($child, ...$optionals); * - prependSibling($sibling, ...$optionals); * - appendSibling($sibling, ...$optionals);
$xml = new FluidXml(); $xml->appendSibling('sibling1', true)->appendSibling(['sibling2', 'sibling3'], ['class' => 'sibling']); $alias = new FluidXml(); $alias->insertSiblingAfter('sibling1', true)->insertSiblingAfter(['sibling2', 'sibling3'], ['class' => 'sibling']); $actual = $xml->xml(); $expected = $alias->xml(); \assert($actual === $expected, __($actual, $expected)); }); }); describe('.attr', function () { it('should be fluid', function () { assert_is_fluid('attr', 'a', 'b'); }); it('should behave like .setAttribute', function () { $xml = new FluidXml(); $xml->setAttribute('attr1', 'Attr1 Value')->setAttribute(['attr2' => 'Attr2 Value', 'attr3' => 'Attr3 Value'])->appendChild('child', true)->setAttribute('attr4', 'Attr4 Value')->setAttribute(['attr5' => 'Attr5 Value', 'attr6' => 'Attr6 Value']); $alias = new FluidXml(); $alias->attr('attr1', 'Attr1 Value')->attr(['attr2' => 'Attr2 Value', 'attr3' => 'Attr3 Value'])->appendChild('child', true)->attr('attr4', 'Attr4 Value')->attr(['attr5' => 'Attr5 Value', 'attr6' => 'Attr6 Value']); $actual = $xml->xml(); $expected = $alias->xml(); \assert($actual === $expected, __($actual, $expected)); }); }); describe('.text', function () { it('should be fluid', function () { assert_is_fluid('text', 'a'); }); it('should behave like .setText', function () { $xml = new FluidXml(); $xml->setText('Text1')->appendChild('child', true)->setText('Text2'); $alias = new FluidXml();
$xml->addChild('child', true)->setAttribute('attr1', 'Attr1 Value')->setAttribute('attr2', 'Attr2 Value')->setAttribute('attr2', 'Attr2 New Value'); $expected = "<doc>\n" . " <child attr1=\"Attr1 Value\" attr2=\"Attr2 New Value\"/>\n" . "</doc>"; assert_equal_xml($xml, $expected); $xml = new FluidXml(); $xml->addChild('child', true)->setAttribute(['attr1' => 'Attr1 Value', 'attr2' => 'Attr2 Value'])->setAttribute('attr1', 'Attr1 New Value'); $expected = "<doc>\n" . " <child attr1=\"Attr1 New Value\" attr2=\"Attr2 Value\"/>\n" . "</doc>"; assert_equal_xml($xml, $expected); }); }); describe('.attr()', function () { it('should be fluid', function () { assert_is_fluid('attr', 'a', 'b'); }); it('should behave like .setAttribute()', function () { $xml = new FluidXml(); $xml->setAttribute('attr1', 'Value 1')->setAttribute('attr2')->setAttribute(['attr3' => 'Value 3', 'attr4' => 'Value 4'])->setAttribute(['attr5', 'attr6'])->addChild('child', true)->setAttribute('attr1', 'Value 1')->setAttribute('attr2')->setAttribute(['attr3' => 'Value 3', 'attr4' => 'Value 4'])->setAttribute(['attr5', 'attr6']); $alias = new FluidXml(); $alias->attr('attr1', 'Value 1')->attr('attr2')->attr(['attr3' => 'Value 3', 'attr4' => 'Value 4'])->attr(['attr5', 'attr6'])->addChild('child', true)->attr('attr1', 'Value 1')->attr('attr2')->attr(['attr3' => 'Value 3', 'attr4' => 'Value 4'])->attr(['attr5', 'attr6']); $actual = $xml->xml(); $expected = $alias->xml(); \assert($actual === $expected, __($actual, $expected)); }); }); describe('.setText()', function () { it('should be fluid', function () { assert_is_fluid('setText', 'a'); }); it('should set/change the text of the root node', function () { $xml = new FluidXml(); $xml->setText('First Text')->setText('Second Text'); $expected = "<doc>Second Text</doc>";