Remove some redundancies

This commit is contained in:
Timothy Warren 2014-05-14 11:58:49 -04:00
parent 4c8ce751f2
commit 56d6ac12c8
2 changed files with 66 additions and 47 deletions

View File

@ -87,10 +87,8 @@ class Input {
/** /**
* Instantiates the class * Instantiates the class
*
* @param array $config
*/ */
public function __construct($config = []) public function __construct()
{ {
// Type of HTTP request // Type of HTTP request
$this->verb = \strtolower($_SERVER['REQUEST_METHOD']); $this->verb = \strtolower($_SERVER['REQUEST_METHOD']);
@ -148,17 +146,11 @@ class Input {
$index = $args[0]; $index = $args[0];
$filter = $args[1]; $filter = $args[1];
if (isset($this->var_map[$name])) if (isset($this->var_map[$name]) || isset($this->$name))
{ {
// Get a superglobal ($_VAR) value // Get an input variable value
return $this->get_superglobal_var($name, $index, $filter); return $this->get_var($name, $index, $filter);
} }
else if(isset($this->$name)) // @codeCoverageIgnoreStart
{
// Get a input variable not in a superglobal (eg. PUT/DELETE)
return $this->get_request_var($name, $index, $filter);
}
// @codeCoverageIgnoreEnd
// What kind of request are you trying to make?! // What kind of request are you trying to make?!
throw new \DomainException('Invalid input array.'); throw new \DomainException('Invalid input array.');
@ -357,57 +349,33 @@ class Input {
return $output; return $output;
} }
/** /**
* Get input var(s) from non-defined superglobal * Get superglobal or generated input array value
* * @param string $type - HTTP verb
* @codeCoverageIgnore
* @param string $type - input array
* @param string $index - variable in the input array * @param string $index - variable in the input array
* @param int $filter - PHP filter_var flag * @param int $filter - PHP filter_var flag
* @return mixed * @return mixed
*/ */
protected function get_request_var($type, $index=NULL, $filter=FILTER_SANITIZE_STRING) protected function get_var($type, $index=NULL, $filter=\FILTER_SANITIZE_STRING)
{ {
// If index is null, return the whole array // Determine if superglobal or generated
if ($index === NULL) $var = array_key_exists($type, $this->var_map)
{ ? $this->var_map[$type]
return ($filter !== NULL) ? \filter_var_array($this->$type, $filter) : $this->$type; : $this->$type;
}
// Prevent errors for non-existant variables
if ( ! isset($this->$type[$index]))
{
return NULL;
}
return ($filter !== NULL) ? \filter_var($this->$type[$index], $filter) : $this->$type[$index];
}
/**
* Get index from superglobal
*
* @param string $type - superglobal
* @param string $index - variable in the superglobal
* @param int $filter - PHP filter_var flag
* @return mixed
*/
protected function get_superglobal_var($type, $index=NULL, $filter=FILTER_SANITIZE_STRING)
{
$var =& $this->var_map[$type];
// Return the whole array if the index is null // Return the whole array if the index is null
if ($index === NULL) if ($index === NULL)
{ {
return ($filter !== NULL) ? \filter_var_array($var, $filter) : $var; return ($filter !== NULL) ? \filter_var_array($var, $filter) : $var;
} }
// Prevent errors for non-existant variables // Prevent errors for non-existant variables
if ( ! isset($var[$index])) if ( ! isset($var[$index]))
{ {
return NULL; return NULL;
} }
return ($filter !== NULL) ? \filter_var($var[$index], $filter) : $var[$index]; return ($filter !== NULL) ? \filter_var($var[$index], $filter) : $var[$index];
} }

51
application/bootstrap.php Normal file
View File

@ -0,0 +1,51 @@
<?php
/**
* Sleepy - a REST framework
*
*
* A PHP Rest Framework valuing convention over configuration,
* but aiming to be as flexible as possible
*
* @author Timothy J. Warren
* @package Sleepy
*/
namespace Sleepy\Core;
// Include namespaces
use Aura\Di\Container as DiContainer;
use Aura\Di\Factory as DiFactory;
use Aura\Router\RouterFactory;
$di = new DiContainer(new DiFactory());
$di->set('config', $di->newInstance('Sleepy\Core\Config'));
$di->set('router', $di->newInstance('Aura\Router\RouterFactory')->newInstance());
$di->set('input', $di->newInstance('Sleepy\Core\Input'));
$di->params['Sleepy\Core\Output'] = [
'config' => $di->get('config'),
'input' => $di->get('input')
];
$di->set('output', function () use ($di) {
return $di->newInstance('Sleepy\Core\Output');
});
// Set the output data
$browser = get_browser();
$browser->browser_name_regex = \utf8_encode($browser->browser_name_regex);
$i = $di->get('input');
$di->get('output')->set_data(['json','yaml','xml','html'], [
'$_SERVER' => $i->server(),
'$_GET' => $i->get(),
'$_POST' => $i->post(),
'$_PUT' => $i->put(),
'$_DELETE' => $i->delete(),
'$_ENV' => $i->env(),
'$_COOKIE' => $i->cookie(),
'browser' => $browser,
'raw headers' => $i->header(),
'parsed headers' => $i->header_array()
]);
// End of bootstrap.php