55/**
66 * ResponseByMethod is used to vary the response to a request by the called HTTP Method.
77 */
8- class ResponseByMethod implements ResponseInterface {
8+ class ResponseByMethod implements MultiResponseInterface {
99
1010 public const METHOD_GET = 'GET ' ;
1111 public const METHOD_POST = 'POST ' ;
@@ -22,13 +22,16 @@ class ResponseByMethod implements ResponseInterface {
2222 /** @var ResponseInterface */
2323 private $ defaultResponse ;
2424
25+ /** @var string|null */
26+ private $ latestMethod ;
27+
2528 /**
2629 * MethodResponse constructor.
2730 *
2831 * @param array<string, ResponseInterface> $responses A map of responses keyed by their method.
29- * @param ResponseInterface|null $defaultResponse The fallthrough response to return if a response for a given
30- * method is not found. If this is not defined the server will
31- * return an HTTP 501 error.
32+ * @param ResponseInterface|null $defaultResponse The fallthrough response to return if a response for a
33+ * given method is not found. If this is not defined the
34+ * server will return an HTTP 501 error.
3235 */
3336 public function __construct ( array $ responses = [], ?ResponseInterface $ defaultResponse = null ) {
3437 foreach ( $ responses as $ method => $ response ) {
@@ -64,7 +67,8 @@ public function getStatus( RequestInfo $request ) : int {
6467 }
6568
6669 private function getMethodResponse ( RequestInfo $ request ) : ResponseInterface {
67- $ method = $ request ->getRequestMethod ();
70+ $ method = $ request ->getRequestMethod ();
71+ $ this ->latestMethod = $ method ;
6872
6973 return $ this ->responses [$ method ] ?? $ this ->defaultResponse ;
7074 }
@@ -76,4 +80,21 @@ public function setMethodResponse( string $method, ResponseInterface $response )
7680 $ this ->responses [$ method ] = $ response ;
7781 }
7882
83+ public function next () : bool {
84+ $ method = $ this ->latestMethod ;
85+ if ( !$ method ) {
86+ return false ;
87+ }
88+
89+ if ( !isset ($ this ->responses [$ method ]) ) {
90+ return false ;
91+ }
92+
93+ if ( !$ this ->responses [$ method ] instanceof MultiResponseInterface ) {
94+ return false ;
95+ }
96+
97+ return $ this ->responses [$ method ]->next ();
98+ }
99+
79100}
0 commit comments