public function matches(Request $httpRequest)
{
$routePath = $httpRequest->getRelativePath();
$this->matchResults = null;
if ($this->uriPattern === null) {
return false;
}
if (!$this->isParsed) {
$this->parse();
}
if ($this->hasHttpMethodConstraints() && !in_array($httpRequest->getMethod(), $this->httpMethods)) {
return false;
}
$matchResults = [];
$routePath = trim($routePath, '/');
$skipOptionalParts = false;
$optionalPartCount = 0;
/** @var $routePart RoutePartInterface */
foreach ($this->routeParts as $routePart) {
if ($routePart->isOptional()) {
$optionalPartCount++;
if ($skipOptionalParts) {
if ($routePart->getDefaultValue() === null) {
return false;
}
continue;
}
} else {
$optionalPartCount = 0;
$skipOptionalParts = false;
}
if ($routePart->match($routePath) !== true) {
if ($routePart->isOptional() && $optionalPartCount === 1) {
if ($routePart->getDefaultValue() === null) {
return false;
}
$skipOptionalParts = true;
} else {
return false;
}
}
$routePartValue = $routePart->getValue();
if ($routePartValue !== null) {
if ($this->containsObject($routePartValue)) {
throw new InvalidRoutePartValueException('RoutePart::getValue() must only return simple types after calling RoutePart::match(). RoutePart "' . get_class($routePart) . '" returned one or more objects in Route "' . $this->getName() . '".');
}
$matchResults = Arrays::setValueByPath($matchResults, $routePart->getName(), $routePartValue);
}
}
if (strlen($routePath) > 0) {
return false;
}
$this->matchResults = Arrays::arrayMergeRecursiveOverrule($this->defaults, $matchResults);
return true;
}
/** * @test */ public function routeMatchesIfRequestMethodIsAccepted() { $this->route->setUriPattern(''); $this->route->setHttpMethods(['POST', 'PUT']); /** @var Request|\PHPUnit_Framework_MockObject_MockObject $mockHttpRequest */ $mockHttpRequest = $this->getMockBuilder(Http\Request::class)->disableOriginalConstructor()->getMock(); $mockUri = $this->getMockBuilder(Http\Uri::class)->disableOriginalConstructor()->getMock(); $mockUri->expects($this->any())->method('getPath')->will($this->returnValue('/')); $mockHttpRequest->expects($this->any())->method('getUri')->will($this->returnValue($mockUri)); $mockBaseUri = $this->getMockBuilder(Http\Uri::class)->disableOriginalConstructor()->getMock(); $mockBaseUri->expects($this->any())->method('getPath')->will($this->returnValue('/')); $mockHttpRequest->expects($this->any())->method('getBaseUri')->will($this->returnValue($mockBaseUri)); $mockHttpRequest->expects($this->atLeastOnce())->method('getMethod')->will($this->returnValue('PUT')); $this->assertTrue($this->route->matches($mockHttpRequest), 'Route should match PUT requests if POST and PUT requests are accepted.'); }