You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
46 lines
1015 B
46 lines
1015 B
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Yansongda\Pay\Packer;
|
|
|
|
use Yansongda\Pay\Contract\PackerInterface;
|
|
|
|
class XmlPacker implements PackerInterface
|
|
{
|
|
public function pack(array $payload): string
|
|
{
|
|
$xml = '<xml>';
|
|
|
|
foreach ($payload as $key => $val) {
|
|
$xml .= is_numeric($val) ? '<'.$key.'>'.$val.'</'.$key.'>' :
|
|
'<'.$key.'><![CDATA['.$val.']]></'.$key.'>';
|
|
}
|
|
|
|
$xml .= '</xml>';
|
|
|
|
return $xml;
|
|
}
|
|
|
|
public function unpack(string $payload): ?array
|
|
{
|
|
if (empty($payload)) {
|
|
return [];
|
|
}
|
|
|
|
if (PHP_VERSION_ID < 80000) {
|
|
libxml_disable_entity_loader();
|
|
}
|
|
|
|
$data = json_decode(json_encode(
|
|
simplexml_load_string($payload, 'SimpleXMLElement', LIBXML_NOCDATA),
|
|
JSON_UNESCAPED_UNICODE
|
|
), true);
|
|
|
|
if (JSON_ERROR_NONE === json_last_error()) {
|
|
return $data;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|