<?php
namespace Yurun\PaySDK\Lib;
class XML
{
public static function fromString($string)
{
$oldValue = libxml_disable_entity_loader(true);
$result = (array) simplexml_load_string($string, null, \LIBXML_NOCDATA | \LIBXML_COMPACT);
if (false === $oldValue)
{
libxml_disable_entity_loader(false);
}
return $result;
}
public static function toString($data)
{
$result = '<xml>';
if (\is_object($data))
{
$_data = ObjectToArray::parse($data);
}
else
{
$_data = &$data;
}
foreach ($_data as $key => $value)
{
if (!is_scalar($value))
{
if (\is_object($value) && method_exists($value, 'toString'))
{
$value = $value->toString();
if (null === $value)
{
continue;
}
}
elseif (null !== $value)
{
$value = json_encode($value);
}
else
{
continue;
}
}
$result .= "<{$key}><![CDATA[{$value}]]></{$key}>";
}
return $result . '</xml>';
}
}