summaryrefslogtreecommitdiff
path: root/src/Http/Request.php
diff options
context:
space:
mode:
authormsquare <msquare@notrademark.de>2017-07-20 18:34:19 +0200
committerGitHub <noreply@github.com>2017-07-20 18:34:19 +0200
commit37d4edcd9ace5021b6eb02761a9c3865c5607f33 (patch)
tree16c0da4cd2d9c6c10f5d5e02c1b02bd0986636c2 /src/Http/Request.php
parent9b3f6f557a127fef16be267c26f8239dc1c22126 (diff)
parentb7ebb05b8e71b391b6b029fceb5a2d00ff27004c (diff)
Merge pull request #328 from MyIgel/master
Changed $_GET, $_POST and $_REQUEST to use the Request object
Diffstat (limited to 'src/Http/Request.php')
-rw-r--r--src/Http/Request.php110
1 files changed, 110 insertions, 0 deletions
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 @@
+<?php
+
+namespace Engelsystem\Http;
+
+use ErrorException;
+
+class Request
+{
+ /** @var self */
+ protected static $instance;
+
+ /** @var array of POST data */
+ protected $request;
+
+ /** @var array of GET data */
+ protected $query;
+
+ /**
+ * Initialize request
+ */
+ public function create()
+ {
+ $this->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;
+ }
+}