Example #1
0
             $xml->save('/.impossible/tmp/out.xml');
         } catch (\Exception $e) {
             $actual = $e;
         }
         assert_is_a($actual, \Exception::class);
     });
 });
 describe('.add', function () {
     it('should be fluid', function () {
         assert_is_fluid('add', 'a');
     });
     it('should behave like .appendChild', function () {
         $xml = new FluidXml();
         $xml->appendChild('parent', true)->appendChild(['child1', 'child2'], ['class' => 'child']);
         $alias = new FluidXml();
         $alias->add('parent', true)->add(['child1', 'child2'], ['class' => 'child']);
         $actual = $xml->xml();
         $expected = $alias->xml();
         \assert($actual === $expected, __($actual, $expected));
     });
 });
 describe('.prepend', function () {
     it('should be fluid', function () {
         assert_is_fluid('prepend', 'a');
     });
     it('should behave like .prependSibling', function () {
         $xml = new FluidXml();
         $xml->prependSibling('sibling1', true)->prependSibling(['sibling2', 'sibling3'], ['class' => 'sibling']);
         $alias = new FluidXml();
         $alias->prepend('sibling1', true)->prepend(['sibling2', 'sibling3'], ['class' => 'sibling']);
         $actual = $xml->xml();
Example #2
0
// PHP arrays can't contain identical keys.
// But it's still possible to create, in a batch operation, nodes with the same tag.
$food->add([['pasta' => 'Carbonara'], ['pasta' => 'Matriciana']]);
// A bunch of egg's all with the same price.
$food->add([['egg'], ['egg'], ['egg']], ['price' => '0.25']);
// Complex array structures are supported too.
$food->add(['menu' => ['pasta' => ['spaghetti' => ['@id' => '123', '@country' => 'Italy', '@' => 'Spaghetti are an Italian dish...', 'variants' => ['tomato' => ['@type' => 'vegan'], 'egg' => ['@type' => 'vegetarian']]]]]]);
echo $food->xml();
echo "————————————————————————————————————————————————————————————————————————————————\n";
/*************************
 * Importing XML strings.*
**************************/
/*
* Raw XML/XHTML strings che be injected at any point of the document too.
*/
$book->add('cover', true)->add(<<<XML
                <h1>The Theory Of Everything</h1>
                <img src="http://goo.gl/kO3Iov"/>
XML
);
/*
* The document can be filled with a raw XML string.
*/
$html = fluidxml(null);
$html->add(<<<XML
<html>
    <head>
        <meta/>
    </head>
    <body>
        <p/>