From 4d47d290bfec2dcd416d3a0a5692942638698c73 Mon Sep 17 00:00:00 2001 From: flashwave Date: Thu, 7 Mar 2019 19:11:24 +0100 Subject: [PATCH] Added experimental request variable access class. --- public/forum/forum.php | 5 ++- src/Request/RequestVar.php | 87 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 src/Request/RequestVar.php diff --git a/public/forum/forum.php b/public/forum/forum.php index 249c5cff..11aa76e7 100644 --- a/public/forum/forum.php +++ b/public/forum/forum.php @@ -1,7 +1,10 @@ select('f')->value('int'); +$forumId = max($forumId, 0); if ($forumId === 0) { header('Location: /forum/'); diff --git a/src/Request/RequestVar.php b/src/Request/RequestVar.php new file mode 100644 index 00000000..a9970431 --- /dev/null +++ b/src/Request/RequestVar.php @@ -0,0 +1,87 @@ +value = $value; + $this->type = $type ?? gettype($value); + } + + public static function get(): RequestVar + { + return new static($_GET ?? []); + } + + public static function post(): RequestVar + { + return new static($_POST ?? []); + } + + public function __get(string $name) + { + return $this->select($name); + } + + public function __isset(string $name): bool + { + switch ($this->type) { + case 'array': + return isset($this->value[$name]); + + case 'object': + return isset($this->value->{$name}); + + default: + return null; + } + } + + public function select(string $name): RequestVar + { + switch ($this->type) { + case 'array': + return new static($this->value[$name]); + + case 'object': + return new static($this->value->{$name}); + + default: + return null; + } + } + + public function value(string $type = 'string') + { + if (!is_null($this->valueCasted)) { + $this->valueCasted; + } + + if ($this->type === 'NULL' || (($type === 'object' || $type === 'array') && $this->type !== $type)) { + return null; + } + + if ($type !== 'string' && $this->type === 'string') { + switch ($type) { + case 'boolean': + case 'bool': + return (bool)$this->value; + case 'integer': + case 'int': + return (int)$this->value; + case 'double': + case 'float': + return (float)$this->value; + } + } elseif ($type !== $this->type) { + return null; + } + + return $this->valueCasted = $this->value; + } +}