Skip to content
This repository has been archived by the owner on Nov 8, 2020. It is now read-only.

Commit

Permalink
Added query
Browse files Browse the repository at this point in the history
  • Loading branch information
klapuch committed Oct 4, 2017
1 parent ed54bc6 commit d4e903c
Show file tree
Hide file tree
Showing 14 changed files with 108 additions and 7 deletions.
20 changes: 14 additions & 6 deletions Core/BaseUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}
7 changes: 7 additions & 0 deletions Core/CachedUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
4 changes: 4 additions & 0 deletions Core/Domain.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
8 changes: 7 additions & 1 deletion Core/FakeUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -21,4 +23,8 @@ public function reference(): string {
public function path(): string {
return $this->path;
}

public function query(): array {
return $this->query;
}
}
4 changes: 4 additions & 0 deletions Core/NormalizedUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ public function reference(): string {
public function path(): string {
return $this->origin->path();
}

public function query(): array {
return $this->origin->query();
}
}
5 changes: 5 additions & 0 deletions Core/ReachableUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions Core/RelativeUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
4 changes: 4 additions & 0 deletions Core/SchemeFoistedUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ public function reference(): string {
public function path(): string {
return $this->origin->path();
}

public function query(): array {
return $this->origin->query();
}
}
4 changes: 4 additions & 0 deletions Core/SchemeForcedUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions Core/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 5 additions & 0 deletions Core/ValidUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 24 additions & 0 deletions Tests/Unit/BaseUrl.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 8 additions & 0 deletions Tests/Unit/CachedUri.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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();
11 changes: 11 additions & 0 deletions Tests/Unit/ValidUrl.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down

0 comments on commit d4e903c

Please sign in to comment.