Skip to content

Commit

Permalink
Add Parameter Type {raw} (Fix #56)
Browse files Browse the repository at this point in the history
  • Loading branch information
cundd committed Apr 6, 2019
1 parent 8aceb0b commit 3047fad
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Classes/Router/ParameterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ private static function createParameter($input)
case 'string':
return ParameterTypeInterface::SLUG;

case 'raw':
return ParameterTypeInterface::RAW;

case 'float':
case 'double':
case 'number':
Expand Down
1 change: 1 addition & 0 deletions Classes/Router/ParameterTypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ interface ParameterTypeInterface
const FLOAT = 'float';
const SLUG = 'slug';
const BOOLEAN = 'boolean';
const RAW = 'raw';
}
18 changes: 16 additions & 2 deletions Classes/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ class Router implements RouterInterface
*/
public function dispatch(RestRequestInterface $request)
{
$parameters = $this->getPreparedParameters($request);
$route = $this->getMatchingRoute($request);

if (!$route) {
return new NotFoundException();
}

$parameters = $this->getPreparedParametersForRoute($request, $route);

return $route->process($request, ...$parameters);
}

Expand Down Expand Up @@ -148,6 +148,18 @@ public function getPreparedParameters(RestRequestInterface $request)
return [];
}

return $this->getPreparedParametersForRoute($request, $route);
}

/**
* Returns the prepared parameters
*
* @param RestRequestInterface $request
* @param RouteInterface $route
* @return array
*/
private function getPreparedParametersForRoute(RestRequestInterface $request, RouteInterface $route)
{
$segments = explode('/', $request->getPath());
$parameters = [];
foreach ($route->getParameters() as $index => $type) {
Expand All @@ -167,6 +179,7 @@ public function getPreparedParameters(RestRequestInterface $request)
private function getPreparedParameter($type, $segment)
{
switch ($type) {
case ParameterTypeInterface::RAW:
case ParameterTypeInterface::SLUG:
return (string)$segment;
case ParameterTypeInterface::BOOLEAN:
Expand All @@ -188,6 +201,7 @@ private function patternToRegularExpression($pattern)
{
$outputPattern = $pattern;
$parameterTypeToRegex = [
ParameterTypeInterface::RAW => '[^/]+',
ParameterTypeInterface::SLUG => '[a-zA-Z0-9\._\-]+',
ParameterTypeInterface::INTEGER => '[0-9]+',
ParameterTypeInterface::FLOAT => '[0-9]+\.[0-9]+',
Expand Down
15 changes: 15 additions & 0 deletions Documentation/Customize.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,21 @@ Route::get($request->getResourceType() . '/{boolean}', function(RestRequestInter
```


### Matching anything aka. `raw`

Extracts the value from segments matching the regular expression `[^/]+`

```php
Route::get($request->getResourceType() . '/{raw}', function(RestRequestInterface $request, $theParameter) {
// Callback will be invoked for
# curl -X GET http://localhost:8888/rest/customhandler/Mr Müller
// with $theParameter set to 'Mr%20Müller'
});
```

> The parameter value will not be decoded before being passed to the route callback

Response
--------

Expand Down
13 changes: 13 additions & 0 deletions Tests/Unit/Router/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,13 @@ public function patternsWithExpressionsHaveLowerPriorityDataProvider()
['path/{string}/{float}/1', 'path/sub-path/another/1'],
['path/sub-path/{string}/1/2/path/item/x', 'path/sub-path/another/1/2/path/item/x'],
['path/sub-path/{string}/1/2/path/item/x/y', 'path/sub-path/another/1/2/path/item/x/y'],
['path/sub-path/{raw}/1/2/path/item/x/y', 'path/sub-path/another/1/2/path/item/x/y'],

['path/sub-path/{string}/{int}/2/path/item/x', 'path/sub-path/{string}/1/2/path/item/x'],
['path/sub-path/{string}/{int}/2/path/item/x/y', 'path/sub-path/{string}/1/2/path/item/x/y'],

['path/sub-path/{raw}/{int}/2/path/item/x', 'path/sub-path/{string}/1/2/path/item/x'],
['path/sub-path/{raw}/{int}/2/path/item/x/y', 'path/sub-path/{string}/1/2/path/item/x/y'],
];
}

Expand Down Expand Up @@ -289,6 +293,15 @@ public function getParametersDataProvider()
'path/sub-path/{string}/{int}/2/path/item/x/y',
[ParameterTypeInterface::SLUG, ParameterTypeInterface::INTEGER],
],

[
'path/sub-path/{string}/{raw}/2/path/item/x',
[ParameterTypeInterface::SLUG, ParameterTypeInterface::RAW],
],
[
'path/sub-path/{string}/{raw}/2/path/item/x/y',
[ParameterTypeInterface::SLUG, ParameterTypeInterface::RAW],
],
];
}

Expand Down
24 changes: 24 additions & 0 deletions Tests/Unit/Router/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,30 @@ public function getPreparedParametersDataProvider()
['path/{slug}/sub-path/{float}/{bool}/{int}/?', '/slug/sub-path/1.0/no/9', [], true],
['path/{slug}/sub-path/{float}/{bool}/{int}/?', '/', [], true],
['path/{slug}/sub-path/{float}/{bool}/{int}/?', '', [], true],
[
'path/{raw}/?',
'/path/here could be änything but a slash',
['here%20could%20be%20änything%20but%20a%20slash'],
false,
],
[
'path/{raw}/?',
'/path/here could be änything but a slash/',
['here%20could%20be%20änything%20but%20a%20slash'],
false,
],
[
'path/{raw}/{int}/?',
'/path/here could be änything but a slash/1',
['here%20could%20be%20änything%20but%20a%20slash', 1],
false,
],
[
'path/{raw}/?',
'/path/Mr Müller/',
['Mr%20Müller'],
false,
],
];
}

Expand Down

0 comments on commit 3047fad

Please sign in to comment.