From 72f99f70e5aeecfd96eb877d50554d114311a303 Mon Sep 17 00:00:00 2001 From: Angger Pradana Date: Sat, 21 Oct 2023 06:28:23 +0700 Subject: [PATCH] [Feat] Http url data object (#225) * feat: url data object * url null checking * remove duplicate comment --- src/System/Http/Url.php | 158 ++++++++++++++++++++++++++++++++++++++++ tests/Http/UrlTest.php | 70 ++++++++++++++++++ 2 files changed, 228 insertions(+) create mode 100644 src/System/Http/Url.php create mode 100644 tests/Http/UrlTest.php diff --git a/src/System/Http/Url.php b/src/System/Http/Url.php new file mode 100644 index 00000000..2781dec1 --- /dev/null +++ b/src/System/Http/Url.php @@ -0,0 +1,158 @@ +|null + */ + private ?array $query; + private ?string $fragment; + + /** + * @param array|null> $parse_url + */ + public function __construct(array $parse_url) + { + $this->schema = $parse_url['scheme'] ?? null; + $this->host = $parse_url['host'] ?? null; + $this->port = $parse_url['port'] ?? null; + $this->user = $parse_url['user'] ?? null; + $this->password = $parse_url['pass'] ?? null; + $this->path = $parse_url['path'] ?? null; + $this->fragment = $parse_url['fragment'] ?? null; + + if (array_key_exists('query', $parse_url)) { + $this->query = $this->parseQuery($parse_url['query']); + } + } + + /** + * @return array + */ + private function parseQuery(string $query): array + { + $result = []; + parse_str($query, $result); + + return $result; + } + + public static function parse(string $url): self + { + return new self(parse_url($url)); + } + + /** + * @return string|null + */ + public function schema() + { + return $this->schema; + } + + /** + * @return string|null + */ + public function host() + { + return $this->host; + } + + /** + * @return int|null + */ + public function port() + { + return $this->port; + } + + /** + * @return string|null + */ + public function user() + { + return $this->user; + } + + /** + * @return string|null + */ + public function password() + { + return $this->password; + } + + /** + * @return string|null + */ + public function path() + { + return $this->path; + } + + /** + * @return array|null + */ + public function query() + { + return $this->query; + } + + /** + * @return string|null + */ + public function fragment() + { + return $this->fragment; + } + + public function hasSchema(): bool + { + return null !== $this->schema; + } + + public function hasHost(): bool + { + return null !== $this->host; + } + + public function hasPort(): bool + { + return null !== $this->port; + } + + public function hasUser(): bool + { + return null !== $this->user; + } + + public function hasPassword(): bool + { + return null !== $this->password; + } + + public function hasPath(): bool + { + return null !== $this->path; + } + + public function hasQuery(): bool + { + return null !== $this->query; + } + + public function hasFragment(): bool + { + return null !== $this->fragment; + } +} diff --git a/tests/Http/UrlTest.php b/tests/Http/UrlTest.php new file mode 100644 index 00000000..1a2d431d --- /dev/null +++ b/tests/Http/UrlTest.php @@ -0,0 +1,70 @@ +assertEquals('http', $url->schema()); + $this->assertEquals('hostname', $url->host()); + $this->assertEquals(9090, $url->port()); + $this->assertEquals('username', $url->user()); + $this->assertEquals('password', $url->password()); + $this->assertEquals('/path', $url->path()); + $this->assertEquals(['arg' => 'value'], $url->query()); + $this->assertEquals('anchor', $url->fragment()); + } + + /** + * @test + */ + public function itUrlParseMissingSchema(): void + { + $url = Url::parse('//www.example.com/path?googleguy=googley'); + + $this->assertEquals('www.example.com', $url->host()); + $this->assertEquals('/path', $url->path()); + $this->assertEquals(['googleguy' => 'googley'], $url->query()); + } + + /** + * @test + */ + public function itCanCheckUrlParse(): void + { + $url = Url::parse('http://username:password@hostname:9090/path?arg=value#anchor'); + + $this->assertTrue($url->hasSchema()); + $this->assertTrue($url->hasHost()); + $this->assertTrue($url->hasPort()); + $this->assertTrue($url->hasUser()); + $this->assertTrue($url->hasPassword()); + $this->assertTrue($url->hasPath()); + $this->assertTrue($url->hasQuery()); + $this->assertTrue($url->hasFragment()); + } + + /** + * @test + */ + public function itCanCheckUrlParseMissingSchema(): void + { + $url = Url::parse('//www.example.com/path?googleguy=googley'); + + $this->assertFalse($url->hasSchema()); + $this->assertTrue($url->hasHost()); + $this->assertFalse($url->hasPort()); + $this->assertFalse($url->hasUser()); + $this->assertFalse($url->hasPassword()); + $this->assertTrue($url->hasPath()); + $this->assertTrue($url->hasQuery()); + $this->assertFalse($url->hasFragment()); + } +}