diff options
author | Daniel Weipert <code@drogueronin.de> | 2023-11-12 11:16:56 +0100 |
---|---|---|
committer | Daniel Weipert <code@drogueronin.de> | 2023-11-20 17:30:04 +0100 |
commit | 6df3d321d9b67c4541f50158b087d37c4b22e886 (patch) | |
tree | 06031988bb4b957b76f051af366ced448a48545f /src/Request.php |
initial commit
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; + } +} |