From 3a1e4602492cec1c8f3d2aabab2c866022f43bf1 Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Tue, 18 Jul 2017 21:38:53 +0200 Subject: Changed $_GET, $_POST and $_REQUEST to use the Request object --- src/Http/Request.php | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/helpers.php | 17 ++++++++ 2 files changed, 127 insertions(+) create mode 100644 src/Http/Request.php (limited to 'src') diff --git a/src/Http/Request.php b/src/Http/Request.php new file mode 100644 index 00000000..2efd1e1d --- /dev/null +++ b/src/Http/Request.php @@ -0,0 +1,110 @@ +request = $_POST; + $this->query = $_GET; + } + + /** + * Get GET input + * + * @param string $key + * @param mixed $default + * @return mixed + */ + public function get($key, $default = null) + { + if (!empty($this->query[$key])) { + return $this->query[$key]; + } + + return $default; + } + + /** + * Get POST input + * + * @param string $key + * @param mixed $default + * @return mixed + */ + public function post($key, $default = null) + { + if (!empty($this->request[$key])) { + return $this->request[$key]; + } + + return $default; + } + + /** + * Get input data + * + * @param string $key + * @param mixed $default + * @return mixed + */ + public function input($key, $default = null) + { + $data = $this->request + $this->query; + + if (!empty($data[$key])) { + return $data[$key]; + } + + return $default; + } + + /** + * Checks if the input exists + * + * @param string $key + * @return bool + */ + public function has($key) + { + $value = $this->input($key); + + return !empty($value); + } + + /** + * @return self + * @throws ErrorException + */ + public static function getInstance() + { + if (!self::$instance instanceof self) { + throw new ErrorException('Request not initialized'); + } + + return self::$instance; + } + + /** + * @param self $instance + */ + public static function setInstance($instance) + { + self::$instance = $instance; + } +} diff --git a/src/helpers.php b/src/helpers.php index aeb256e9..a410b27e 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -2,6 +2,7 @@ // Some useful functions use Engelsystem\Config\Config; +use Engelsystem\Http\Request; /** * Get or set config values @@ -22,3 +23,19 @@ function config($key = null, $default = null) return Config::getInstance()->get($key, $default); } + +/** + * @param string $key + * @param mixed $default + * @return Request|mixed + */ +function request($key = null, $default = null) +{ + $request = Request::getInstance(); + + if (is_null($key)) { + return $request; + } + + return $request->input($key, $default); +} -- cgit v1.2.3-54-g00ecf