feat(core): add HTTP request, response and route match value objects
This commit is contained in:
1 parent
bcd3572890
commit
9a729f1609
3 files changed
+158
No files matched your search
@@ -0,0 +1,79 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Bazaar\Core;
|
||||||
|
|
||||||
|
final class Request
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly string $method,
|
||||||
|
private readonly string $path,
|
||||||
|
private readonly array $query,
|
||||||
|
private readonly array $body,
|
||||||
|
private readonly array $server
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function capture(): Request
|
||||||
|
{
|
||||||
|
$uri = $_SERVER['REQUEST_URI'] ?? '/';
|
||||||
|
$path = parse_url($uri, PHP_URL_PATH) ?: '/';
|
||||||
|
$path = '/' . trim(rawurldecode($path), '/');
|
||||||
|
if ($path === '/') {
|
||||||
|
$path = '/';
|
||||||
|
}
|
||||||
|
return new self(
|
||||||
|
strtoupper($_SERVER['REQUEST_METHOD'] ?? 'GET'),
|
||||||
|
$path,
|
||||||
|
$_GET,
|
||||||
|
$_POST,
|
||||||
|
$_SERVER
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function method(): string
|
||||||
|
{
|
||||||
|
return $this->method;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function path(): string
|
||||||
|
{
|
||||||
|
return $this->path;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isPost(): bool
|
||||||
|
{
|
||||||
|
return $this->method === 'POST';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function query(string $key, mixed $default = null): mixed
|
||||||
|
{
|
||||||
|
return $this->query[$key] ?? $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function input(string $key, mixed $default = null): mixed
|
||||||
|
{
|
||||||
|
return $this->body[$key] ?? $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function integer(string $key, int $default = 0): int
|
||||||
|
{
|
||||||
|
$value = $this->body[$key] ?? $this->query[$key] ?? $default;
|
||||||
|
return is_numeric($value) ? (int) $value : $default;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function body(): array
|
||||||
|
{
|
||||||
|
return $this->body;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function header(string $name): ?string
|
||||||
|
{
|
||||||
|
$key = 'HTTP_' . strtoupper(str_replace('-', '_', $name));
|
||||||
|
return $this->server[$key] ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function rawBody(): string
|
||||||
|
{
|
||||||
|
return file_get_contents('php://input') ?: '';
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Bazaar\Core;
|
||||||
|
|
||||||
|
final class Response
|
||||||
|
{
|
||||||
|
private array $headers = [];
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
private string $content = '',
|
||||||
|
private int $status = 200
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function html(string $content, int $status = 200): Response
|
||||||
|
{
|
||||||
|
$response = new self($content, $status);
|
||||||
|
$response->headers['Content-Type'] = 'text/html; charset=UTF-8';
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function json(array $payload, int $status = 200): Response
|
||||||
|
{
|
||||||
|
$response = new self(json_encode($payload, JSON_UNESCAPED_SLASHES), $status);
|
||||||
|
$response->headers['Content-Type'] = 'application/json';
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function redirect(string $location, int $status = 302): Response
|
||||||
|
{
|
||||||
|
$response = new self('', $status);
|
||||||
|
$response->headers['Location'] = $location;
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function withHeader(string $name, string $value): Response
|
||||||
|
{
|
||||||
|
$this->headers[$name] = $value;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function status(): int
|
||||||
|
{
|
||||||
|
return $this->status;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function content(): string
|
||||||
|
{
|
||||||
|
return $this->content;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function send(): void
|
||||||
|
{
|
||||||
|
http_response_code($this->status);
|
||||||
|
foreach ($this->headers as $name => $value) {
|
||||||
|
header($name . ': ' . $value);
|
||||||
|
}
|
||||||
|
echo $this->content;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Bazaar\Core;
|
||||||
|
|
||||||
|
final class RouteMatch
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
public readonly ?array $handler,
|
||||||
|
public readonly array $params,
|
||||||
|
public readonly array $middleware,
|
||||||
|
public readonly int $status
|
||||||
|
) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public function found(): bool
|
||||||
|
{
|
||||||
|
return $this->status === 200 && $this->handler !== null;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user