-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: url data object * url null checking * remove duplicate comment
- Loading branch information
1 parent
0771235
commit 72f99f7
Showing
2 changed files
with
228 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace System\Http; | ||
|
||
class Url | ||
{ | ||
private ?string $schema; | ||
private ?string $host; | ||
private ?int $port; | ||
private ?string $user; | ||
private ?string $password; | ||
private ?string $path; | ||
/** | ||
* @var array<int|string, string>|null | ||
*/ | ||
private ?array $query; | ||
private ?string $fragment; | ||
|
||
/** | ||
* @param array<string, string|int|array<int|string, string>|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<int|string, string> | ||
*/ | ||
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<int|string, string>|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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use System\Http\Url; | ||
|
||
class UrlTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function itUrlParse(): void | ||
{ | ||
$url = Url::parse('http://username:password@hostname:9090/path?arg=value#anchor'); | ||
|
||
$this->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()); | ||
} | ||
} |