Skip to content

Commit

Permalink
Added Uri::post() and Uri::getConentType()
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Apr 27, 2018
1 parent f681f1c commit 0b1c18d
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions system/src/Grav/Common/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class Uri
protected $root;
protected $root_path;
protected $uri;
protected $content_type;
protected $post;

/**
* Uri constructor.
Expand Down Expand Up @@ -1235,6 +1237,55 @@ protected function reset()
$this->url = $this->base . $this->uri;
}

/**
* Get's post from either $_POST or JSON response object
* By default returns all data, or can return a single item
*
* @param string $element
* @param string $filter_type
* @return array|mixed|null
*/
public function post($element = null, $filter_type = null)
{
if (!$this->post) {
$content_type = $this->getContentType();
if ($content_type == 'application/json') {
$json = file_get_contents('php://input');
$this->post = json_decode($json, true);
} elseif (!empty($_POST)) {
$this->post = (array)$_POST;
}
}

if ($this->post && !is_null($element)) {
$item = Utils::getDotNotation($this->post, $element);
if ($filter_type) {
$item = filter_var($item, $filter_type);
}
return $item;
}

return $this->post;
}

/**
* Get content type from request
*
* @param bool $short
* @return null|string
*/
private function getContentType($short = true)
{
if (isset($_SERVER['CONTENT_TYPE'])) {
$content_type = $_SERVER['CONTENT_TYPE'];
if ($short) {
return Utils::substrToString($content_type,';');
}
return $content_type;
}
return null;
}

/**
* Get the base URI with port if needed
*
Expand Down

0 comments on commit 0b1c18d

Please sign in to comment.