67 lines
1.5 KiB
PHP
67 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace Dabestaniha\AuthenticateBridge\App\Http\Requests;
|
|
|
|
use Dabestaniha\AuthenticateBridge\Support\Collection;
|
|
|
|
abstract class BaseFormRequest
|
|
{
|
|
public static function make(): static
|
|
{
|
|
return new static();
|
|
}
|
|
|
|
protected array $url;
|
|
protected array $form;
|
|
protected Collection $validated;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->url = parse_url($_SERVER['REQUEST_URI']);
|
|
$this->form = [
|
|
...$_GET,
|
|
...$_POST,
|
|
];
|
|
}
|
|
|
|
public function getRoutePath(): string
|
|
{
|
|
return $this->url['path'];
|
|
}
|
|
|
|
public function validate(): void
|
|
{
|
|
$data = [];
|
|
|
|
foreach ($this->rules() as $attribute => $rules) {
|
|
$value = $this->form[$attribute] ?? null;
|
|
|
|
foreach ($rules as $rule) {
|
|
if (!$this->validateValue($value, $rule)) {
|
|
wp_die(view('invalid-token'), 'Invalid Token', ['response' => 403]);
|
|
}
|
|
|
|
$data[$attribute] = $value;
|
|
}
|
|
}
|
|
|
|
$this->validated = Collection::make($data);
|
|
}
|
|
|
|
abstract public function rules(): array;
|
|
|
|
protected function validateValue($value, $rule): bool
|
|
{
|
|
return match ($rule) {
|
|
'required' => !empty($value),
|
|
'nullable' => true,
|
|
default => false,
|
|
};
|
|
}
|
|
|
|
|
|
public function string(string $key, string $default = ''): string
|
|
{
|
|
return sanitize_text_field($this->validated->get($key, $default));
|
|
}
|
|
} |