$message], $status_code); } // --------------------------------------------------------------------- // Validate Required Fields // Checks if all specified Fields exist in the given Data Array and are // non-empty. Returns an Array of missing Field Names, or an empty // Array if all Fields are present. // --------------------------------------------------------------------- function validate_required($data, $fields) { $missing = []; foreach ($fields as $field) { if (!isset($data[$field]) || trim($data[$field]) === '') { $missing[] = $field; } } return $missing; } // --------------------------------------------------------------------- // Get POST Input // Reads and trims all POST Parameters. Returns an associative Array. // Falls back to JSON Request Body if no POST Data is present // (for Clients that send JSON instead of Form Data). // --------------------------------------------------------------------- function get_input() { // Check for standard POST Form Data first if (!empty($_POST)) { return array_map('trim', $_POST); } // Fall back to JSON Request Body $json = file_get_contents('php://input'); $data = json_decode($json, true); if (is_array($data)) { return array_map('trim', $data); } return []; } // --------------------------------------------------------------------- // Get PDO Connection // Returns the PDO Instance created in init.php. Wrapped in a Function // to avoid global Variable Dependencies in Endpoint Files. // --------------------------------------------------------------------- function get_db() { global $pdo; if (!$pdo) { error_response('Database Connection failed.', 500); } return $pdo; }