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

Commit

Permalink
Extracted DefaultRoute
Browse files Browse the repository at this point in the history
  • Loading branch information
klapuch committed Oct 21, 2017
1 parent 26891bd commit 89461b4
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 168 deletions.
24 changes: 24 additions & 0 deletions Core/CombinedMask.php
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();
},
[]
);
}
}
46 changes: 0 additions & 46 deletions Core/DefaultMask.php

This file was deleted.

32 changes: 32 additions & 0 deletions Core/PathMask.php
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
);
}
}
28 changes: 28 additions & 0 deletions Core/QueryMask.php
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;
}
}
31 changes: 31 additions & 0 deletions Tests/Unit/CombinedMask.phpt
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();
119 changes: 0 additions & 119 deletions Tests/Unit/DefaultMask.phpt

This file was deleted.

6 changes: 3 additions & 3 deletions Tests/Unit/MappedRoutes.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ final class MappedRoutes extends Tester\TestCase {
]
),
function(array $match): Routing\Mask {
return new Routing\DefaultMask(
return new Routing\PathMask(
key($match),
new Uri\FakeUri()
);
}
))->matches();
Assert::equal(
[
'foo/{name :int} [GET]' => new Routing\DefaultMask('foo/{name :int} [GET]', new Uri\FakeUri()),
'bar/{name :int} [GET]' => new Routing\DefaultMask('bar/{name :int} [GET]', new Uri\FakeUri()),
'foo/{name :int} [GET]' => new Routing\PathMask('foo/{name :int} [GET]', new Uri\FakeUri()),
'bar/{name :int} [GET]' => new Routing\PathMask('bar/{name :int} [GET]', new Uri\FakeUri()),
],
$routes
);
Expand Down
39 changes: 39 additions & 0 deletions Tests/Unit/PathMask.phpt
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();

0 comments on commit 89461b4

Please sign in to comment.