+7
-6
@@ -85,24 +85,25 @@ class Router
|
||||
private function respond(int $code, mixed $result): void
|
||||
{
|
||||
http_response_code($code);
|
||||
|
||||
if (is_array($result)) {
|
||||
$hasObjects = false;
|
||||
$isList = array_is_list($result);
|
||||
foreach ($result as $key => $val) {
|
||||
if (is_object($val) && method_exists($val, 'toArray')) {
|
||||
$result[$key] = $val->toArray();
|
||||
$hasObjects = true;
|
||||
}
|
||||
}
|
||||
if ($hasObjects) {
|
||||
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
||||
return;
|
||||
}
|
||||
if (array_is_list($result) && (empty($result) || !isset($result['data']))) {
|
||||
$result = ['data' => $result];
|
||||
if ($hasObjects || ($isList && (empty($result) || !isset($result['data'])))) {
|
||||
if ($isList) {
|
||||
$result = ['data' => $result];
|
||||
}
|
||||
}
|
||||
} elseif (is_object($result) && method_exists($result, 'toArray')) {
|
||||
$result = ['data' => $result->toArray()];
|
||||
}
|
||||
|
||||
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user