dbms_primeidc/extdbms/lib/Http/Request.php
2025-04-10 10:48:46 +09:00

74 lines
1.4 KiB
PHP

<?php
namespace lib\Http;
class Request extends Http
{
protected array $get;
protected array $post;
protected array $data;
public function __construct()
{
parent::__construct();
$this->get = $_GET;
$this->post = $_POST;
$this->data = array_merge($_GET, $_POST);
}
public function all(): array
{
return $this->data;
}
public function get(?string $key = null, $default = null): mixed
{
if ($key === null) {
return $this->all();
}
return $this->data[$key] ?? $default;
}
public function only(array $keys): array
{
return array_intersect_key($this->data, array_flip($keys));
}
public function has(string $key): bool
{
return array_key_exists($key, $this->data);
}
/**
* Validator 연동
*/
//사용예:
// $request = new Request();
// $validation = $request->validate([
// 'username' => 'required|alpha_numeric|min[4]',
// 'email' => 'required|email',
// 'password' => 'required|min[6]'
// ]);
// if ($validation !== true) {
// // 에러 처리
// print_r($validation);
// } else {
// // 통과
// echo "유효성 검사 통과!";
// }
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;
}
}