This commit is contained in:
2021-09-10 18:04:57 +08:00
parent 5b33d09da0
commit cde1704866
2 changed files with 14 additions and 15 deletions
+12 -12
View File
@@ -10,26 +10,26 @@ class Parse
/**
* @param $content
* @param $contentType
* @return mixed
* @throws \Exception
*/
public static function data($content, $contentType): mixed
public static function data($content): mixed
{
if (empty($content)) {
return null;
}
if (str_starts_with($content, '<') || str_contains($contentType, 'xml')) {
return Xml::toArray($content);
if (is_bool($content) || is_numeric($content)) {
return $content;
}
if (str_contains($contentType, 'x-www-form-urlencoded')) {
parse_str($content, $array);
return $array;
}
if (str_contains($contentType, 'serialize')) {
return unserialize($content);
}
return json_decode($content, true);
$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;
})
};
}
}