Add pjson support for 1D arrays
This commit is contained in:
@@ -27,10 +27,23 @@ class Parameters
|
||||
*/
|
||||
public static function json(string $name, object|array $document): array
|
||||
{
|
||||
return [$name => match (is_object($document) && method_exists($document, 'toJson')) {
|
||||
true => $document->toJson(),
|
||||
false => json_encode($document, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES)
|
||||
}];
|
||||
$flags = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
|
||||
|
||||
if (is_object($document)) {
|
||||
return [
|
||||
$name => method_exists($document, 'toJson') ? $document->toJson($flags) : json_encode($document, $flags)
|
||||
];
|
||||
}
|
||||
if (empty($document)) return [$name => json_encode($document, $flags)];
|
||||
|
||||
$key = array_key_first($document);
|
||||
if (is_array($document[$key]) && method_exists($document[$key][array_key_first($document[$key])], 'toJson')) {
|
||||
return [
|
||||
$name => sprintf('{%s:[%s]}', json_encode($key, $flags),
|
||||
implode(',', array_map(fn($it) => $it->toJson($flags), $document[$key])))
|
||||
];
|
||||
}
|
||||
return [$name => json_encode($document, $flags)];
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user