Files
kiri-core/http-message/Parse.php
T

36 lines
635 B
PHP
Raw Normal View History

2021-09-10 03:33:45 +08:00
<?php
2021-09-10 10:11:23 +08:00
namespace Protocol\Message;
2021-09-10 03:33:45 +08:00
use Kiri\Core\Xml;
class Parse
{
2021-09-10 10:24:11 +08:00
/**
* @param $content
* @param $contentType
* @return mixed
2021-09-10 14:06:38 +08:00
* @throws \Exception
2021-09-10 10:24:11 +08:00
*/
public static function data($content, $contentType): mixed
{
2021-09-10 14:06:38 +08:00
if (empty($content)) {
return null;
2021-09-10 10:24:11 +08:00
}
2021-09-10 14:06:38 +08:00
if (str_starts_with($content, '<') || str_contains($contentType, 'xml')) {
2021-09-10 13:57:27 +08:00
return Xml::toArray($content);
2021-09-10 10:24:11 +08:00
}
if (str_contains($contentType, 'x-www-form-urlencoded')) {
parse_str($content, $array);
return $array;
}
if (str_contains($contentType, 'serialize')) {
return unserialize($content);
}
2021-09-10 14:06:38 +08:00
return json_encode($content, true);
2021-09-10 10:24:11 +08:00
}
2021-09-10 03:33:45 +08:00
}