dbms_primeidc/extdbms/lib/Core/Http/Request.php
2025-04-25 19:15:31 +09:00

154 lines
3.4 KiB
PHP

<?php
namespace lib\Core\Http;
class Request extends Http
{
private array $_datas = [];
private array $_files = [];
private array $_server = [];
protected string $baseUrl;
protected string $uri;
public function __construct(array $params = [])
{
parent::__construct();
$this->_datas = array_merge($_GET, $_POST, $params);
$this->_files = $_FILES ?? [];
$this->_server = $_SERVER ?? [];
$this->baseUrl = $this->detectBaseUrl();
$this->uri = $this->detectUri();
}
// --------------------
// 요청 데이터 관련
// --------------------
public function all(): array
{
return $this->_datas;
}
public function get(?string $key = null, $default = null): mixed
{
if ($key === null) {
return $this->all();
}
return $this->_datas[$key] ?? $default;
}
public function only(array $keys): array
{
return array_intersect_key($this->_datas, array_flip($keys));
}
public function has(string $key): bool
{
return array_key_exists($key, $this->_datas);
}
public function file(string $key): mixed
{
return $this->_files[$key] ?? null;
}
public function method(): string
{
return strtoupper($_SERVER['REQUEST_METHOD'] ?? 'GET');
}
public function isPost(): bool
{
return $this->method() === 'POST';
}
public function isGet(): bool
{
return $this->method() === 'GET';
}
// --------------------
// URL/경로 관련
// --------------------
public function baseUrl(): string
{
return $this->baseUrl;
}
public function currentUrl(): string
{
return $this->baseUrl . '/' . ltrim($this->uri, '/');
}
public function previousUrl(): ?string
{
return $_SESSION['_previous_url'] ?? null;
}
public function segments(): array
{
return explode('/', trim($this->uri, '/'));
}
public function segment(int $n): ?string
{
$segments = $this->segments();
return $segments[$n - 1] ?? null;
}
public function urlIs(string $pattern): bool
{
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$pattern = str_replace('*', '.*', preg_quote($pattern, '/'));
return preg_match("/^{$pattern}$/", $uri) === 1;
}
public function to(string $path): string
{
return rtrim($this->baseUrl, '/') . '/' . ltrim($path, '/');
}
// 현재 URI 반환 (도메인 제외)
public function path(): string
{
return $this->uri;
}
// --------------------
// 내부 URL 추출 로직
// --------------------
protected function detectUri(): string
{
$uri = $_SERVER['REQUEST_URI'] ?? '/';
$scriptName = dirname($_SERVER['SCRIPT_NAME'] ?? '');
$uri = str_replace($scriptName, '', $uri);
$uri = strtok($uri, '?'); // 쿼리 스트링 제거
return $uri;
}
protected function detectBaseUrl(): string
{
$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'] ?? 'localhost';
$scriptDir = rtrim(dirname($_SERVER['SCRIPT_NAME'] ?? ''), '/');
return $scheme . '://' . $host . $scriptDir;
}
// --------------------
// Validator 연동
// --------------------
public function validate(array $rules, array $messages = []): bool|array
{
$validator = new Validator();
$validator->setData($this->all())->setRules($rules)->setMessages($messages);
if (!$validator->run()) {
return $validator->errors();
}
return true;
}
}