Skip to content

Commit

Permalink
Update Xml.php
Browse files Browse the repository at this point in the history
  • Loading branch information
limingxinleo authored and huangdijia committed Dec 5, 2023
1 parent 4ec3a4b commit 1412ca1
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/codec/src/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,20 @@ public static function toXml(mixed $data, ?SimpleXMLElement $parentNode = null,
}
}
}
return trim($xml->asXML());

$result = $xml->asXML();
if ($result === false) {
throw new InvalidArgumentException('Syntax error.', originData: $data);
}
return trim($result);
}

public static function toArray($xml): array
{
$respObject = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOERROR);

if ($respObject === false) {
throw new InvalidArgumentException('Syntax error.');
throw new InvalidArgumentException('Syntax error.', originData: $xml);
}

return json_decode(json_encode($respObject), true);
Expand Down

0 comments on commit 1412ca1

Please sign in to comment.