From d4e903cc77a933a33da5c1313975765405c5bf6e Mon Sep 17 00:00:00 2001 From: Dominik Klapuch Date: Wed, 4 Oct 2017 21:56:50 +0200 Subject: [PATCH] Added query --- Core/BaseUrl.php | 20 ++++++++++++++------ Core/CachedUri.php | 7 +++++++ Core/Domain.php | 4 ++++ Core/FakeUri.php | 8 +++++++- Core/NormalizedUrl.php | 4 ++++ Core/ReachableUrl.php | 5 +++++ Core/RelativeUrl.php | 4 ++++ Core/SchemeFoistedUrl.php | 4 ++++ Core/SchemeForcedUrl.php | 4 ++++ Core/Uri.php | 7 +++++++ Core/ValidUrl.php | 5 +++++ Tests/Unit/BaseUrl.phpt | 24 ++++++++++++++++++++++++ Tests/Unit/CachedUri.phpt | 8 ++++++++ Tests/Unit/ValidUrl.phpt | 11 +++++++++++ 14 files changed, 108 insertions(+), 7 deletions(-) diff --git a/Core/BaseUrl.php b/Core/BaseUrl.php index fb7c084..b1f6810 100644 --- a/Core/BaseUrl.php +++ b/Core/BaseUrl.php @@ -26,13 +26,15 @@ public function __construct( } public function reference(): string { - return $this->toFullyQualified( + return $this->absolute( implode( self::DELIMITER, $this->withoutExecutedScript( explode(self::DELIMITER, $this->script) ) - ) + ), + $this->scheme, + $this->host ); } @@ -50,6 +52,12 @@ public function path(): string { return self::EMPTY; } + public function query(): array { + return (new ValidUrl( + $this->absolute($this->url, $this->scheme, $this->host) + ))->query(); + } + /** * Parts of the url without index.php or other file where is script executed * @param array $parts @@ -78,10 +86,10 @@ private function withoutTrailingSlashes(array $parts): array { return array_filter($parts, 'strlen'); } - private function toFullyQualified(string $base): string { - $scheme = $this->scheme && $this->host - ? preg_replace('~[^a-z]~i', '', $this->scheme) . '://' + private function absolute(string $base, string $scheme, string $host): string { + $scheme = $scheme && $host + ? preg_replace('~[^a-z]~i', '', $scheme) . '://' : self::EMPTY; - return $scheme . $this->host . $base; + return $scheme . $host . $base; } } \ No newline at end of file diff --git a/Core/CachedUri.php b/Core/CachedUri.php index 4d1b920..12d8173 100644 --- a/Core/CachedUri.php +++ b/Core/CachedUri.php @@ -9,6 +9,7 @@ final class CachedUri implements Uri { private $origin; private $reference; private $path; + private $query; public function __construct(Uri $origin) { $this->origin = $origin; @@ -25,4 +26,10 @@ public function path(): string { $this->path = $this->origin->path(); return $this->path; } + + public function query(): array { + if ($this->query === null) + $this->query = $this->origin->query(); + return $this->query; + } } \ No newline at end of file diff --git a/Core/Domain.php b/Core/Domain.php index 46659c9..e2827b1 100644 --- a/Core/Domain.php +++ b/Core/Domain.php @@ -22,4 +22,8 @@ public function reference(): string { $host = parse_url($url, PHP_URL_HOST); return $scheme . '://' . $host; } + + public function query(): array { + return $this->origin->query(); + } } diff --git a/Core/FakeUri.php b/Core/FakeUri.php index 63f21ae..84c0afd 100644 --- a/Core/FakeUri.php +++ b/Core/FakeUri.php @@ -8,10 +8,12 @@ final class FakeUri implements Uri { private $uri; private $path; + private $query; - public function __construct(string $uri = null, string $path = null) { + public function __construct(string $uri = null, string $path = null, array $query = null) { $this->uri = $uri; $this->path = $path; + $this->query = $query; } public function reference(): string { @@ -21,4 +23,8 @@ public function reference(): string { public function path(): string { return $this->path; } + + public function query(): array { + return $this->query; + } } diff --git a/Core/NormalizedUrl.php b/Core/NormalizedUrl.php index 23191dd..cd5b483 100644 --- a/Core/NormalizedUrl.php +++ b/Core/NormalizedUrl.php @@ -27,4 +27,8 @@ public function reference(): string { public function path(): string { return $this->origin->path(); } + + public function query(): array { + return $this->origin->query(); + } } diff --git a/Core/ReachableUrl.php b/Core/ReachableUrl.php index 8e21249..e16725c 100644 --- a/Core/ReachableUrl.php +++ b/Core/ReachableUrl.php @@ -24,10 +24,15 @@ public function reference(): string { ) ); } + public function path(): string { return $this->origin->path(); } + public function query(): array { + return $this->origin->query(); + } + /** * Is the URL reachable? * @return bool diff --git a/Core/RelativeUrl.php b/Core/RelativeUrl.php index dec3950..1ce98f6 100644 --- a/Core/RelativeUrl.php +++ b/Core/RelativeUrl.php @@ -22,4 +22,8 @@ public function reference(): string { public function path(): string { return trim($this->path, self::DELIMITER); } + + public function query(): array { + return $this->origin->query(); + } } \ No newline at end of file diff --git a/Core/SchemeFoistedUrl.php b/Core/SchemeFoistedUrl.php index 6eb2495..b74780e 100644 --- a/Core/SchemeFoistedUrl.php +++ b/Core/SchemeFoistedUrl.php @@ -24,4 +24,8 @@ public function reference(): string { public function path(): string { return $this->origin->path(); } + + public function query(): array { + return $this->origin->query(); + } } diff --git a/Core/SchemeForcedUrl.php b/Core/SchemeForcedUrl.php index f39073f..8b2386c 100644 --- a/Core/SchemeForcedUrl.php +++ b/Core/SchemeForcedUrl.php @@ -30,6 +30,10 @@ public function path(): string { return $this->origin->path(); } + public function query(): array { + return $this->origin->query(); + } + /** * Schemes transferred to human readable form * @param array $schemes diff --git a/Core/Uri.php b/Core/Uri.php index aa37526..759447c 100644 --- a/Core/Uri.php +++ b/Core/Uri.php @@ -16,4 +16,11 @@ public function path(): string; * @return string */ public function reference(): string; + + /** + * Query parameters from URI + * @throws \InvalidArgumentException + * @return array + */ + public function query(): array; } diff --git a/Core/ValidUrl.php b/Core/ValidUrl.php index b1dd803..38313cc 100644 --- a/Core/ValidUrl.php +++ b/Core/ValidUrl.php @@ -24,6 +24,11 @@ public function reference(): string { ); } + public function query(): array { + parse_str((string) parse_url($this->reference(), PHP_URL_QUERY), $query); + return $query; + } + /** * Is the given url valid? * @return bool diff --git a/Tests/Unit/BaseUrl.phpt b/Tests/Unit/BaseUrl.phpt index 0bd9438..af3b978 100644 --- a/Tests/Unit/BaseUrl.phpt +++ b/Tests/Unit/BaseUrl.phpt @@ -66,6 +66,30 @@ final class BaseUrl extends Tester\TestCase { ); } + public function testExtractingQuery() { + Assert::same( + ['name' => 'Dom', 'age' => '21'], + (new Uri\BaseUrl( + '/Acme/www/index.php', + '/Acme/www/a/b/c/?name=Dom&age=21', + 'localhost', + 'https' + ))->query() + ); + } + + public function testNoQueryLeadingToEmptyArray() { + Assert::same( + [], + (new Uri\BaseUrl( + '/Acme/www/index.php', + '/Acme/www/a/b/c', + 'localhost', + 'https' + ))->query() + ); + } + public function testWithScheme() { Assert::same( 'http://localhost/foo', diff --git a/Tests/Unit/CachedUri.phpt b/Tests/Unit/CachedUri.phpt index 1f52aae..6cfd269 100644 --- a/Tests/Unit/CachedUri.phpt +++ b/Tests/Unit/CachedUri.phpt @@ -31,6 +31,14 @@ final class CachedUri extends Tester\TestCase { Assert::same('/home', $uri->path()); Assert::same('/home', $uri->path()); } + + public function testCallingQueryJustOnce() { + $origin = $this->mock(Uri\Uri::class); + $origin->shouldReceive('query')->once()->andReturn(['abc']); + $uri = new Uri\CachedUri($origin); + Assert::same(['abc'], $uri->query()); + Assert::same(['abc'], $uri->query()); + } } (new CachedUri())->run(); \ No newline at end of file diff --git a/Tests/Unit/ValidUrl.phpt b/Tests/Unit/ValidUrl.phpt index 26a35c7..c0f5a89 100755 --- a/Tests/Unit/ValidUrl.phpt +++ b/Tests/Unit/ValidUrl.phpt @@ -50,6 +50,17 @@ final class ValidUrl extends Tester\TestCase { ); } + public function testExtractingQuery() { + Assert::same( + ['name' => 'Dom', 'age' => '21'], + (new Uri\ValidUrl('https://www.google.com/?name=Dom&age=21'))->query() + ); + } + + public function testNoQueryLeadingToEmptyArray() { + Assert::same([], (new Uri\ValidUrl('https://www.google.com'))->query()); + } + protected function validReferences() { return [ ['http://www.google.com'],