1e51ef7def
- Konzept/, didaktik_geografie/, didaktik_simulation/, v2-modules/, v2-platform/ - 12 code-workspace-Files - STATUS-*.md - viele M/D/R-Änderungen an bereits getrackten Files - .gitignore verstärkt: **/.humaninput/, **/secret_keys.txt Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
51 lines
1.2 KiB
PHP
51 lines
1.2 KiB
PHP
<?php
|
|
/**
|
|
* V2-Plattform — JSON-Response-Helpers
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
function v2_response_json(int $status, array $data): never {
|
|
if (!headers_sent()) {
|
|
http_response_code($status);
|
|
}
|
|
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
|
|
exit;
|
|
}
|
|
|
|
function v2_response_ok(array $data = []): never {
|
|
v2_response_json(200, $data);
|
|
}
|
|
|
|
function v2_response_created(array $data = []): never {
|
|
v2_response_json(201, $data);
|
|
}
|
|
|
|
function v2_response_error(int $status, string $code, string $message, array $extra = []): never {
|
|
v2_response_json($status, array_merge([
|
|
'error' => $code,
|
|
'message' => $message,
|
|
], $extra));
|
|
}
|
|
|
|
/**
|
|
* Liest Request-Body als JSON. Gibt assoc-Array oder null bei Fehler.
|
|
*/
|
|
function v2_request_body_json(): ?array {
|
|
$raw = file_get_contents('php://input');
|
|
if (!$raw) return null;
|
|
$decoded = json_decode($raw, true);
|
|
return is_array($decoded) ? $decoded : null;
|
|
}
|
|
|
|
/**
|
|
* Pflicht-JSON-Body — bei fehlend/ungültig: 400 + exit.
|
|
*/
|
|
function v2_request_body_json_required(): array {
|
|
$body = v2_request_body_json();
|
|
if (!$body) {
|
|
v2_response_error(400, 'bad_request', 'JSON-Body fehlt oder ist ungültig.');
|
|
}
|
|
return $body;
|
|
}
|