bupy7 / xml-constructor
XML文档结构的类似数组的构造函数。
v2.0.3
2023-12-29 19:11 UTC
Requires
- php: >=5.6
- ext-xmlwriter: *
Requires (Dev)
- phpunit/phpunit: >=5.7.27
README
XML文档结构的类似数组的构造函数。
支持PHP从5.6到8.3。
安装
将以下内容添加到您的composer.json
文件的require
部分:
"bupy7/xml-constructor": "*"
然后执行composer install
;
或者执行以下命令
$ composer require bupy7/xml-constructor
用法
输入
$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();
输出
<?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>
配置
$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', ], ]);
您可以将indentString
和/或startDocument
设置为null
来禁用它们。
测试
运行测试
$ ./vendor/bin/phpunit --no-coverage
带有覆盖率运行测试
$ XDEBUG_MODE=coverage ./vendor/bin/phpunit
HTML覆盖率路径:build/coverage/index.html
代码风格
要修复代码风格,运行
~/.composer/vendor/bin/php-cs-fixer fix --config=./phpcs.php --verbose
如果您不使用内置的Docker镜像,您必须首先安装PHP CS Fixer
composer global require friendsofphp/php-cs-fixer
许可证
xml-constructor在BSD-3-Clause许可证下发布。有关详细信息,请参阅捆绑的LICENSE.md
。