Skip to content

Instantly share code, notes, and snippets.

@mageekguy
Created April 23, 2015 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mageekguy/6d68dc736f19108732a6 to your computer and use it in GitHub Desktop.
Save mageekguy/6d68dc736f19108732a6 to your computer and use it in GitHub Desktop.
HTTP Request matcher
<?php
namespace east\request;
interface http
{
function methodIsAskedByHttpRequestMatcher(http\matcher $matcher);
function pathIsAskedByHttpRequestMatcher(http\matcher $matcher);
}
namespace east\request\http;
use east\request;
interface matcher
{
function httpRequestMethodIs(method $method);
function httpRequestPathIs(path $path);
}
final class method
{
private $value;
function __construct($value)
{
$this->value = $value;
}
}
final class path
{
private $value;
function __construct($value)
{
$this->value = $value;
}
}
class httpRequest implements request\http
{
private
$method,
$path
;
function __construct(method $method, path $path)
{
$this->method = $method;
$this->path = $path;
}
function methodIsAskedByHttpRequestMatcher(matcher $matcher)
{
$matcher->httpRequestMethodIs($this->method);
return $this;
}
function pathIsAskedByHttpRequestMatcher(matcher $matcher)
{
$matcher->httpRequestPathIs($this->path);
return $this;
}
}
class router implements matcher
{
private
$targetMethod,
$targetPath,
$httpRequestMethod,
$httpRequestPath
;
function __construct(method $targetMethod, path $targetPath)
{
$this->targetMethod = $targetMethod;
$this->targetPath = $targetPath;
}
function httpRequestMethodIs(method $method)
{
$this->httpRequestMethod = $method;
return $this;
}
function httpRequestPathIs(path $path)
{
$this->httpRequestPath = $path;
return $this;
}
function httpRequestIs(request\http $httpRequest)
{
$router = new self($this->targetMethod, $this->targetPath);
$httpRequest->methodIsAskedByHttpRequestMatcher($router);
$httpRequest->pathIsAskedByHttpRequestMatcher($router);
$router->httpRequestHasAnswered();
return $this;
}
private function httpRequestHasAnswered()
{
echo ($this->httpRequestMethod != $this->targetMethod || $this->httpRequestPath != $this->targetPath ? 'Not match!' : 'Match!') . PHP_EOL;
return $this;
}
}
(new router(new method('GET'), new path('/foo')))
->httpRequestIs(new httpRequest(new method('GET'), new path('/foo')))
->httpRequestIs(new httpRequest(new method('POST'), new path('/foo')))
->httpRequestIs(new httpRequest(new method('GET'), new path('/bar')))
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment