Skip to content

Commit b151929

Browse files
committed
Add return types and factory methods for PATCH and OPTIONS requests
1 parent 3dfe127 commit b151929

File tree

4 files changed

+59
-14
lines changed

4 files changed

+59
-14
lines changed

Classes/Handler/CrudHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ public function configureRoutes(RouterInterface $router, RestRequestInterface $r
196196
$router->add(Route::put($resourceType . '/{slug}/?', [$this, 'update']));
197197
$router->add(Route::post($resourceType . '/{slug}/?', [$this, 'update']));
198198
$router->add(Route::delete($resourceType . '/{slug}/?', [$this, 'delete']));
199-
$router->add(Route::routeWithPatternAndMethod($resourceType . '/{slug}/?', 'PATCH', [$this, 'update']));
199+
$router->add(Route::patch($resourceType . '/{slug}/?', [$this, 'update']));
200200
$router->add(Route::get($resourceType . '/{slug}/{slug}/?', [$this, 'getProperty']));
201-
$router->add(Route::routeWithPatternAndMethod($resourceType . '/?', 'OPTIONS', [$this, 'options']));
201+
$router->add(Route::options($resourceType . '/?', [$this, 'options']));
202202
}
203203

204204
/**

Classes/Router/Route.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function __construct($pattern, $method, callable $callback)
6161
* @param callable $callback
6262
* @return static
6363
*/
64-
public static function get($pattern, callable $callback)
64+
public static function get($pattern, callable $callback): \Cundd\Rest\Router\Route
6565
{
6666
return new static($pattern, 'GET', $callback);
6767
}
@@ -73,7 +73,7 @@ public static function get($pattern, callable $callback)
7373
* @param callable $callback
7474
* @return static
7575
*/
76-
public static function post($pattern, callable $callback)
76+
public static function post($pattern, callable $callback): Route
7777
{
7878
return new static($pattern, 'POST', $callback);
7979
}
@@ -85,7 +85,7 @@ public static function post($pattern, callable $callback)
8585
* @param callable $callback
8686
* @return static
8787
*/
88-
public static function put($pattern, callable $callback)
88+
public static function put($pattern, callable $callback): Route
8989
{
9090
return new static($pattern, 'PUT', $callback);
9191
}
@@ -97,11 +97,35 @@ public static function put($pattern, callable $callback)
9797
* @param callable $callback
9898
* @return static
9999
*/
100-
public static function delete($pattern, callable $callback)
100+
public static function delete($pattern, callable $callback): Route
101101
{
102102
return new static($pattern, 'DELETE', $callback);
103103
}
104104

105+
/**
106+
* Creates a new Route with the given pattern and callback for the method OPTIONS
107+
*
108+
* @param string $pattern
109+
* @param callable $callback
110+
* @return static
111+
*/
112+
public static function options($pattern, callable $callback): Route
113+
{
114+
return new static($pattern, 'OPTIONS', $callback);
115+
}
116+
117+
/**
118+
* Creates a new Route with the given pattern and callback for the method PATCH
119+
*
120+
* @param string $pattern
121+
* @param callable $callback
122+
* @return static
123+
*/
124+
public static function patch($pattern, callable $callback): Route
125+
{
126+
return new static($pattern, 'PATCH', $callback);
127+
}
128+
105129
/**
106130
* Creates a new Route with the given pattern and callback for the method GET
107131
*

Classes/Router/RouteFactoryInterface.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace Cundd\Rest\Router;
55

66
use Cundd\Rest\Domain\Model\ResourceType;
7+
use Cundd\Rest\Router\Route;
78

89
/**
910
* Interface for Route factory methods
@@ -15,34 +16,52 @@ interface RouteFactoryInterface
1516
*
1617
* @param string|ResourceType $pattern
1718
* @param callable $callback
18-
* @return static
19+
* @return Route
1920
*/
20-
public static function get($pattern, callable $callback);
21+
public static function get($pattern, callable $callback): Route;
2122

2223
/**
2324
* Creates a new Route with the given pattern and callback for the method POST
2425
*
2526
* @param string|ResourceType $pattern
2627
* @param callable $callback
27-
* @return static
28+
* @return \Cundd\Rest\Router\Route
2829
*/
29-
public static function post($pattern, callable $callback);
30+
public static function post($pattern, callable $callback): Route;
3031

3132
/**
3233
* Creates a new Route with the given pattern and callback for the method PUT
3334
*
3435
* @param string|ResourceType $pattern
3536
* @param callable $callback
36-
* @return static
37+
* @return \Cundd\Rest\Router\Route
3738
*/
38-
public static function put($pattern, callable $callback);
39+
public static function put($pattern, callable $callback): Route;
3940

4041
/**
4142
* Creates a new Route with the given pattern and callback for the method DELETE
4243
*
4344
* @param string|ResourceType $pattern
4445
* @param callable $callback
45-
* @return static
46+
* @return \Cundd\Rest\Router\Route
4647
*/
47-
public static function delete($pattern, callable $callback);
48+
public static function delete($pattern, callable $callback): Route;
49+
50+
/**
51+
* Creates a new Route with the given pattern and callback for the method OPTIONS
52+
*
53+
* @param string $pattern
54+
* @param callable $callback
55+
* @return \Cundd\Rest\Router\Route
56+
*/
57+
public static function options($pattern, callable $callback): Route;
58+
59+
/**
60+
* Creates a new Route with the given pattern and callback for the method PATCH
61+
*
62+
* @param string $pattern
63+
* @param callable $callback
64+
* @return \Cundd\Rest\Router\Route
65+
*/
66+
public static function patch($pattern, callable $callback): Route;
4867
}

Tests/Unit/Router/RouteTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,5 +322,7 @@ public function factoryMethodsTest()
322322
$this->assertEquals('POST', Route::post('path/sub-path', $this->cb)->getMethod());
323323
$this->assertEquals('PUT', Route::put('path/sub-path', $this->cb)->getMethod());
324324
$this->assertEquals('DELETE', Route::delete('path/sub-path', $this->cb)->getMethod());
325+
$this->assertEquals('OPTIONS', Route::options('path/sub-path', $this->cb)->getMethod());
326+
$this->assertEquals('PATCH', Route::patch('path/sub-path', $this->cb)->getMethod());
325327
}
326328
}

0 commit comments

Comments
 (0)