// 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); * and their alias methods (of course). */ $book->addChild('chapters', true)->addChild('chapter', 'Ideas About The Universe', ['id' => 123, 'first' => ''])->addChild('chapter', 'The Expanding Universe', ['id' => 321])->addChild('chapter', 'Black Holes', ['id' => 432])->addChild('chapter', 'Black Holes Ain\'t So Black', ['id' => 234]); /******************** * Appending Nodes. * *********************/ /* * Inserting a node can be performed in different ways, * each one with its pros and cons. */ /* * In this examples, it is used the concise syntax, but the same concepts * are applicable to the standard syntax. */ $food = fluidxml('food'); $food->add('fruit')->add('fruit', 'orange'); // A 'fruit' node with 'orange' as content. // A node can have even a bunch of attributes.
\assert($actual === $expected, __($actual, $expected)); $cx = $cx->addChild(['subchild4', 'subchild5', 'subchild6', 'subchild7'], true); $actual = $cx->length(); $expected = 8; \assert($actual === $expected, __($actual, $expected)); $expected = "<doc>\n" . " <child1>\n" . " <subchild1/>\n" . " <subchild2/>\n" . " <subchild3/>\n" . " <subchild4/>\n" . " <subchild5/>\n" . " <subchild6/>\n" . " <subchild7/>\n" . " </child1>\n" . " <child2>\n" . " <subchild1/>\n" . " <subchild2/>\n" . " <subchild3/>\n" . " <subchild4/>\n" . " <subchild5/>\n" . " <subchild6/>\n" . " <subchild7/>\n" . " </child2>\n" . "</doc>"; assert_equal_xml($xml, $expected); }); }); describe('.size()', function () { it('should behave like .length()', function () { $xml = new FluidXml(); $actual = $xml->size(); $expected = $xml->length(); \assert($actual === $expected, __($actual, $expected)); $cx = $xml->addChild('parent', true)->addChild(['child1', 'child2']); $actual = $cx->size(); $expected = $cx->length(); \assert($actual === $expected, __($actual, $expected)); }); }); }); describe('FluidNamespace', function () { describe('.__construct()', function () { it('should accept an id, an uri and an optional mode flag', function () { $ns_id = 'x'; $ns_uri = 'x.com'; $ns_mode = FluidNamespace::MODE_EXPLICIT; $ns = new FluidNamespace($ns_id, $ns_uri); $actual = $ns->id(); $expected = $ns_id;