feat(xml): 实现了解析xml的方法

Signed-off-by: 李东云 <dongyu.li@luxcreo.ai>
This commit is contained in:
李东云
2023-05-06 17:46:32 +08:00
committed by 李东云
parent d22bd3ee58
commit be818b7080
2 changed files with 62 additions and 19 deletions

View File

@@ -27,13 +27,8 @@ class XmlService
* @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
*/
@@ -44,29 +39,48 @@ class XmlService
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
bool $removeEmptyTags = false,
): false|string {
return $this->service->encode(
$data,
XmlEncoder::FORMAT,
[
XmlEncoder::ENCODER_IGNORED_NODE_TYPES => $encoderIgnoredNodeTypes,
XmlEncoder::ROOT_NODE_NAME => $xmlRootNodeName,
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,
]
);
}
/**
* @param string $xml
* @param bool $xmlTypeCastAttributes 是否自动转换类型
* @param bool $asCollection 是否全部使用数组
* @param int[] $decoderIgnoredNodeTypes
* @param int $loadOptions
* @return array<string, mixed>
*/
public function decode(
string $xml,
bool $xmlTypeCastAttributes = true,
bool $asCollection = false,
array $decoderIgnoredNodeTypes = [XML_PI_NODE, XML_COMMENT_NODE],
int $loadOptions = LIBXML_NONET | LIBXML_NOBLANKS,
): array {
return $this->service->decode(
$xml,
XmlEncoder::FORMAT,
[
XmlEncoder::LOAD_OPTIONS => $loadOptions,
XmlEncoder::DECODER_IGNORED_NODE_TYPES => $decoderIgnoredNodeTypes,
XmlEncoder::TYPE_CAST_ATTRIBUTES => $xmlTypeCastAttributes,
XmlEncoder::AS_COLLECTION => $asCollection,
]
);
}
}