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

36 lines
574 B
PHP
Raw Normal View History

2021-09-10 03:33:45 +08:00
<?php
2021-09-16 14:53:36 +08:00
namespace Http\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
* @return mixed
2021-09-10 14:06:38 +08:00
* @throws \Exception
2021-09-10 10:24:11 +08:00
*/
2021-09-10 18:04:57 +08:00
public static function data($content): mixed
2021-09-10 10:24:11 +08:00
{
2021-09-10 14:06:38 +08:00
if (empty($content)) {
return null;
2021-09-10 10:24:11 +08:00
}
2021-09-10 18:04:57 +08:00
if (is_bool($content) || is_numeric($content)) {
return $content;
2021-09-10 10:24:11 +08:00
}
2021-09-10 18:04:57 +08:00
$start = substr($content, 0, 1);
return match ($start) {
'<' => Xml::toArray($content),
'[', '{' => json_decode($content, true),
default => call_user_func(function () use ($content) {
parse_str($content, $array);
return $array;
})
};
2021-09-10 10:24:11 +08:00
}
2021-09-10 03:33:45 +08:00
}