Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## Unreleased

### Added

- Add a request matcher interface and regex implementation


## 1.0.0 - 2016-01-27

Expand Down
46 changes: 46 additions & 0 deletions spec/RequestMatcher/RegexRequestMatcherSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace spec\Http\Message\RequestMatcher;

use Http\Message\RequestMatcher;
use PhpSpec\ObjectBehavior;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;

class RegexRequestMatcherSpec extends ObjectBehavior
{
function let($regex)
{
$this->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);
}
}
26 changes: 26 additions & 0 deletions src/RequestMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Http\Message;

use Psr\Http\Message\RequestInterface;

/**
* Match a request.
*
* PSR-7 equivalent of Symfony's RequestMatcher
*
* @see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpFoundation/RequestMatcherInterface.php
*
* @author Joel Wurtz <joel.wurtz@gmail.com>
*/
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);
}
34 changes: 34 additions & 0 deletions src/RequestMatcher/RegexRequestMatcher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Http\Message\RequestMatcher;

use Http\Message\RequestMatcher;
use Psr\Http\Message\RequestInterface;

/**
* Match a request with a regex on the uri.
*
* @author Joel Wurtz <joel.wurtz@gmail.com>
*/
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());
}
}