From fd60d2f504c9f79060a5111698605a8ac2223e5d Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Wed, 14 May 2014 20:32:30 -0400 Subject: [PATCH] Clean up the input class a bit --- Sleepy/Core/Input.php | 53 +++++++++++++++++++--------------------- tests/Core/InputTest.php | 4 ++- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/Sleepy/Core/Input.php b/Sleepy/Core/Input.php index 4d65871..d8c0325 100755 --- a/Sleepy/Core/Input.php +++ b/Sleepy/Core/Input.php @@ -14,13 +14,13 @@ namespace Sleepy\Core; /** * Class for accessing request data * - * @method array server() - * @method array env() - * @method array get() - * @method array post() - * @method array put() - * @method array delete() - * @method array cookie() + * @method array server(string $index=null, int $filter=\FILTER_SANITIZE_STRING) + * @method array env(string $index=null, int $filter=\FILTER_SANITIZE_STRING) + * @method array get(string $index=null, int $filter=\FILTER_SANITIZE_STRING) + * @method array post(string $index=null, int $filter=\FILTER_SANITIZE_STRING) + * @method array put(string $index=null, int $filter=\FILTER_SANITIZE_STRING) + * @method array delete(string $index=null, int $filter=\FILTER_SANITIZE_STRING) + * @method array cookie(string $index=null, int $filter=\FILTER_SANITIZE_STRING) */ class Input { @@ -125,6 +125,8 @@ class Input { $this->request_headers[$new_key] = $val; } } + + $this->parsed_headers = $this->parse_headers(); } /** @@ -164,19 +166,7 @@ class Input { */ public function header($index = NULL) { - if ($index !== NULL) - { - $index = (\strtolower(\str_replace([' ', '_'], '-', $index))); - - if (isset($this->request_headers[$index])) - { - return $this->request_headers[$index]; - } - - return NULL; - } - - return $this->request_headers; + return $this->_header($this->request_headers, $index); } /** @@ -187,24 +177,31 @@ class Input { */ public function header_array($index = NULL) { - if (empty($this->parsed_headers)) - { - $this->parsed_headers = $this->parse_headers(); - } + return $this->_header($this->parsed_headers, $index); + } + /** + * Retrieve headers + * + * @param array $var + * @param string|null $index + * @return mixed + */ + protected function _header($var, $index = NULL) + { if ($index !== NULL) { $index = (str_replace([' ', '-'], '_', $index)); - if (isset($this->parsed_headers[$index])) + if (array_key_exists($index, $var)) { - return $this->parsed_headers[$index]; + return $var[$index]; } return NULL; } - return $this->parsed_headers; + return $var; } /** @@ -270,7 +267,7 @@ class Input { $paren_matches = []; $paren_pattern = "`\(.*?\)`i"; - // Get all the foo/12.3 paterns from the user agent string + // Get all the foo/12.3 patterns from the user agent string preg_match_all($slash_pattern, $ua_string, $slash_matches); foreach($slash_matches[0] as $arr) { diff --git a/tests/Core/InputTest.php b/tests/Core/InputTest.php index 4eb225c..7c2f6ef 100755 --- a/tests/Core/InputTest.php +++ b/tests/Core/InputTest.php @@ -1,5 +1,7 @@ 'tasket' ]; - $this->input = new Sleepy\Core\Input(); + $this->input = new Input(); } public function dataTestHeader()