/** Datei: library/ARUNOX/HTTP/Request.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\HTTP; class Request { private array $query; private array $body; private array $files; private array $headers; private string $method; private ?string $contentType; private ?array $referer; private string $uri; private string $pathname; public function __construct() { $this->headers = getallheaders() ?: []; $this->method = $_SERVER['REQUEST_METHOD'] ?? 'GET'; $this->contentType = $_SERVER["CONTENT_TYPE"] ?? NULL; $this->query = $_GET ?? []; $this->uri = strtok($_SERVER['REQUEST_URI'] ?? '/', '?'); // URI ohne Query-String $this->referer = parse_url_full( $this->getHeaders("Referer") ); $this->pathname = $this->parsePathname(); [ $this->body, $this->files ] = $this->parseBody(); } /** * Gibt die Query-Parameter zurück. * * @return array */ public function getQueryParams( ?string $name = NULL, mixed $default = NULL ): null|array|string { return $name ? ( $this->query[ $name ] ?? $default ) : $this->query; } /** * Gibt die Body-Parameter zurück (JSON). * * @return array */ public function getBodyParams(): array { return $this->method == "GET" ? $this->query : $this->body; } /** * Gibt die Header der Anfrage zurück. * * @return array */ public function getHeaders( ?string $name = NULL ) : null|array|string { return $name ? ( $this->headers[ $name ] ?? "" ) : $this->headers; } public function getReferer( ?string $name = NULL ) : array|string { return $name ? $this->referer[ $name ] : $this->referer; } /** * Gibt die HTTP-Methode zurück. * * @return string */ public function getMethod(): string { return $this->method; } /** * Gibt die angeforderte URI ohne Query-String zurück. * * @return string */ public function getUri( string $prefix = "" ): string { return $this->uri; } public function getPathname(): string { return $this->pathname; } private function parsePathname(): string { if( preg_match( "/(\/api\/v[0-9]+)\/?(.*)/", $this->uri, $matches ) ) { return $matches[2]; } return $this->uri; } /** * Parst den Anfrage-Body. * * @return array */ private function parseBody(): array { if( !empty( $_POST ) || !empty( $_FILES ) ) { return [ $_POST, $_FILES ]; } if( $this->method === "GET" || !$this->contentType ) return [ [], [] ]; if( $this->contentType === "application/json" ) { $input = file_get_contents('php://input'); $decoded = json_decode( $input, true ); return [ $decoded, [] ]; } return [ $_POST, $_FILES ] = request_parse_body(); } public static function __callStatic( string $name, array $args ) { $that = new self(); return $that->{"get{$name}"}( ...$args ); } } /** Datei: library/ARUNOX/HTTP/Response.php **/ status = $status; $this->headers = $headers; $this->body = $body; } /* ### SETTERS ### */ public function withStatus(int $status): self { $this->status = $status; return $this; } public function withHeader(string $name, string $value): self { $this->headers[$name] = $value; return $this; } public function withBody(mixed $body): self { $this->body = $body; return $this; } /* ### GETTERS ### */ public function hasError() : bool { return $this->status >= 400; } public function isStatus( int $code ) : bool { return $this->status === $code; } public function getStatus() : int { return $this->status; } public function getHeaders(): array { return $this->headers; } public function getBody( ...$names ) : mixed { $data = $this->body; if( $names[0] == "*" ) { return json_decode( $data, TRUE ); } foreach( $names as $name ) { $data = $this->getData( $name, $data ); } return $data; } private function getData( string $name, mixed $data ) : mixed { if( is_json( $data ) ) $data = json_decode( $data ); if( is_array( $data ) ) return $data[ $name ] ?? NULL; if( is_object( $data ) ) return $data->{$name} ?? NULL; return $data; } /* ### OUTPUT ### */ public function send(): void { http_response_code($this->status); foreach ($this->headers as $name => $value) { header("{$name}: {$value}"); } if( $this->body !== null ) { if( is_scalar( $this->body ) ) { echo $this->body; } else { echo json_encode( $this->body ); } } exit; } }