Skip to content

Commit 17e8b80

Browse files
committed
Add request matcher
1 parent c944ccc commit 17e8b80

File tree

4 files changed

+107
-0
lines changed

4 files changed

+107
-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

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

0 commit comments

Comments
 (0)