This repository has been archived by the owner on Nov 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
246 additions
and
168 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,24 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
namespace Klapuch\Routing; | ||
|
||
/** | ||
* Mask behaving as a huge single one | ||
*/ | ||
final class CombinedMask implements Mask { | ||
private $origins; | ||
|
||
public function __construct(Mask ...$origins) { | ||
$this->origins = $origins; | ||
} | ||
|
||
public function parameters(): array { | ||
return array_reduce( | ||
$this->origins, | ||
function(array $merge, Mask $origin) { | ||
return $merge + $origin->parameters(); | ||
}, | ||
[] | ||
); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,32 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
namespace Klapuch\Routing; | ||
|
||
use Klapuch\Uri; | ||
|
||
/** | ||
* Mask for path | ||
*/ | ||
final class PathMask implements Mask { | ||
private const SEPARATOR = '/'; | ||
private $source; | ||
private $uri; | ||
|
||
public function __construct(string $source, Uri\Uri $uri) { | ||
$this->source = $source; | ||
$this->uri = $uri; | ||
} | ||
|
||
public function parameters(): array { | ||
$sources = explode(self::SEPARATOR, parse_url($this->source, PHP_URL_PATH)); | ||
$parameters = array_diff(explode(self::SEPARATOR, $this->uri->path()), $sources); | ||
return array_combine( | ||
preg_replace( | ||
'~{|}|\s\S+~', | ||
'', | ||
array_intersect_key($sources, $parameters) | ||
), | ||
$parameters | ||
); | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
namespace Klapuch\Routing; | ||
|
||
use Klapuch\Uri; | ||
|
||
/** | ||
* Mask for query | ||
*/ | ||
final class QueryMask implements Mask { | ||
private $source; | ||
private $uri; | ||
|
||
public function __construct(string $source, Uri\Uri $uri) { | ||
$this->source = $source; | ||
$this->uri = $uri; | ||
} | ||
|
||
public function parameters(): array { | ||
parse_str((string) parse_url($this->source, PHP_URL_QUERY), $query); | ||
return $this->uri->query() + array_map( | ||
function(string $part): string { | ||
return current(explode(' ', substr($part, 1, -1), 2)); | ||
}, | ||
preg_grep('~^\(.*\)$~', $query) | ||
) + $query; | ||
} | ||
} |
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,31 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
/** | ||
* @testCase | ||
* @phpVersion > 7.1 | ||
*/ | ||
namespace Klapuch\Routing\Unit; | ||
|
||
use Klapuch\Routing; | ||
use Tester; | ||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
final class CombinedMask extends Tester\TestCase { | ||
public function testPrecedenceByPassedPosition() { | ||
Assert::same( | ||
['name' => 'dom', 'position' => 'developer', 'age' => 21], | ||
(new Routing\CombinedMask( | ||
new Routing\FakeMask(['name' => 'dom']), | ||
new Routing\FakeMask(['name' => 'xxx']), | ||
new Routing\FakeMask(['position' => 'developer']), | ||
new Routing\FakeMask(['position' => 'xxx']), | ||
new Routing\FakeMask(['age' => 21]) | ||
))->parameters() | ||
); | ||
} | ||
} | ||
|
||
|
||
(new CombinedMask())->run(); |
This file was deleted.
Oops, something went wrong.
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
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,39 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
/** | ||
* @testCase | ||
* @phpVersion > 7.1 | ||
*/ | ||
namespace Klapuch\Routing\Unit; | ||
|
||
use Klapuch\Routing; | ||
use Klapuch\Uri; | ||
use Tester; | ||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
final class PathMask extends Tester\TestCase { | ||
public function testNamesByPosition() { | ||
Assert::same( | ||
['name' => 'dom', 'position' => 'developer'], | ||
(new Routing\PathMask( | ||
'/books/{name}/{position}', | ||
new Uri\FakeUri(null, '/books/dom/developer', []) | ||
))->parameters() | ||
); | ||
} | ||
|
||
public function testRemovingRegexParts() { | ||
Assert::same( | ||
['foo' => '10', 'adjective' => 'cool', 'word' => 'bar'], | ||
(new Routing\PathMask( | ||
'/books/{foo \d+}/{adjective \w+}/{word}', | ||
new Uri\FakeUri(null, '/books/10/cool/bar', []) | ||
))->parameters() | ||
); | ||
} | ||
} | ||
|
||
|
||
(new PathMask())->run(); |
Oops, something went wrong.