diff options
Diffstat (limited to 'src/Request.php')
-rw-r--r-- | src/Request.php | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/Request.php b/src/Request.php new file mode 100644 index 0000000..586134d --- /dev/null +++ b/src/Request.php @@ -0,0 +1,48 @@ +<?php + +namespace GeminiFoundation; + +class Request +{ + protected string $scheme; + protected string $host; + protected string $path; + protected array $query; + + public function __construct(string $url) + { + $requestUrl = parse_url($url); + + $this->scheme = $requestUrl['scheme']; + $this->host = $requestUrl['host']; + $this->path = $requestUrl['path'] ?? '/'; + + $this->query = []; + if (isset($requestUrl['query'])) { + foreach (explode('&', $requestUrl['query']) as $queryString) { + $query = explode('=', $queryString); + $this->query[$query[0]] = $query[1] ?? null; + } + } + } + + /** + * @param resource $resource + */ + public static function fromResource($resource): static + { + return Request::fromString(fread($resource, 1024)); + } + + public static function fromString(string $string): static + { + $requestUrl = explode("\r\n", $string)[0] ?? ''; + + return new Request($requestUrl); + } + + public function getPath(): string + { + return $this->path; + } +} |