// 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: * - appendChild($child, ...$optionals); * - prependSibling($sibling, ...$optionals); * - appendSibling($sibling, ...$optionals); * and their alias methods (of course). */ $book->appendChild('chapters', true)->appendChild('chapter', 'Ideas About The Universe', ['id' => 123, 'first' => ''])->appendChild('chapter', 'The Expanding Universe', ['id' => 321])->appendChild('chapter', 'Black Holes', ['id' => 432])->appendChild('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.
$a = $cx->asArray(); $actual = $a; assert(\is_array($actual)); $actual = \count($a); $expected = 2; assert($actual === $expected, __($actual, $expected)); }); }); describe('.length', function () { it('should return the number of nodes inside the context', function () { $xml = new FluidXml(); $cx = $xml->query('/*'); $actual = $cx->length(); $expected = 1; assert($actual === $expected, __($actual, $expected)); $cx = $xml->appendChild(['child1', 'child2'], true); $actual = $cx->length(); $expected = 2; assert($actual === $expected, __($actual, $expected)); $cx = $cx->appendChild(['subchild1', 'subchild2', 'subchild3']); $actual = $cx->length(); $expected = 2; assert($actual === $expected, __($actual, $expected)); $cx = $cx->appendChild(['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); }); });