method = $method ?: (\PHP_SAPI === 'cli' ? 'CLI' : ($_SERVER['REQUEST_METHOD'] ?? 'GET')); $this->compat_method = \Context::getRequestMethod(); $this->url = $url ?: \RX_REQUEST_URL; $this->hostname = $hostname ?: $_SERVER['HTTP_HOST']; $this->protocol = \RX_SSL ? 'https' : 'http'; $this->callback_function = \Context::get('js_callback_func') ?: ''; // Initialize the arguments object. $this->args = []; // Initialize route options. $this->_route_options = new \stdClass; $this->_route_options->cache_control = true; $this->_route_options->check_csrf = true; $this->_route_options->is_forwarded = false; $this->_route_options->is_indexable = true; $this->_route_options->enable_session = true; } /** * Get a request argument, optionally coerced into a type. * * @param string $name * @param string $type * @return mixed */ public function get(string $name, string $type = '') { $value = $this->args[$name] ?? null; switch ($type) { case 'int': $value = (int)$value; break; case 'float': $value = (float)$value; break; case 'bool': $value = tobool($value); break; } return $value; } /** * Get all request arguments. * * @return object */ public function getAll(): object { return (object)($this->args); } /** * Get the complete URL of this request. * * @return string */ public function getFullUrl(): string { return sprintf('%s://%s%s%s', $this->protocol, $this->hostname, \RX_BASEURL, $this->url); } /** * Get the HTTP method of this request. * * @return string */ public function getMethod(): string { return $this->method; } /** * Get the JS callback function. * * @return string */ public function getCallbackFunction(): string { return $this->callback_function; } /** * Get route status. * * @return int */ public function getRouteStatus(): int { return $this->_route_status; } /** * Set a request argument. * * @param string $name * @param string|array $value * @return void */ public function set(string $name, $value): void { if ($value === null || $value === '') { unset($this->args[$name]); } else { $this->args[$name] = $value; } } /** * Set all request arguments. * * @param array $args * @return void */ public function setAll(array $args): void { $this->args = array_filter($args, function($item) { return $item !== null && $item !== ''; }); } /** * Set route status. * * @param int $status * @return void */ public function setRouteStatus(int $status): void { $this->_route_status = $status; } /** * Get route options. * * @param string $name * @return mixed */ public function getRouteOption(string $name) { return $this->_route_options->{$name} ?? null; } /** * Get all route options. * * @return object */ public function getRouteOptions(): object { return $this->_route_options; } /** * Set route option. * * @param string $name * @param mixed $value * @return void */ public function setRouteOption(string $name, $value): void { $this->_route_options->{$name} = $value; } }