bupy7/xml-constructor

The array-like constructor of XML document structure.

v1.3.3 2022-01-22 12:40 UTC

This package is auto-updated.

Last update: 2022-07-22 13:54:30 UTC


README

Stable Version Build status Coverage Status Total Downloads License

The array-like constructor of XML document structure.

Supporting PHP from 5.6 up to 8.1.

Install

Add the following to require section of your composer.json:

"bupy7/xml-constructor": "*"

Then do composer install;

or execute the command:

$ composer require bupy7/xml-constructor

Usage

Input:

$xml = new XmlConstructor();
$in = [
    [
        'tag' => 'root',
        'elements' => [
            [
                'tag' => 'tag1',
                'attributes' => [
                    'attr1' => 'val1',
                    'attr2' => 'val2',
                ],
            ],
            [
                'tag' => 'tag2',
                'content' => 'content2',
            ],
            [
                'tag' => 'tag3',
                'elements' => [
                    [
                        'tag' => 'tag4',
                        'content' => 'content4',
                    ],
                ],
            ],
            [
                'tag' => 'tag4',
                'content' => '<b>content4</b>',
                'cdata' => true, // by default - false, see https://en.wikipedia.org/wiki/CDATA
            ],
        ],
    ],
];
echo $xml->fromArray($in)->toOutput();

Output:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <tag1 attr1="val1" attr2="val2"/>
    <tag2>content2</tag2>
    <tag3>
        <tag4>content4</tag4>
    </tag3>
    <tag4><![CDATA[<b>content4</b>]]></tag4>
</root>

Testing

Run tests:

$ ./vendor/bin/phpunit --no-coverage

Run tests with coverage:

$ XDEBUG_MODE=coverage ./vendor/bin/phpunit

HTML coverage path: build/coverage/index.html

License

xml-constructor is released under the BSD 3-Clause License. See the bundled LICENSE.md for details.