Files
kiri-core/System/Core/Xml.php
T

50 lines
854 B
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 2019-03-20
* Time: 01:03
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
namespace Snowflake\Core;
/**
* Class Xml
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\Core
2020-08-31 01:27:08 +08:00
*/
class Xml
{
/**
* @param $data
* @param bool $asArray
* @return array|object
*/
2020-12-17 14:09:14 +08:00
public static function toArray($data, $asArray = true): object|array
2020-08-31 01:27:08 +08:00
{
$data = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
if ($asArray) {
return json_decode(json_encode($data), TRUE);
}
return json_decode(json_encode($data));
}
/**
* @param $str
2020-09-15 18:24:55 +08:00
* @return array|bool|object
2020-08-31 01:27:08 +08:00
*/
2020-12-17 14:09:14 +08:00
public static function isXml($str): object|bool|array
2020-08-31 01:27:08 +08:00
{
$xml_parser = xml_parser_create();
if (!xml_parse($xml_parser, $str, true)) {
xml_parser_free($xml_parser);
return false;
} else {
2020-09-15 18:24:55 +08:00
return self::toArray($str);
2020-08-31 01:27:08 +08:00
}
}
}