Beispiel #1
0
 /**
  * Sanitizes a value to a certain type.
  *
  * @param string  $type     Type of parameter/item to sanitize.
  * @param mixed   $value    Value to sanitize.
  * @param mixed   $default  Return value, if parameter/item is null/empty/notset/...
  * @param boolean $empty    Must parameter/item be not null or empty.
  * @param object  $messages Messages generated while validation.
  *
  * @return mixed Sanitized value.
  */
 public static function sanitize($type, $value, $default = null, $empty = false, $messages = null)
 {
     if (!isset($messages)) {
         $messages = Cleaner_Engine::getMessages();
     }
     /* If strings are empty they are totally valid as
        long as $empty is not specified */
     if (Cleaner_Util::isBlank($value) && ($type != 'string' || $empty)) {
         if (!$empty) {
             $messages->add('SANITIZE_DEFAULT');
         }
         return $default;
     }
     $instance = Cleaner_Engine::getInstance();
     $result = $instance->sanitize($type, $value);
     if (is_null($result)) {
         if (!$empty) {
             $messages->add('SANITIZE_DEFAULT');
         }
         return $default;
     }
     return $result;
 }
Beispiel #2
0
 /**
  * Validates an item in the wrapped scope for a certain type and if
  * invalid sanitizes the value of the parameter/item.
  *
  * @param string  $type     Type of parameter/item to validate and/or sanitize.
  * @param string  $name     Name of parameter / Name of item in scope.
  * @param boolean $empty    Must parameter/item be not null or empty.
  * @param mixed   $default  Return value, if parameter/item and/or sanitizesparameter/item is null/empty/notset/...
  * @param boolean $sanitize Wheather sanitize value of parameter/item, when value is invalid.
  *
  * @return Cleaner_Parameter Instance, representing selected parameter/item in Scope.
  */
 public function getParameter($type, $name, $empty = false, $default = null, $sanitize = true)
 {
     $messages = Cleaner_Engine::getMessages();
     if (!isset($this->_scope[$name]) || Cleaner_Util::isBlank($this->_scope[$name])) {
         if (!$empty) {
             $messages->add('INVALID_REQUIRED');
         }
         return new Cleaner_Parameter(null, null, $default, $empty, false, true, $messages, $type, $name, $this->_key);
     }
     $engine = Cleaner_Engine::getInstance();
     $value = $this->_scope[$name];
     if ($engine->validate($type, $value, $messages)) {
         return new Cleaner_Parameter(null, $value, $default, true, false, false, $messages, $type, $name, $this->_key);
     }
     if (!$sanitize) {
         return new Cleaner_Parameter(null, $value, $default, false, false, false, $messages, $type, $name, $this->_key);
     }
     $clean = $engine->sanitze($type, $value, $messages);
     if (is_null($clean)) {
         if (!$empty) {
             $messages->add('SANITIZE_DEFAULT');
         }
         return new Cleaner_Parameter(null, null, $default, $empty, true, true, $messages, $type, $name, $this->_key);
     }
     return new Cleaner_Parameter($clean, $value, $default, false, true, false, $messages, $type, $name, $this->_key);
 }