Skip to content

Commit

Permalink
Add return types and factory methods for PATCH and OPTIONS requests
Browse files Browse the repository at this point in the history
  • Loading branch information
cundd committed Mar 12, 2019
1 parent 3dfe127 commit b151929
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Classes/Handler/CrudHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ public function configureRoutes(RouterInterface $router, RestRequestInterface $r
$router->add(Route::put($resourceType . '/{slug}/?', [$this, 'update']));
$router->add(Route::post($resourceType . '/{slug}/?', [$this, 'update']));
$router->add(Route::delete($resourceType . '/{slug}/?', [$this, 'delete']));
$router->add(Route::routeWithPatternAndMethod($resourceType . '/{slug}/?', 'PATCH', [$this, 'update']));
$router->add(Route::patch($resourceType . '/{slug}/?', [$this, 'update']));
$router->add(Route::get($resourceType . '/{slug}/{slug}/?', [$this, 'getProperty']));
$router->add(Route::routeWithPatternAndMethod($resourceType . '/?', 'OPTIONS', [$this, 'options']));
$router->add(Route::options($resourceType . '/?', [$this, 'options']));
}

/**
Expand Down
32 changes: 28 additions & 4 deletions Classes/Router/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function __construct($pattern, $method, callable $callback)
* @param callable $callback
* @return static
*/
public static function get($pattern, callable $callback)
public static function get($pattern, callable $callback): \Cundd\Rest\Router\Route
{
return new static($pattern, 'GET', $callback);
}
Expand All @@ -73,7 +73,7 @@ public static function get($pattern, callable $callback)
* @param callable $callback
* @return static
*/
public static function post($pattern, callable $callback)
public static function post($pattern, callable $callback): Route
{
return new static($pattern, 'POST', $callback);
}
Expand All @@ -85,7 +85,7 @@ public static function post($pattern, callable $callback)
* @param callable $callback
* @return static
*/
public static function put($pattern, callable $callback)
public static function put($pattern, callable $callback): Route
{
return new static($pattern, 'PUT', $callback);
}
Expand All @@ -97,11 +97,35 @@ public static function put($pattern, callable $callback)
* @param callable $callback
* @return static
*/
public static function delete($pattern, callable $callback)
public static function delete($pattern, callable $callback): Route
{
return new static($pattern, 'DELETE', $callback);
}

/**
* Creates a new Route with the given pattern and callback for the method OPTIONS
*
* @param string $pattern
* @param callable $callback
* @return static
*/
public static function options($pattern, callable $callback): Route
{
return new static($pattern, 'OPTIONS', $callback);
}

/**
* Creates a new Route with the given pattern and callback for the method PATCH
*
* @param string $pattern
* @param callable $callback
* @return static
*/
public static function patch($pattern, callable $callback): Route
{
return new static($pattern, 'PATCH', $callback);
}

/**
* Creates a new Route with the given pattern and callback for the method GET
*
Expand Down
35 changes: 27 additions & 8 deletions Classes/Router/RouteFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Cundd\Rest\Router;

use Cundd\Rest\Domain\Model\ResourceType;
use Cundd\Rest\Router\Route;

/**
* Interface for Route factory methods
Expand All @@ -15,34 +16,52 @@ interface RouteFactoryInterface
*
* @param string|ResourceType $pattern
* @param callable $callback
* @return static
* @return Route
*/
public static function get($pattern, callable $callback);
public static function get($pattern, callable $callback): Route;

/**
* Creates a new Route with the given pattern and callback for the method POST
*
* @param string|ResourceType $pattern
* @param callable $callback
* @return static
* @return \Cundd\Rest\Router\Route
*/
public static function post($pattern, callable $callback);
public static function post($pattern, callable $callback): Route;

/**
* Creates a new Route with the given pattern and callback for the method PUT
*
* @param string|ResourceType $pattern
* @param callable $callback
* @return static
* @return \Cundd\Rest\Router\Route
*/
public static function put($pattern, callable $callback);
public static function put($pattern, callable $callback): Route;

/**
* Creates a new Route with the given pattern and callback for the method DELETE
*
* @param string|ResourceType $pattern
* @param callable $callback
* @return static
* @return \Cundd\Rest\Router\Route
*/
public static function delete($pattern, callable $callback);
public static function delete($pattern, callable $callback): Route;

/**
* Creates a new Route with the given pattern and callback for the method OPTIONS
*
* @param string $pattern
* @param callable $callback
* @return \Cundd\Rest\Router\Route
*/
public static function options($pattern, callable $callback): Route;

/**
* Creates a new Route with the given pattern and callback for the method PATCH
*
* @param string $pattern
* @param callable $callback
* @return \Cundd\Rest\Router\Route
*/
public static function patch($pattern, callable $callback): Route;
}
2 changes: 2 additions & 0 deletions Tests/Unit/Router/RouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,5 +322,7 @@ public function factoryMethodsTest()
$this->assertEquals('POST', Route::post('path/sub-path', $this->cb)->getMethod());
$this->assertEquals('PUT', Route::put('path/sub-path', $this->cb)->getMethod());
$this->assertEquals('DELETE', Route::delete('path/sub-path', $this->cb)->getMethod());
$this->assertEquals('OPTIONS', Route::options('path/sub-path', $this->cb)->getMethod());
$this->assertEquals('PATCH', Route::patch('path/sub-path', $this->cb)->getMethod());
}
}

0 comments on commit b151929

Please sign in to comment.