diff --git a/extdbms/lib/Controllers/DBMS/Client/PaymentController.php b/extdbms/lib/Controllers/DBMS/Client/PaymentController.php index 78ac5bb..30711d8 100644 --- a/extdbms/lib/Controllers/DBMS/Client/PaymentController.php +++ b/extdbms/lib/Controllers/DBMS/Client/PaymentController.php @@ -49,8 +49,10 @@ class PaymentController extends ClientController //Client_Code = ["C219"=>WinIDC,"C116"=>IDC-JP"] -> 미지급금계산 제외 Client_Code $exclude_clients = ['C116', 'C219']; //mode 당일,1일전,2일전,3일전,custom - $today = date("Y-m-d"); - switch ($params['mode']) { + $today = date("Y-m-d");; + echo var_dump($this->getRequest()->get()); + exit; + switch ($this->getRequest()->get('mode')) { case 'today': $this->getServiceService()->getModel()->where("service_payment_date = CURDATE()"); $this->message = "[{$today} 기준 당일 "; diff --git a/extdbms/lib/Core/Controller.php b/extdbms/lib/Core/Controller.php index eacc97e..6ab13ad 100644 --- a/extdbms/lib/Core/Controller.php +++ b/extdbms/lib/Core/Controller.php @@ -5,19 +5,13 @@ namespace lib\Core; require_once __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'Configs' . DIRECTORY_SEPARATOR . 'Constant.php'; use lib\Configs\View; +use lib\Http\Request; abstract class Controller { - private $_request = null; private ?View $_view = null; - protected function __construct() - { - $this->_view = new View(); - } // - final public function getView(): View - { - return $this->_view; - } + private ?Request $_request = null; + protected function __construct() {} // final public function __get($name) { return $this->getView()->$name; @@ -26,8 +20,22 @@ abstract class Controller { $this->getView()->$name = $value; } + final public function getView(): View + { + if ($this->_view === null) { + $this->_view = new View(); + } + return $this->_view; + } public function render(string $path) { return $this->getView()->render($path); } + final public function getRequest(): Request + { + if ($this->_request === null) { + $this->_request = new Request(); + } + return $this->_request; + } } //Class diff --git a/extdbms/lib/Http/BearerToken.php b/extdbms/lib/Http/BearerToken.php new file mode 100644 index 0000000..81fb60f --- /dev/null +++ b/extdbms/lib/Http/BearerToken.php @@ -0,0 +1,15 @@ +getHeader('Authorization'); + if ($header && str_starts_with($header, 'Bearer ')) { + return substr($header, 7); + } + return null; + } +} diff --git a/extdbms/lib/Http/Cookie.php b/extdbms/lib/Http/Cookie.php new file mode 100644 index 0000000..c587d7a --- /dev/null +++ b/extdbms/lib/Http/Cookie.php @@ -0,0 +1,21 @@ +headers = foreach ($_SERVER as $name => $value) { + if (str_starts_with($name, 'HTTP_')) { + $key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))); + $headers[$key] = $value; + } + } + } + + public function getHeaders(): array + { + return $this->headers; + } + + public function getHeader(string $key): ?string + { + return $this->headers[$key] ?? null; + } +} diff --git a/extdbms/lib/Http/Request.php b/extdbms/lib/Http/Request.php new file mode 100644 index 0000000..7f45837 --- /dev/null +++ b/extdbms/lib/Http/Request.php @@ -0,0 +1,73 @@ +get = $_GET; + $this->post = $_POST; + $this->data = array_merge($_GET, $_POST, json_decode(file_get_contents('php://data'), true) ?? []); + } + + 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; + } +} diff --git a/extdbms/lib/Core/Response.php b/extdbms/lib/Http/Response.php similarity index 81% rename from extdbms/lib/Core/Response.php rename to extdbms/lib/Http/Response.php index 9ae4a86..bfefacb 100644 --- a/extdbms/lib/Core/Response.php +++ b/extdbms/lib/Http/Response.php @@ -1,9 +1,14 @@ setData([ +// 'username' => 'admin123', +// 'email' => 'admin@example.com', +// 'password' => '1234' +// ])->setRules([ +// 'username' => 'required|alpha_numeric|min[4]|max[20]', +// 'email' => 'required|email', +// 'password' => 'required|min[6]' +// ]); + +// if (!$validator->run()) { +// print_r($validator->errors()); +// } + +class Validator +{ + protected array $data = []; + protected array $rules = []; + protected array $errors = []; + protected array $customMessages = []; + + protected array $availableRules = [ + 'required', + 'min', + 'max', + 'email', + 'numeric', + 'alpha', + 'alpha_numeric' + ]; + + public function setData(array $data): self + { + $this->data = $data; + return $this; + } + + public function setRules(array $rules): self + { + $this->rules = $rules; + return $this; + } + + public function setMessages(array $messages): self + { + $this->customMessages = $messages; + return $this; + } + + public function run(): bool + { + $this->errors = []; + + foreach ($this->rules as $field => $ruleStr) { + $rules = explode('|', $ruleStr); + $value = $this->data[$field] ?? null; + + foreach ($rules as $rule) { + $param = null; + if (strpos($rule, '[') !== false) { + preg_match('/(\w+)\[(.*?)\]/', $rule, $matches); + $rule = $matches[1]; + $param = $matches[2]; + } + + $method = "validate_$rule"; + if (method_exists($this, $method)) { + $result = $this->$method($value, $param); + if (!$result) { + $this->addError($field, $rule, $param); + } + } + } + } + + return empty($this->errors); + } + + public function errors(): array + { + return $this->errors; + } + + protected function addError(string $field, string $rule, $param = null): void + { + $message = $this->customMessages["$field.$rule"] ?? $this->defaultMessage($field, $rule, $param); + $this->errors[$field][] = $message; + } + + protected function defaultMessage(string $field, string $rule, $param): string + { + return match ($rule) { + 'required' => "$field is required.", + 'min' => "$field must be at least $param characters.", + 'max' => "$field must be at most $param characters.", + 'email' => "$field must be a valid email address.", + 'numeric' => "$field must be a number.", + 'alpha' => "$field must contain only letters.", + 'alpha_numeric' => "$field must contain only letters and numbers.", + default => "$field is invalid.", + }; + } + + // --- 기본 유효성 검사 메서드 --- + protected function validate_required($value): bool + { + return !empty($value) || $value === '0'; + } + + protected function validate_min($value, $param): bool + { + return strlen($value) >= (int)$param; + } + + protected function validate_max($value, $param): bool + { + return strlen($value) <= (int)$param; + } + + protected function validate_email($value): bool + { + return filter_var($value, FILTER_VALIDATE_EMAIL) !== false; + } + + protected function validate_numeric($value): bool + { + return is_numeric($value); + } + + protected function validate_alpha($value): bool + { + return ctype_alpha($value); + } + + protected function validate_alpha_numeric($value): bool + { + return ctype_alnum($value); + } + + // 사용자 정의 규칙도 등록할 수 있도록 향후 확장 가능 +}