50 lines
1.2 KiB
PHP
50 lines
1.2 KiB
PHP
<?php
|
|
namespace Blog\Http;
|
|
|
|
class Request {
|
|
private string $method;
|
|
private string $path;
|
|
private array $postData;
|
|
|
|
public function __construct(string $method, string $uri, array $postData = []) {
|
|
$this->method = strtoupper($method);
|
|
$this->path = parse_url($uri, PHP_URL_PATH) ?? '/';
|
|
$this->postData = $postData ?: $_POST;
|
|
}
|
|
|
|
public function getMethod(): string {
|
|
return $this->method;
|
|
}
|
|
|
|
public function getPath(): string {
|
|
return $this->path;
|
|
}
|
|
|
|
public function getPost(string $key, $default = null): mixed {
|
|
return $this->postData[$key] ?? $default;
|
|
}
|
|
|
|
public function allPost(): array {
|
|
return $this->postData;
|
|
}
|
|
|
|
public function getQuery(string $key, $default = null): mixed {
|
|
$query = [];
|
|
parse_str(parse_url($this->path, PHP_URL_QUERY) ?? '', $query);
|
|
return $query[$key] ?? $default;
|
|
}
|
|
|
|
public function getHeader(string $key): ?string {
|
|
$headerKey = 'HTTP_' . strtoupper(str_replace('-', '_', $key));
|
|
return $_SERVER[$headerKey] ?? null;
|
|
}
|
|
|
|
public function getRawInput(): string {
|
|
return file_get_contents('php://input');
|
|
}
|
|
|
|
public function getJson(): array {
|
|
return json_decode($this->getRawInput(), true) ?? [];
|
|
}
|
|
}
|