/**
  * Set a scalar filter to use for the FilterArray
  *
  * @param 	KFilterInterface $filter
  * @return	this
  */
 public function setFilter(KFilterInterface $filter)
 {
     $this->_filter = $filter;
     $this->setClassName(array('prefix' => KInflector::getPart($filter, 0), 'base' => 'FilterArray', 'suffix' => KInflector::getPart($filter, 1)));
     return $this;
 }
Beispiel #2
0
 /**
  * Get a validated and optionally sanitized variable from the request. 
  * 
  * When an array of hashes is supplied, the hash will be prioritized in the 
  * same order. Eg. array('post', 'get'). Use this (if you really have to) as
  * a safer equivalent for $_REQUEST
  * 
  * When no sanitizers are supplied, the same filters as the validators will 
  * be used.
  * 
  * @param	string			Variable name eg 'foo[bar]'
  * @param 	string|array  	Hash(es) [COOKIE|ENV|FILES|GET|POST|SERVER]
  * @param 	mixed			Validator(s), can be a KFilterInterface object, or array of objects 
  * @param 	mixed			Sanitizer(s), can be a KFilterInterface object, or array of objects
  * @param 	mixed			Default value when the variable doesn't exist
  * @throws	KInputException	When the variable doesn't validate
  * @return 	mixed			(Sanitized) variable 
  */
 public static function get($var, $hashes, $validators, $sanitizers = array(), $default = null)
 {
     settype($hashes, 'array');
     // Is the hash in our list?
     foreach ($hashes as $k => $hash) {
         $hashes[$k] = strtoupper($hash);
         if (!in_array($hashes[$k], self::$_hashes)) {
             throw new KInputException('Unknown hash: ' . $hash);
         }
     }
     // find $var in the hashes
     $result = null;
     foreach ($hashes as $hash) {
         if ($result = self::_getNested($GLOBALS['_' . $hash], self::_split($var))) {
             break;
         }
     }
     // return the default value if $var wasn't set in any of the hashes
     if (is_null($result)) {
         return $default;
     }
     // trim values
     if (is_scalar($result)) {
         $result = trim($result);
     } else {
         array_walk_recursive($result, 'trim');
     }
     // Handle magic quotes compatability
     if (get_magic_quotes_gpc() && $hash != 'FILES') {
         $result = self::_stripSlashes($result);
     }
     // if $validators or $sanitizers is an object, turn it into an array of objects
     // don't use settype because it will convert objects to arrays
     $validators = is_array($validators) ? $validators : (empty($validators) ? array() : array($validators));
     // if no sanitizers are given, use the validators
     $sanitizers = empty($sanitizers) ? $validators : (is_array($sanitizers) ? $sanitizers : array($sanitizers));
     // validate the variable
     foreach ($validators as $filter) {
         //Create the filter if needed
         if (is_string($filter)) {
             $filter = KFactory::tmp('lib.koowa.filter.' . $filter);
         }
         if (!$filter instanceof KFilterInterface) {
             throw new KInputException('Invalid filter passed: ' . get_class($filter));
         }
         if (!$filter->validate($result)) {
             $filtername = KInflector::getPart(get_class($filter), -1);
             throw new KInputException('Input is not a valid ' . $filtername);
         }
     }
     // sanitize the variable
     foreach ($sanitizers as $filter) {
         //Create the filter if needed
         if (is_string($filter)) {
             $filter = KFactory::tmp('lib.koowa.filter.' . $filter);
         }
         if (!$filter instanceof KFilterInterface) {
             throw new KInputException('Invalid filter passed: ' . get_class($filter));
         }
         $result = $filter->sanitize($result);
     }
     return $result;
 }