45 lines
1014 B
PHP
45 lines
1014 B
PHP
<?php
|
|
require_once __DIR__ . '/../config/database.php';
|
|
require_once __DIR__ . '/../config/auth.php';
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
function json_response(array $payload, int $status = 200): void
|
|
{
|
|
http_response_code($status);
|
|
echo json_encode($payload, JSON_UNESCAPED_UNICODE);
|
|
exit;
|
|
}
|
|
|
|
function get_json_input(): array
|
|
{
|
|
$raw = file_get_contents('php://input');
|
|
if (!$raw) return [];
|
|
$data = json_decode($raw, true);
|
|
return is_array($data) ? $data : [];
|
|
}
|
|
|
|
function required_fields(array $data, array $fields): void
|
|
{
|
|
foreach ($fields as $field) {
|
|
if (!isset($data[$field]) || $data[$field] === '') {
|
|
json_response(['success' => false, 'message' => "Field {$field} wajib diisi."], 422);
|
|
}
|
|
}
|
|
}
|
|
|
|
function clean_string($value): string
|
|
{
|
|
return trim((string)($value ?? ''));
|
|
}
|
|
|
|
function clean_float($value): float
|
|
{
|
|
return (float)($value ?? 0);
|
|
}
|
|
|
|
function clean_int($value): int
|
|
{
|
|
return (int)($value ?? 0);
|
|
}
|