From 87dd1bafa71fa5a596cfb3f8f552896fba932007 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Fri, 10 Sep 2021 14:18:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Core/Xml.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/core/Core/Xml.php b/core/Core/Xml.php index 294074b3..106a8b88 100644 --- a/core/Core/Xml.php +++ b/core/Core/Xml.php @@ -9,6 +9,8 @@ declare(strict_types=1); namespace Kiri\Core; +use Exception; + /** * Class Xml * @package Kiri\Kiri\Core @@ -20,19 +22,26 @@ class Xml * @param $data * @param bool $asArray * @return array|object + * @throws Exception */ public static function toArray($data, bool $asArray = true): object|array { $data = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA); - if ($asArray) { - return json_decode(json_encode($data), TRUE); + if ($data === false) { + throw new Exception('Parameter format error.'); } - return json_decode(json_encode($data)); + $array = get_object_vars($data); + if (isset($array[0])) { + $array[$data->getName()] = $array[0]; + unset($array[0]); + } + return $array; } /** * @param $str * @return array|bool|object + * @throws Exception */ public static function isXml($str): object|bool|array {