Upgrade to 3.7.1
This commit is contained in:
@@ -16,114 +16,114 @@ namespace Kirby\Http\Request;
|
||||
*/
|
||||
class Body
|
||||
{
|
||||
use Data;
|
||||
use Data;
|
||||
|
||||
/**
|
||||
* The raw body content
|
||||
*
|
||||
* @var string|array
|
||||
*/
|
||||
protected $contents;
|
||||
/**
|
||||
* The raw body content
|
||||
*
|
||||
* @var string|array
|
||||
*/
|
||||
protected $contents;
|
||||
|
||||
/**
|
||||
* The parsed content as array
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $data;
|
||||
/**
|
||||
* The parsed content as array
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $data;
|
||||
|
||||
/**
|
||||
* Creates a new request body object.
|
||||
* You can pass your own array or string.
|
||||
* If null is being passed, the class will
|
||||
* fetch the body either from the $_POST global
|
||||
* or from php://input.
|
||||
*
|
||||
* @param array|string|null $contents
|
||||
*/
|
||||
public function __construct($contents = null)
|
||||
{
|
||||
$this->contents = $contents;
|
||||
}
|
||||
/**
|
||||
* Creates a new request body object.
|
||||
* You can pass your own array or string.
|
||||
* If null is being passed, the class will
|
||||
* fetch the body either from the $_POST global
|
||||
* or from php://input.
|
||||
*
|
||||
* @param array|string|null $contents
|
||||
*/
|
||||
public function __construct($contents = null)
|
||||
{
|
||||
$this->contents = $contents;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the raw contents for the body
|
||||
* or uses the passed contents.
|
||||
*
|
||||
* @return string|array
|
||||
*/
|
||||
public function contents()
|
||||
{
|
||||
if ($this->contents === null) {
|
||||
if (empty($_POST) === false) {
|
||||
$this->contents = $_POST;
|
||||
} else {
|
||||
$this->contents = file_get_contents('php://input');
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Fetches the raw contents for the body
|
||||
* or uses the passed contents.
|
||||
*
|
||||
* @return string|array
|
||||
*/
|
||||
public function contents()
|
||||
{
|
||||
if ($this->contents === null) {
|
||||
if (empty($_POST) === false) {
|
||||
$this->contents = $_POST;
|
||||
} else {
|
||||
$this->contents = file_get_contents('php://input');
|
||||
}
|
||||
}
|
||||
|
||||
return $this->contents;
|
||||
}
|
||||
return $this->contents;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses the raw contents once and caches
|
||||
* the result. The parser will try to convert
|
||||
* the body with the json decoder first and
|
||||
* then run parse_str to get some results
|
||||
* if the json decoder failed.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function data(): array
|
||||
{
|
||||
if (is_array($this->data) === true) {
|
||||
return $this->data;
|
||||
}
|
||||
/**
|
||||
* Parses the raw contents once and caches
|
||||
* the result. The parser will try to convert
|
||||
* the body with the json decoder first and
|
||||
* then run parse_str to get some results
|
||||
* if the json decoder failed.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function data(): array
|
||||
{
|
||||
if (is_array($this->data) === true) {
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
$contents = $this->contents();
|
||||
$contents = $this->contents();
|
||||
|
||||
// return content which is already in array form
|
||||
if (is_array($contents) === true) {
|
||||
return $this->data = $contents;
|
||||
}
|
||||
// return content which is already in array form
|
||||
if (is_array($contents) === true) {
|
||||
return $this->data = $contents;
|
||||
}
|
||||
|
||||
// try to convert the body from json
|
||||
$json = json_decode($contents, true);
|
||||
// try to convert the body from json
|
||||
$json = json_decode($contents, true);
|
||||
|
||||
if (is_array($json) === true) {
|
||||
return $this->data = $json;
|
||||
}
|
||||
if (is_array($json) === true) {
|
||||
return $this->data = $json;
|
||||
}
|
||||
|
||||
if (strstr($contents, '=') !== false) {
|
||||
// try to parse the body as query string
|
||||
parse_str($contents, $parsed);
|
||||
if (strstr($contents, '=') !== false) {
|
||||
// try to parse the body as query string
|
||||
parse_str($contents, $parsed);
|
||||
|
||||
if (is_array($parsed)) {
|
||||
return $this->data = $parsed;
|
||||
}
|
||||
}
|
||||
if (is_array($parsed)) {
|
||||
return $this->data = $parsed;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->data = [];
|
||||
}
|
||||
return $this->data = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the data array back
|
||||
* to a http query string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function toString(): string
|
||||
{
|
||||
return http_build_query($this->data());
|
||||
}
|
||||
/**
|
||||
* Converts the data array back
|
||||
* to a http query string
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function toString(): string
|
||||
{
|
||||
return http_build_query($this->data());
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic string converter
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->toString();
|
||||
}
|
||||
/**
|
||||
* Magic string converter
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->toString();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user