Skip to content

Commit 62e9ff4

Browse files
committed
Merge pull request #32 from php-http/feature/request-matcher
Add request matcher
2 parents c944ccc + e792553 commit 62e9ff4

File tree

4 files changed

+112
-0
lines changed

4 files changed

+112
-0
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log
22

3+
## Unreleased
4+
5+
### Added
6+
7+
- Add a request matcher interface and regex implementation
8+
39

410
## 1.0.0 - 2016-01-27
511

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace spec\Http\Message\RequestMatcher;
4+
5+
use Http\Message\RequestMatcher;
6+
use PhpSpec\ObjectBehavior;
7+
use Psr\Http\Message\RequestInterface;
8+
use Psr\Http\Message\UriInterface;
9+
10+
class RegexRequestMatcherSpec extends ObjectBehavior
11+
{
12+
function let($regex)
13+
{
14+
$this->beConstructedWith($regex);
15+
}
16+
17+
function it_is_a_request_matcher()
18+
{
19+
$this->shouldImplement('Http\Message\RequestMatcher');
20+
}
21+
22+
function it_is_initializable()
23+
{
24+
$this->shouldHaveType('Http\Message\RequestMatcher\RegexRequestMatcher');
25+
}
26+
27+
function it_matches(RequestInterface $request, UriInterface $uri)
28+
{
29+
$this->beConstructedWith('/test/');
30+
31+
$request->getUri()->willReturn($uri);
32+
$uri->__toString()->willReturn('/test');
33+
34+
$this->matches($request)->shouldReturn(true);
35+
}
36+
37+
function it_does_not_match(RequestInterface $request, UriInterface $uri)
38+
{
39+
$this->beConstructedWith('/test/');
40+
41+
$request->getUri()->willReturn($uri);
42+
$uri->__toString()->willReturn('/ttttt');
43+
44+
$this->matches($request)->shouldReturn(false);
45+
}
46+
}

src/RequestMatcher.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Http\Message;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
7+
/**
8+
* Match a request.
9+
*
10+
* PSR-7 equivalent of Symfony's RequestMatcher
11+
*
12+
* @see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/RequestMatcherInterface.php
13+
*
14+
* @author Joel Wurtz <[email protected]>
15+
*/
16+
interface RequestMatcher
17+
{
18+
/**
19+
* Decides whether the rule(s) implemented by the strategy matches the supplied request.
20+
*
21+
* @param RequestInterface $request The PSR7 request to check for a match
22+
*
23+
* @return bool true if the request matches, false otherwise
24+
*/
25+
public function matches(RequestInterface $request);
26+
}
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Http\Message\RequestMatcher;
4+
5+
use Http\Message\RequestMatcher;
6+
use Psr\Http\Message\RequestInterface;
7+
8+
/**
9+
* Match a request with a regex on the uri.
10+
*
11+
* @author Joel Wurtz <[email protected]>
12+
*/
13+
class RegexRequestMatcher implements RequestMatcher
14+
{
15+
/**
16+
* Matching regex.
17+
*
18+
* @var string
19+
*/
20+
private $regex;
21+
22+
public function __construct($regex)
23+
{
24+
$this->regex = $regex;
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function matches(RequestInterface $request)
31+
{
32+
return (bool) preg_match($this->regex, (string) $request->getUri());
33+
}
34+
}

0 commit comments

Comments
 (0)