mirror of
http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore.git
synced 2026-01-15 07:15:06 +08:00
feat(xml): 增加了 xml 生成的方法及测试用例
This commit is contained in:
72
src/Service/XmlService.php
Normal file
72
src/Service/XmlService.php
Normal 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,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user