本文整理汇总了PHP中Psr\Http\Message\ServerRequestInterface::withParsedBody方法的典型用法代码示例。如果您正苦于以下问题:PHP ServerRequestInterface::withParsedBody方法的具体用法?PHP ServerRequestInterface::withParsedBody怎么用?PHP ServerRequestInterface::withParsedBody使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Psr\Http\Message\ServerRequestInterface
的用法示例。
在下文中一共展示了ServerRequestInterface::withParsedBody方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: enrichRequestWithParsedBody
public function enrichRequestWithParsedBody(ServerRequestInterface $request)
{
if ($request->hasHeader(HeaderName::CONTENT_TYPE) && $request->getHeaderLine(HeaderName::CONTENT_TYPE) === 'application/json') {
$parsedBody = $this->serializer->deserialize($request->getBody()->__toString(), 'array', 'json');
return $request->withParsedBody($parsedBody);
} else {
return $request->withParsedBody([]);
}
}
示例2: modify
public function modify(ServerRequestInterface $request) : ServerRequestInterface
{
if (false === strpos($request->getHeaderLine('Content-Type'), 'application/json')) {
return $request;
}
// TODO: Implement broken json handling
return $request->withParsedBody(json_decode($request->getBody()->getContents(), $this->asAssociativeArray));
}
示例3: modify
public function modify(ServerRequestInterface $request) : ServerRequestInterface
{
if (false === strpos($request->getHeaderLine('Content-Type'), 'application/x-www-form-urlencoded')) {
return $request;
}
parse_str($request->getBody()->getContents(), $parsedBody);
return $request->withParsedBody($parsedBody);
}
示例4: parse
/**
* @inheritDoc
*/
public function parse(ServerRequestInterface $request) : ServerRequestInterface
{
if (false === strpos($request->getHeaderLine('Content-Type'), 'application/json')) {
return $request;
}
$body = $request->getBody()->getContents();
$parsedBody = json_decode($body, true);
// TODO: Implement broken json handling
return $request->withParsedBody($parsedBody);
}
示例5: handlePayload
/**
* Handle the payload.
*
* @param ServerRequestInterface $request
*
* @return ServerRequestInterface
*/
protected function handlePayload(ServerRequestInterface $request)
{
if ($request->getParsedBody() || !in_array($request->getMethod(), ['POST', 'PUT', 'DELETE'], true)) {
return $request;
}
$contentType = trim($request->getHeaderLine('Content-Type'));
//json
if (stripos($contentType, 'application/json') === 0) {
return $request->withParsedBody($this->parseJson($request->getBody()))->withBody(Middleware::createStream());
}
//urlencoded
if (stripos($contentType, 'application/x-www-form-urlencoded') === 0) {
return $request->withParsedBody($this->parseUrlEncoded($request->getBody()))->withBody(Middleware::createStream());
}
//csv
if (stripos($contentType, 'text/csv') === 0) {
return $request->withParsedBody($this->parseCsv($request->getBody()))->withBody(Middleware::createStream());
}
return $request;
}
示例6: parse
/**
* {@inheritDoc}
*/
public function parse(ServerRequestInterface $request)
{
$parsedBody = $request->getParsedBody();
if (!empty($parsedBody)) {
return $request;
}
$rawBody = (string) $request->getBody();
if (empty($rawBody)) {
return $request;
}
parse_str($rawBody, $parsedBody);
return $request->withParsedBody($parsedBody);
}
示例7: csv
/**
* Parses csv strings.
*
* @param ServerRequestInterface $request
*
* @return ServerRequestInterface
*/
public function csv(ServerRequestInterface $request)
{
$body = $request->getBody();
if ($body->isSeekable()) {
$body->rewind();
}
$stream = $body->detach();
$data = [];
while (($row = fgetcsv($stream)) !== false) {
$data[] = $row;
}
fclose($stream);
return $request->withParsedBody($data);
}
示例8: modify
public function modify(ServerRequestInterface $request) : ServerRequestInterface
{
$headerLine = $request->getHeaderLine('Content-Type');
if (false === strpos($headerLine, 'multipart/')) {
return $request;
}
$boundary = null;
if (preg_match("/boundary=\"?(.*)\"?\$/", $headerLine, $matches)) {
$boundary = $matches[1];
}
$variables = [];
$files = [];
$body = $request->getBody()->detach();
$partInfo = null;
while (false !== ($lineN = fgets($body))) {
if (0 === strpos($lineN, '--')) {
if (null === $boundary) {
$boundary = rtrim($lineN);
}
continue;
}
$line = rtrim($lineN);
if ('' === $line) {
if (isset($partInfo['Content-Disposition']) && true !== empty($partInfo['Content-Disposition']['filename'])) {
$this->parseFile($body, $boundary, $partInfo, $files);
} elseif ($partInfo !== null) {
$this->parseVariable($body, $boundary, $partInfo['Content-Disposition']['name'], $variables);
}
$partInfo = null;
continue;
}
list($key, $value) = explode(':', $line, 2);
$partInfo[$key] = $this->parseHeader($value, $key);
}
return $request->withParsedBody($variables)->withUploadedFiles($files);
}
示例9: withParsedBody
/**
* {@inheritdoc}
*/
public function withParsedBody($data)
{
return new static($this->wrapped->withParsedBody($data));
}
示例10: withParsedBody
/**
* {@inheritDoc}
*/
public function withParsedBody($data)
{
return new self($this->app, $this->psrRequest->withParsedBody($data));
}
示例11: withParsedBody
/**
* Proxy to ServerRequestInterface::withParsedBody()
*
* {@inheritdoc}
*/
public function withParsedBody($params)
{
$new = $this->psrRequest->withParsedBody($params);
return new self($new, $this->originalRequest);
}
示例12: withParsedBody
/**
* @inheritDoc
*/
public function withParsedBody($data)
{
$self = clone $this;
$self->serverRequest = $this->serverRequest->withParsedBody($data);
return $self;
}
示例13: parseFromUrlEncoded
/**
* @param Request $request
* @return Request
*/
protected function parseFromUrlEncoded(Request $request)
{
$rawBody = (string) $request->getBody();
if (empty($rawBody)) {
return $request;
}
$parsedBody = [];
parse_str($rawBody, $parsedBody);
return $request->withParsedBody($parsedBody);
}