- Fixed post() method

This commit is contained in:
Dave M. 2022-06-27 17:28:35 +00:00
parent 18a0fd7fdc
commit 22e046dfcd
1 changed files with 17 additions and 1 deletions

View File

@ -7,6 +7,7 @@ use Picea\Extension\Extension,
use Picea\Compiler\Context;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ServerRequestInterface;
class Request implements Extension {
@ -46,7 +47,7 @@ class Request implements Extension {
public function post(? string $variableName = null, $default = null)
{
return $variableName === null ? $this->request->getParsedBody() : static::arrayGet($this->request->getParsedBody(), $variableName) ?? $default;
return $variableName === null ? $this->_post() : static::arrayGet($this->_post(), $variableName) ?? $default;
}
public function request(? string $variableName = null, $default = null)
@ -75,4 +76,19 @@ class Request implements Extension {
return null;
}
}
protected function _post() : array
{
$post = $this->request->getParsedBody();
if ( ! $post ) {
$content = utf8_encode((string) $this->request->getBody());
if ( $content && ( $json = json_decode($content, true) ) ) {
$post = $json;
}
}
return $post ?: [];
}
}