Skip to content

Commit

Permalink
Accept string[] as methods in match method
Browse files Browse the repository at this point in the history
  • Loading branch information
jtojnar committed Jan 18, 2022
1 parent 1d91697 commit 0d55477
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/Bramus/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
*/
class Router
{
/**
* List of all HTTP methods.
*/
const ALL_METHODS = ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH', 'HEAD'];

/**
* @var array The route patterns and their handling functions
*/
Expand Down Expand Up @@ -70,7 +75,7 @@ public function before($methods, $pattern, $fn)
/**
* Store a route and a handling function to be executed when accessed using one of the specified methods.
*
* @param string $methods Allowed methods, | delimited
* @param string|string[] $methods Allowed methods, | delimited
* @param string $pattern A route pattern such as /about/system
* @param object|callable $fn The handling function to be executed
*/
Expand All @@ -79,7 +84,8 @@ public function match($methods, $pattern, $fn)
$pattern = $this->baseRoute . '/' . trim($pattern, '/');
$pattern = $this->baseRoute ? rtrim($pattern, '/') : $pattern;

foreach (explode('|', $methods) as $method) {
$handledMethods = is_array($methods) ? $methods : explode('|', $methods);
foreach ($handledMethods as $method) {
$this->afterRoutes[$method][] = array(
'pattern' => $pattern,
'fn' => $fn,
Expand All @@ -95,7 +101,7 @@ public function match($methods, $pattern, $fn)
*/
public function all($pattern, $fn)
{
$this->match('GET|POST|PUT|DELETE|OPTIONS|PATCH|HEAD', $pattern, $fn);
$this->match(self::ALL_METHODS, $pattern, $fn);
}

/**
Expand All @@ -106,7 +112,7 @@ public function all($pattern, $fn)
*/
public function get($pattern, $fn)
{
$this->match('GET', $pattern, $fn);
$this->match(['GET'], $pattern, $fn);
}

/**
Expand All @@ -117,7 +123,7 @@ public function get($pattern, $fn)
*/
public function post($pattern, $fn)
{
$this->match('POST', $pattern, $fn);
$this->match(['POST'], $pattern, $fn);
}

/**
Expand All @@ -128,7 +134,7 @@ public function post($pattern, $fn)
*/
public function patch($pattern, $fn)
{
$this->match('PATCH', $pattern, $fn);
$this->match(['PATCH'], $pattern, $fn);
}

/**
Expand All @@ -139,7 +145,7 @@ public function patch($pattern, $fn)
*/
public function delete($pattern, $fn)
{
$this->match('DELETE', $pattern, $fn);
$this->match(['DELETE'], $pattern, $fn);
}

/**
Expand All @@ -150,7 +156,7 @@ public function delete($pattern, $fn)
*/
public function put($pattern, $fn)
{
$this->match('PUT', $pattern, $fn);
$this->match(['PUT'], $pattern, $fn);
}

/**
Expand All @@ -161,7 +167,7 @@ public function put($pattern, $fn)
*/
public function options($pattern, $fn)
{
$this->match('OPTIONS', $pattern, $fn);
$this->match(['OPTIONS'], $pattern, $fn);
}

/**
Expand Down
58 changes: 58 additions & 0 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,64 @@ public function testRequestMethods()
});
}

public function testArrayMatch()
{
// Create Router
$router = new \Bramus\Router\Router();
$router->match(['POST', 'PUT'], '/', function () {
echo 'post|put';
});

$notFound = false;
$router->set404(function () use (&$notFound) {
$notFound = true;
});

// Test GET
$notFound = false;
run_request($router, 'GET', '/', function ($responseBody) use (&$notFound) {
$this->assertTrue($notFound);
});

// Test POST
$notFound = false;
run_request($router, 'POST', '/', function ($responseBody) use (&$notFound) {
$this->assertFalse($notFound);
$this->assertEquals('post|put', $responseBody);
});

// Test PUT
$notFound = false;
run_request($router, 'PUT', '/', function ($responseBody) use (&$notFound) {
$this->assertFalse($notFound);
$this->assertEquals('post|put', $responseBody);
});

// Test DELETE
$notFound = false;
run_request($router, 'DELETE', '/', function ($responseBody) use (&$notFound) {
$this->assertTrue($notFound);
});

// Test OPTIONS
$notFound = false;
run_request($router, 'OPTIONS', '/', function ($responseBody) use (&$notFound) {
$this->assertTrue($notFound);
});

// Test PATCH
$notFound = false;
run_request($router, 'PATCH', '/', function ($responseBody) use (&$notFound) {
$this->assertTrue($notFound);
});

// Test HEAD
$notFound = false;
run_request($router, 'HEAD', '/', function ($responseBody) use (&$notFound) {
$this->assertTrue($notFound);
});
}

public function testShorthandAll()
{
// Create Router
Expand Down

0 comments on commit 0d55477

Please sign in to comment.