The array-like constructor of XML document structure.
Supporting PHP from 5.6 up to 8.3.
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
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>
Configuration:
$xml = new XmlConstructor([
// Indent each line in the XML document. 4 space by default.
'indentString' => ' ',
/* Header document tag. "<?xml version="1.0" encoding="UTF-8"?>" by default. */
'startDocument' => [
// version
'1.0',
// encoding
'UTF-8',
],
]);
You can set up indentString
and/or startDocument
as null
to disable them.
Run tests:
$ ./vendor/bin/phpunit --no-coverage
Run tests with coverage:
$ XDEBUG_MODE=coverage ./vendor/bin/phpunit
HTML coverage path: build/coverage/index.html
To fix code style, run:
~/.composer/vendor/bin/php-cs-fixer fix --config=./phpcs.php --verbose
You have to install PHP CS Fixer at first, if you don't use build-in Docker image:
composer global require friendsofphp/php-cs-fixer
xml-constructor is released under the BSD-3-Clause License. See the bundled LICENSE.md
for details.