url = parse_url($_SERVER['REQUEST_URI']); $this->form = array_merge($_GET, $_POST); } public function isRequestForThisRoute(): bool { return trim($this->getCurrentRoutePath(), '/') === $this->getRequestRoutePath(); } abstract public function getRequestRoutePath(): string; public function getCurrentRoutePath(): string { $site = parse_url(get_option('siteurl')); return str_replace($site['path'] ?? '', null, $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)) { throw new UnexpectedValueException( sprintf('The request field "%s" failed the "%s" validation rule.', $attribute, $rule), 403 ); } $data[$attribute] = $value; } } $this->validated = Collection::make($data); } abstract public function rules(): array; protected function validateValue($value, $rule): bool { switch ($rule) { case 'required': return !empty($value); case 'nullable': return true; default: return false; } } public function string(string $key, string $default = ''): string { return sanitize_text_field($this->validated->get($key, $default)); } }