Example #1
0
 public static function run($dataName, $key, $type, $default = NULL)
 {
     $val = validate::getData($dataName, $key);
     $arrType = explode('.', $type);
     if (2 === count($arrType)) {
         $data = [];
         if (TRUE === is_array($val)) {
             foreach ($val as $key => $value) {
                 if ($tmp = $this->_run($value, $arrType[1])) {
                     $data[$key] = $tmp;
                 }
             }
         }
         if (TRUE === request::isEmpty($data)) {
             return $default;
         }
         return $data;
     } else {
         return self::_run($val, $type, $default);
     }
 }
Example #2
0
 private static function _run($val, $type, $default = NULL)
 {
     $data = '';
     if (FALSE === request::isEmpty($val)) {
         switch (trim($type)) {
             case 'email':
                 $data = self::email($val);
                 break;
             case 'url':
                 $data = self::url($val);
                 break;
             case 'int':
                 $data = self::int($val);
                 break;
             case 'float':
                 $data = self::float($val);
                 break;
             case 'string':
                 $data = self::string($val);
                 break;
             case 'boolean':
                 $data = self::boolean($val);
                 break;
             case 'htmlentities':
                 $data = self::htmlentities($val);
                 break;
             case 'htmlescape':
                 $data = self::htmlescape($val);
                 break;
             case 'raw':
                 $data = self::raw($val);
                 break;
             default:
                 //error
                 $caller = debug_backtrace()[3];
                 trigger_error("Undefined method: " . $type . " in " . $caller['file'] . ' on line ' . $caller['line'] . ' and defined ', E_USER_NOTICE);
                 return;
                 break;
         }
     }
     if (TRUE === request::isEmpty($data)) {
         if (gettype($default) == "object" && $default instanceof \Closure) {
             $data = $default($val);
         } else {
             $data = $default;
         }
     }
     $valid = TRUE;
     if (FALSE === request::isEmpty($data)) {
         switch ($type) {
             case 'email':
                 $valid = validate::email($data);
                 break;
             case 'url':
                 $valid = validate::url($data);
                 break;
             case 'int':
                 $valid = validate::int($data);
                 break;
             case 'float':
                 $valid = validate::float($data);
                 break;
             default:
                 $valid = $data;
                 break;
         }
     }
     if (TRUE === is_null($data) || FALSE === request::isEmpty($valid)) {
         return $data;
     } else {
         $caller = debug_backtrace()[4];
         if (TRUE === is_null($default)) {
             trigger_error("Argument 1 passed to " . $type . " or NULL must be an instance of " . $caller['function'] . ", " . gettype($data) . " given, called in " . $caller['file'] . ' on line ' . $caller['line'] . '' . ' and defined ', E_USER_NOTICE);
         } else {
             trigger_error("Argument 3 passed to " . $type . " or NULL must be an instance of " . $caller['function'] . ", " . gettype($data) . " given, called in " . $caller['file'] . ' on line ' . $caller['line'] . '' . ' and defined ', E_USER_NOTICE);
         }
     }
 }