Files
awo-hamburg-intranet/module/tasks/lib/Request.php

93 lines
2.1 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
class TaskCertRequest
{
public static function method(): string
{
return strtoupper((string) ($_SERVER['REQUEST_METHOD'] ?? 'GET'));
}
public static function isPost(): bool
{
return self::method() === 'POST';
}
public static function postString(string $key, string $default = ''): string
{
$value = $_POST[$key] ?? $default;
if (is_array($value)) {
return $default;
}
return trim((string) $value);
}
public static function postInt(string $key, int $default = 0): int
{
return (int) ($_POST[$key] ?? $default);
}
public static function postArray(string $key): array
{
$value = $_POST[$key] ?? [];
return is_array($value) ? $value : [];
}
public static function postIntArray(string $key): array
{
$items = self::postArray($key);
$result = [];
$collect = static function ($value) use (&$collect, &$result): void {
if (is_array($value)) {
foreach ($value as $nestedValue) {
$collect($nestedValue);
}
return;
}
$itemInt = (int) $value;
if ($itemInt > 0) {
$result[] = $itemInt;
}
};
foreach ($items as $item) {
$collect($item);
}
return array_values(array_unique($result));
}
public static function queryString(string $key, string $default = ''): string
{
$value = $_GET[$key] ?? $default;
if (is_array($value)) {
return $default;
}
return trim((string) $value);
}
public static function queryInt(string $key, int $default = 0): int
{
return (int) ($_GET[$key] ?? $default);
}
public static function jsonBody(): array
{
$raw = file_get_contents('php://input');
if ($raw === false || $raw === '') {
return [];
}
$decoded = json_decode($raw, true);
if (!is_array($decoded)) {
return [];
}
return $decoded;
}
}