Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/Parser/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

class Path implements ParserInterface
{

use ExplodeTrait;

/** @var Request */
Expand Down Expand Up @@ -36,17 +35,31 @@ public function __construct(Request $request, array $parameter, $route)
public function getValue()
{
$path = $this->request->getUri()->getPath();
preg_match_all("/\{(.*?)\}/", $this->route, $variableMatchersInRoute);
$key = str_replace(
'{' . $this->parameter['name'] . '}',
'(?P<' . $this->parameter['name'] . '>[^/]+)',
$this->route
);
$key = "@{$key}@";

// inject other path variables too into the final regexp key to be able to match it
if (!empty($variableMatchersInRoute[1])) {
foreach ($variableMatchersInRoute[1] as $variableMatcherInRoute) {
$key = str_replace(
'{' . $variableMatcherInRoute . '}',
'(?P<' . $variableMatcherInRoute . '>[^/]+)',
$key
);
}
}

$key = "@{$key}@";
if (!preg_match($key, $path, $pathMatches)) {
return;
}

if (!isset($pathMatches[$this->parameter['name']])) {
return null;
}
$value = $pathMatches[$this->parameter['name']];
if ($this->parameter['type'] === 'array') {
$value = $this->explodeValue($value, $this->parameter);
Expand Down
43 changes: 42 additions & 1 deletion tests/unit/src/Parser/PathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

class PathTest extends PHPUnit_Framework_TestCase
{

public function testImplementsParserInterface()
{
$mockRequest = $this->createMock(RequestInterface::class);
Expand Down Expand Up @@ -181,4 +180,46 @@ public function testGetValueReturnsMultipleValuesIfMatched()

$this->assertEquals($expectedValue, $result);
}

public function testGetValueReturnsSingleValueIfMatchedMultiplePaths()
{
$expectedValue = '1234';

$mockUri = $this->createMock(UriInterface::class);
$mockUri->method('getPath')
->willReturn("/path/{$expectedValue}/another/one");

$mockRequest = $this->createMock(RequestInterface::class);
$mockRequest->method('getUri')
->willReturn($mockUri);

$mockParameter = [
'name' => 'id',
'type' => 'string',
];
$mockRoute = '/path/{id}/another/{one}';

$reflectedPathParser = new ReflectionClass(Path::class);
$reflectedRequest = $reflectedPathParser->getProperty('request');
$reflectedRequest->setAccessible(true);
$reflectedParameter = $reflectedPathParser->getProperty('parameter');
$reflectedParameter->setAccessible(true);
$reflectedRoute = $reflectedPathParser->getProperty('route');
$reflectedRoute->setAccessible(true);

$pathParser = $this->getMockBuilder(Path::class)
->disableOriginalConstructor()
->setMethods([ 'explodeValue' ])
->getMock();
$pathParser->expects($this->never())
->method('explodeValue');

$reflectedRequest->setValue($pathParser, $mockRequest);
$reflectedParameter->setValue($pathParser, $mockParameter);
$reflectedRoute->setValue($pathParser, $mockRoute);

$result = $pathParser->getValue();

$this->assertEquals($expectedValue, $result);
}
}