Clean up the input class a bit

This commit is contained in:
Timothy Warren 2014-05-14 20:32:30 -04:00
parent 5e3f011cfa
commit fd60d2f504
2 changed files with 28 additions and 29 deletions

View File

@ -14,13 +14,13 @@ namespace Sleepy\Core;
/** /**
* Class for accessing request data * Class for accessing request data
* *
* @method array server() * @method array server(string $index=null, int $filter=\FILTER_SANITIZE_STRING)
* @method array env() * @method array env(string $index=null, int $filter=\FILTER_SANITIZE_STRING)
* @method array get() * @method array get(string $index=null, int $filter=\FILTER_SANITIZE_STRING)
* @method array post() * @method array post(string $index=null, int $filter=\FILTER_SANITIZE_STRING)
* @method array put() * @method array put(string $index=null, int $filter=\FILTER_SANITIZE_STRING)
* @method array delete() * @method array delete(string $index=null, int $filter=\FILTER_SANITIZE_STRING)
* @method array cookie() * @method array cookie(string $index=null, int $filter=\FILTER_SANITIZE_STRING)
*/ */
class Input { class Input {
@ -125,6 +125,8 @@ class Input {
$this->request_headers[$new_key] = $val; $this->request_headers[$new_key] = $val;
} }
} }
$this->parsed_headers = $this->parse_headers();
} }
/** /**
@ -164,19 +166,7 @@ class Input {
*/ */
public function header($index = NULL) public function header($index = NULL)
{ {
if ($index !== NULL) return $this->_header($this->request_headers, $index);
{
$index = (\strtolower(\str_replace([' ', '_'], '-', $index)));
if (isset($this->request_headers[$index]))
{
return $this->request_headers[$index];
}
return NULL;
}
return $this->request_headers;
} }
/** /**
@ -187,24 +177,31 @@ class Input {
*/ */
public function header_array($index = NULL) public function header_array($index = NULL)
{ {
if (empty($this->parsed_headers)) return $this->_header($this->parsed_headers, $index);
{
$this->parsed_headers = $this->parse_headers();
} }
/**
* Retrieve headers
*
* @param array $var
* @param string|null $index
* @return mixed
*/
protected function _header($var, $index = NULL)
{
if ($index !== NULL) if ($index !== NULL)
{ {
$index = (str_replace([' ', '-'], '_', $index)); $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 NULL;
} }
return $this->parsed_headers; return $var;
} }
/** /**
@ -270,7 +267,7 @@ class Input {
$paren_matches = []; $paren_matches = [];
$paren_pattern = "`\(.*?\)`i"; $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); preg_match_all($slash_pattern, $ua_string, $slash_matches);
foreach($slash_matches[0] as $arr) foreach($slash_matches[0] as $arr)
{ {

View File

@ -1,5 +1,7 @@
<?php <?php
use Sleepy\Core\Input;
class InputTest extends Sleepy_TestCase { class InputTest extends Sleepy_TestCase {
public function setUp() public function setUp()
@ -30,7 +32,7 @@ class InputTest extends Sleepy_TestCase {
'tisket' => 'tasket' 'tisket' => 'tasket'
]; ];
$this->input = new Sleepy\Core\Input(); $this->input = new Input();
} }
public function dataTestHeader() public function dataTestHeader()