forked from zendframework/zend-expressive-helpers
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added strategies to the BodyParamsMiddleware
This patch builds on the one in zendframework#3, moving the act of parsing a Content-Type as well as parsing the request body to *strategies*, which implement `Zend\Expressive\BodyParams\StrategyInterface`. These define two methods: - `match($contentType)`, which should accept a Content-Type header value, and return a boolean value indicating whether or not the strategy matches. - `parse(ServerRequestInterface $request)`, which should accept the request instance, parse its raw body, and return a new request instance with the results of parsing injected as the parsed body. The form and json matching/parsing were rewritten as strategies, and added as default strategies to the middleware. Custom strategies can be written and then attached using `addStrategy()`.
- Loading branch information
1 parent
fe9916b
commit 368e8ed
Showing
9 changed files
with
576 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
/** | ||
* @see http://github.com/zendframework/zend-expressive-helpers for the canonical source repository | ||
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license https://github.com/zendframework/zend-expressive-helpers/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace Zend\Expressive\Helper\BodyParams; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
class FormUrlEncodedStrategy implements StrategyInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function match($contentType) | ||
{ | ||
return (bool) preg_match('#^application/x-www-form-urlencoded($|[ ;])#', $contentType); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function parse(ServerRequestInterface $request) | ||
{ | ||
return $request; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
/** | ||
* @see http://github.com/zendframework/zend-expressive-helpers for the canonical source repository | ||
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license https://github.com/zendframework/zend-expressive-helpers/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace Zend\Expressive\Helper\BodyParams; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
class JsonStrategy implements StrategyInterface | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function match($contentType) | ||
{ | ||
$parts = explode(';', $contentType); | ||
$mime = array_shift($parts); | ||
return (bool) preg_match('#[/+]json$#', trim($mime)); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function parse(ServerRequestInterface $request) | ||
{ | ||
$rawBody = $request->getBody()->getContents(); | ||
return $request | ||
->withAttribute('rawBody', $rawBody) | ||
->withParsedBody(json_decode($rawBody, true)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
/** | ||
* @see http://github.com/zendframework/zend-expressive-helpers for the canonical source repository | ||
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license https://github.com/zendframework/zend-expressive-helpers/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace Zend\Expressive\Helper\BodyParams; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
/** | ||
* Interface defining a body parameter strategy. | ||
*/ | ||
interface StrategyInterface | ||
{ | ||
/** | ||
* Match the content type to the strategy criteria. | ||
* | ||
* @param string $contentType | ||
* @return bool Whether or not the strategy matches. | ||
*/ | ||
public function match($contentType); | ||
|
||
/** | ||
* Parse the body content and return a new response. | ||
* | ||
* @param ServerRequestInterface $request | ||
* @return ServerRequestInterface | ||
*/ | ||
public function parse(ServerRequestInterface $request); | ||
} |
Oops, something went wrong.