From e79255399c20686fabaf2cd5bbc72473776b4a24 Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Fri, 19 Feb 2016 14:46:47 +0100 Subject: [PATCH] Add request matcher --- CHANGELOG.md | 6 +++ .../RegexRequestMatcherSpec.php | 46 +++++++++++++++++++ src/RequestMatcher.php | 26 +++++++++++ src/RequestMatcher/RegexRequestMatcher.php | 34 ++++++++++++++ 4 files changed, 112 insertions(+) create mode 100644 spec/RequestMatcher/RegexRequestMatcherSpec.php create mode 100644 src/RequestMatcher.php create mode 100644 src/RequestMatcher/RegexRequestMatcher.php diff --git a/CHANGELOG.md b/CHANGELOG.md index a247f9b..0bec23b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Change Log +## Unreleased + +### Added + +- Add a request matcher interface and regex implementation + ## 1.0.0 - 2016-01-27 diff --git a/spec/RequestMatcher/RegexRequestMatcherSpec.php b/spec/RequestMatcher/RegexRequestMatcherSpec.php new file mode 100644 index 0000000..3f07503 --- /dev/null +++ b/spec/RequestMatcher/RegexRequestMatcherSpec.php @@ -0,0 +1,46 @@ +beConstructedWith($regex); + } + + function it_is_a_request_matcher() + { + $this->shouldImplement('Http\Message\RequestMatcher'); + } + + function it_is_initializable() + { + $this->shouldHaveType('Http\Message\RequestMatcher\RegexRequestMatcher'); + } + + function it_matches(RequestInterface $request, UriInterface $uri) + { + $this->beConstructedWith('/test/'); + + $request->getUri()->willReturn($uri); + $uri->__toString()->willReturn('/test'); + + $this->matches($request)->shouldReturn(true); + } + + function it_does_not_match(RequestInterface $request, UriInterface $uri) + { + $this->beConstructedWith('/test/'); + + $request->getUri()->willReturn($uri); + $uri->__toString()->willReturn('/ttttt'); + + $this->matches($request)->shouldReturn(false); + } +} diff --git a/src/RequestMatcher.php b/src/RequestMatcher.php new file mode 100644 index 0000000..94fe532 --- /dev/null +++ b/src/RequestMatcher.php @@ -0,0 +1,26 @@ + + */ +interface RequestMatcher +{ + /** + * Decides whether the rule(s) implemented by the strategy matches the supplied request. + * + * @param RequestInterface $request The PSR7 request to check for a match + * + * @return bool true if the request matches, false otherwise + */ + public function matches(RequestInterface $request); +} diff --git a/src/RequestMatcher/RegexRequestMatcher.php b/src/RequestMatcher/RegexRequestMatcher.php new file mode 100644 index 0000000..1560718 --- /dev/null +++ b/src/RequestMatcher/RegexRequestMatcher.php @@ -0,0 +1,34 @@ + + */ +class RegexRequestMatcher implements RequestMatcher +{ + /** + * Matching regex. + * + * @var string + */ + private $regex; + + public function __construct($regex) + { + $this->regex = $regex; + } + + /** + * {@inheritdoc} + */ + public function matches(RequestInterface $request) + { + return (bool) preg_match($this->regex, (string) $request->getUri()); + } +}