File tree 4 files changed +112
-0
lines changed
4 files changed +112
-0
lines changed Original file line number Diff line number Diff line change 1
1
# Change Log
2
2
3
+ ## Unreleased
4
+
5
+ ### Added
6
+
7
+ - Add a request matcher interface and regex implementation
8
+
3
9
4
10
## 1.0.0 - 2016-01-27
5
11
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments