feat(xml): 增加了 xml 生成的方法及测试用例

This commit is contained in:
李东云
2023-04-11 16:00:39 +08:00
parent db31689f66
commit 90a521f90a
6 changed files with 689 additions and 429 deletions

View File

@@ -0,0 +1,72 @@
<?php
/**
* XmlService.php@LuxOP
*
* @author 李东云 <Dongyun.Li@LuxCreo.Ai>
* Powered by PhpStorm
* Created on 2023/4/10
*/
namespace Singularity\HDK\Core\Service;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
class XmlService
{
private XmlEncoder $service;
public function __construct(XmlEncoder $encoder)
{
$this->service = $encoder;
}
/**
* @param object|array<string, mixed> $data
* @param string $xmlRootNodeName
* @param bool $xmlFormatOutput 是否格式化
* @param string $xmlVersion
* @param string $xmlEncoding
* @param bool $xmlStandalone 是否添加声明规范的标签到xml
* @param bool $xmlTypeCastAttributes
* @param bool $asCollection
* @param int[] $decoderIgnoredNodeTypes
* @param int[] $encoderIgnoredNodeTypes
* @param int $loadOptions
* @param bool $removeEmptyTags
*
* @return false|string
* @see https://symfony.com/doc/current/components/serializer.html#the-xmlencoder
*/
public function encode(
object|array $data,
string $xmlRootNodeName = 'root',
bool $xmlFormatOutput = true,
string $xmlVersion = '1.0',
string $xmlEncoding = 'utf-8',
bool $xmlStandalone = false,
bool $xmlTypeCastAttributes = true,
bool $asCollection = true,
array $decoderIgnoredNodeTypes = [XML_PI_NODE, XML_COMMENT_NODE],
array $encoderIgnoredNodeTypes = [],
int $loadOptions = LIBXML_NONET | LIBXML_NOBLANKS,
bool $removeEmptyTags = false
): false|string {
return $this->service->encode(
$data,
XmlEncoder::FORMAT,
[
XmlEncoder::FORMAT_OUTPUT => $xmlFormatOutput,
XmlEncoder::VERSION => $xmlVersion,
XmlEncoder::ENCODING => $xmlEncoding,
XmlEncoder::STANDALONE => $xmlStandalone,
XmlEncoder::TYPE_CAST_ATTRIBUTES => $xmlTypeCastAttributes,
XmlEncoder::ROOT_NODE_NAME => $xmlRootNodeName,
XmlEncoder::AS_COLLECTION => $asCollection,
XmlEncoder::DECODER_IGNORED_NODE_TYPES => $decoderIgnoredNodeTypes,
XmlEncoder::ENCODER_IGNORED_NODE_TYPES => $encoderIgnoredNodeTypes,
XmlEncoder::LOAD_OPTIONS => $loadOptions,
XmlEncoder::REMOVE_EMPTY_TAGS => $removeEmptyTags,
]
);
}
}