🚀 مُصادَق: الإطلاق الأولي للنظام المتكامل
This commit is contained in:
56
app/Core/Request.php
Normal file
56
app/Core/Request.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Core;
|
||||
|
||||
final class Request
|
||||
{
|
||||
private string $method;
|
||||
private string $path;
|
||||
private array $headers;
|
||||
private array $queryParams;
|
||||
private array $body;
|
||||
private array $files;
|
||||
public ?object $user = null; // Populated by AuthMiddleware
|
||||
public ?string $tenantId = null; // Populated by TenantMiddleware
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->method = $_SERVER['REQUEST_METHOD'];
|
||||
$this->path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
||||
$this->headers = getallheaders();
|
||||
$this->queryParams = $_GET;
|
||||
$this->files = $_FILES;
|
||||
|
||||
$contentType = $this->getHeader('Content-Type');
|
||||
if ($contentType && str_contains($contentType, 'application/json')) {
|
||||
$this->body = json_decode(file_get_contents('php://input'), true) ?? [];
|
||||
} else {
|
||||
$this->body = $_POST;
|
||||
}
|
||||
}
|
||||
|
||||
public function getMethod(): string { return $this->method; }
|
||||
public function getPath(): string { return $this->path; }
|
||||
public function getHeaders(): array { return $this->headers; }
|
||||
public function getQueryParams(): array { return $this->queryParams; }
|
||||
public function getBody(): array { return $this->body; }
|
||||
public function getFiles(): array { return $this->files; }
|
||||
|
||||
public function getHeader(string $name): ?string
|
||||
{
|
||||
$name = strtolower($name);
|
||||
foreach ($this->headers as $key => $value) {
|
||||
if (strtolower($key) === $name) {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function input(string $key, mixed $default = null): mixed
|
||||
{
|
||||
return $this->body[$key] ?? $this->queryParams[$key] ?? $default;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user