Axiom (v1.2.0)

A lightweight PHP framework
H:/Workspace/php-axiom/www/php-axiom/libraries/axiom/axRequest.class.php
Go to the documentation of this file.
00001 <?php
00017 class axRequest {
00018     
00023     protected $_headers;
00024     
00029     protected $_post;
00030     
00035     protected $_get;
00036     
00041     protected $_request;
00042     
00047     protected $_cookies;
00048     
00053     protected $_files;
00054     
00059     protected $_filters;
00060     
00065     protected $_browscap;
00066     
00071     public function __construct ($cache_dir = null) {
00072         $this->_headers  = getallheaders();
00073         $this->_post     = $_POST;
00074         $this->_get      = $_GET;
00075         $this->_request  = $_REQUEST;
00076         $this->_cookies  = $_COOKIE;
00077         $this->_files    = $_FILES;
00078         $this->_filters  = array();
00079         $this->_browscap = ($cache_dir && class_exists('Browscap', true)) ? new Browscap($cache_dir) : null;
00080     }
00081     
00086     public function reset () {
00087         $this->_headers  = getallheaders();
00088         $this->_post     = $_POST;
00089         $this->_get      = $_GET;
00090         $this->_request  = $_REQUEST;
00091         $this->_cookies  = $_COOKIE;
00092         $this->_filters  = array();
00093     }
00094     
00099     public function getHeaders () {
00100         return $this->_headers;
00101     }
00102     
00108     public function headerExists ($header) {
00109         return isset($this->_headers[$header]);
00110     }
00111     
00117     public function getHeader ($header) {
00118         return $this->headerExists($header) ? $this->_headers[$header] : null;
00119     }
00120     
00125     public function getMethod () {
00126         return $this->_server['REQUEST_METHOD'];
00127     }
00128     
00138     public function getEnv ($varname) {
00139         return getenv($varname);
00140     }
00141     
00150     public function getServer ($varname = null) {
00151         if ($varname)
00152             return isset($_SERVER[$varname]) ? $_SERVER[$varname] : null;
00153         
00154         return $_SERVER;
00155     }
00156     
00161     public function getCookies () {
00162         return $this->_cookies;
00163     }
00164     
00170     public function cookieExists ($name) {
00171         return array_key_exists($name, $this->_cookies);
00172     }
00173     
00179     public function getCookie ($name) {
00180         return isset($this->_cookies[$name]) ? $this->_cookies[$name] : null;
00181     }
00182     
00204     public function setFilter (array $filter, $type = INPUT_REQUEST) {
00205         if (!$type = self::_determineType($type))
00206             throw new InvalidArgumentException("Invalid type");
00207             
00208         $this->_filters[$type] = array(
00209             'filter' => $filter,
00210             'flag'   => true,
00211         );
00212         return $this;
00213     }
00214     
00225     public function getParameter ($name, $type = INPUT_REQUEST) {
00226         if (!$type = self::_determineType($type))
00227             throw new InvalidArgumentException("Invalid type");
00228         
00229         if (isset($this->_filters[$type]) && $this->_filters[$type]['flag'])
00230             $this->_applyFilter($type);
00231             
00232         return isset($this->{"_{$type}"}[$name]) ? $this->{"_{$type}"}[$name] : null; 
00233     }
00234     
00244     public function getParameters ($type = INPUT_REQUEST) {
00245         if (!$type = self::_determineType($type))
00246             throw new InvalidArgumentException("Invalid type");
00247             
00248         if (isset($this->_filters[$type]) && $this->_filters[$type]['flag'])
00249             $this->_applyFilter($type);
00250             
00251         return $this->{"_{$type}"};
00252     }
00253     
00265     public function setParameter ($name, $value, $type = INPUT_REQUEST) {
00266         if (!$type = self::_determineType($type))
00267             throw new InvalidArgumentException("Invalid type");
00268             
00269         $this->{"_{$type}"}[$name] = $value;
00270         if (isset($this->_filters[$type]))
00271             $this->_filters[$type]['flag'] = true;
00272         return $this;
00273     }
00274     
00286     public function add ($collection, $method = self::PROPERTY_MERGE, $type = INPUT_REQUEST) {
00287         if (!$type = self::_determineType($type))
00288             throw new InvalidArgumentException("Invalid type");
00289         
00290         switch (strtolower($method)) {
00291             case 'merge':
00292             case self::PROPERTY_MERGE:
00293                 $this->{"_{$type}"} = array_merge($this->{"_{$type}"}, $collection);
00294                 break;
00295             case 'add':
00296             case self::PROPERTY_ADD:
00297                 $this->{"_{$type}"} += $collection;
00298                 break;
00299             default:
00300                 throw new InvalidArgumentException("Invalid method {$method}");
00301         }
00302         if (isset($this->_filters[$type]))
00303             $this->_filters[$type]['flag'] = true;
00304             
00305         return $this;
00306     } 
00307     
00316     public function __get ($key) {
00317         return $this->getParameter($key);
00318     }
00319     
00329     public function __set ($key, $value) {
00330         $this->setParameter($key, $value);
00331     }
00332     
00341     public function getFile ($param_name) {
00342         return isset($this->_files[param_name]) ? $this->_files[param_name] : null;
00343     }
00344     
00350     public function getFiles () {
00351         return $this->_files;
00352     }
00353     
00365     protected function _applyFilter ($type) {
00366         if (!isset($this->_filters[$type]) || !isset($this->{"_{$type}"}))
00367             return false;
00368         
00369         if (!$this->{"_{$type}"} = filter_var_array($this->{"_{$type}"}, $this->_filters[$type]))
00370             throw new RuntimeException("Invalid filter");
00371 
00372         $this->_filters[$type]['flag'] = false;
00373         return $this;
00374     }
00375     
00381     protected static function _determineType ($type) {
00382         $types = array(
00383             INPUT_POST    => 'post', 
00384             INPUT_GET     => 'get', 
00385             INPUT_REQUEST => 'request', 
00386             INPUT_COOKIE  => 'cookie',
00387         );
00388         
00389         $type = strtolower($type);
00390         
00391         if (array_key_exists($type, $types))
00392             $type = $types[$type];
00393         elseif (!in_array($type, $types))
00394             return false;
00395             
00396         return $type;
00397     }
00398     
00403     const PROPERTY_MERGE = 1;
00404     const PROPERTY_ADD   = 2;
00405 }
00406 
00410 defined('INPUT_REQUEST') or define('INPUT_REQUEST', 99);
 All Data Structures Files Functions Variables