Ejemplo n.º 1
0
 /**
  * Return filted value.
  *
  * @param   mixed  The value to filter.
  * @return  mixed  The result.
  */
 public function filter($value)
 {
     $type = $this->getOption('type');
     Util\ZendValUtils::convertToExplicitType($value, $type);
     return $value;
 }
Ejemplo n.º 2
0
 /**
  * Returns true, if a given value meets the validation requirements.
  *
  * @param   mixed  The value to validate.
  * @return  bool
  */
 public function isValid($value)
 {
     $result = false;
     $bitMask = $this->getOption('type');
     switch (Util\ZendValUtils::getType($value)) {
         case IS_NULL:
             $result = $bitMask & self::BIT_NULL;
             break;
         case IS_LONG:
             $result = $bitMask & self::BIT_LONG;
             break;
         case IS_DOUBLE:
             $result = $bitMask & self::BIT_DOUBLE;
             break;
         case IS_BOOL:
             $result = $bitMask & self::BIT_BOOL;
             break;
         case IS_ARRAY:
             $result = $bitMask & self::BIT_ARRAY;
             break;
         case IS_OBJECT:
             $result = $bitMask & self::BIT_OBJECT;
             break;
         case IS_STRING:
             $result = $bitMask & self::BIT_STRING;
             break;
         case IS_RESOURCE:
             $result = $bitMask & self::BIT_RESOURCE;
             break;
         case IS_CALLABLE:
             $result = $bitMask & self::BIT_CALLABLE;
             break;
         default:
     }
     if (!$result) {
         $this->reportError(self::IS_ERR_NOT_MATCH_ZVAL_TYPE, $value);
     }
     return $result;
 }
Ejemplo n.º 3
0
 /**
  * Returns true, if a given value meets the validation requirements.
  *
  * @param   mixed  The value to validate.
  * @return  bool
  */
 public function isValid($value)
 {
     $haystack = $this->getOption('haystack');
     $strict = $this->getOption('strict');
     if (static::$PREVENT_STR_TO_LONG_VULNERABILITY) {
         foreach ($haystack as &$h) {
             $type = Util\ZendValUtils::getType($h);
             if (in_array($type, [IS_LONG, IS_DOUBLE])) {
                 Util\ZendValUtils::convertToString($h);
             }
         }
         unset($h);
     }
     $result = in_array($value, $haystack, $strict);
     if (!$result) {
         $this->reportError(self::IS_ERR_NOT_IN_ARRAY);
     }
     return $result;
 }
Ejemplo n.º 4
0
 /**
  * Returns true, if a given value meets the validation requirements.
  *
  * @param   mixed  The value to validate.
  * @return  bool
  */
 public function isValid($value)
 {
     $length = strlen($value);
     $min = $this->getOption('min');
     $max = $this->getOption('max');
     try {
         Util\ZendValUtils::convertToString($value, $strict = true);
     } catch (\Exception $ex) {
         $this->reportError(self::IS_ERR_INVALID, $value);
         return false;
     }
     if ($length < $min) {
         $this->reportError(self::IS_ERR_SHORT);
         return false;
     }
     if ($max !== null && $max < $length) {
         $this->reportError(self::IS_ERR_LONG);
         return false;
     }
     return true;
 }
Ejemplo n.º 5
0
 /**
  * Returns true, if a given value meets the validation requirements.
  *
  * @param   mixed  The value to validate.
  * @return  bool
  */
 public function isValid($value)
 {
     try {
         Util\ZendValUtils::convertToString($value, $strict = true);
     } catch (\Exception $ex) {
         $this->reportError(self::IS_ERR_INVALID, $value);
         return false;
     }
     Util\ErrorHandlerUtils::start();
     $status = preg_match($this->getOption('pattern'), $value);
     Util\ErrorHandlerUtils::stop();
     if ($status === false) {
         $this->reportError(self::IS_ERR_INTERNAL, $value);
         return false;
     }
     if ($status < 1) {
         $this->reportError(self::IS_ERR_NOT_MATCH, $value);
         return false;
     }
     return true;
 }
Ejemplo n.º 6
0
 /**
  * Performs type conversions whenever possible,
  * so that you always receive the data in the format you asked for.
  *
  * @param   array   The input data.
  * @param   string  The type spec format.
  * @param   bool    Throws Exception, if warning conversions is occurred.
  * @return  array   The result data.
  * @throws  Exception\IllegalArgumentException
  *          Exception\ZendTypeCastException
  */
 public static function parse($args, $typeSpec, $strict = false)
 {
     static $symbolToZType = ['l' => IS_LONG, 'L' => IS_LONG, 'd' => IS_DOUBLE, 'b' => IS_BOOL, 'a' => IS_ARRAY, 'A' => IS_ARRAY, 's' => IS_STRING, 'P' => IS_STRING, 'o' => IS_OBJECT, 'O' => IS_OBJECT, 'r' => IS_RESOURCE, 'f' => IS_CALLBACK];
     $typeArgs = [];
     $minNumArgs = -1;
     $maxNumArgs = 0;
     $numArgs = count($args);
     foreach (str_split($typeSpec) as $c) {
         switch ($c) {
             case 'l':
             case 'L':
             case 'd':
             case 'b':
             case 'a':
             case 'A':
             case 's':
             case 'p':
             case 'o':
             case 'O':
             case 'r':
             case 'f':
                 $typeArgs[] = $symbolToZType[$c];
                 $maxNumArgs++;
                 break;
             case '|':
                 $minNumArgs = $maxNumArgs;
                 break;
             default:
                 $message = 'Bad type specifier while parsing parameters';
                 $context = ['typeSpec' => $typeSpec];
                 throw new Exception\IllegalArgumentException($message, -1, $context);
         }
     }
     if ($minNumArgs < 0) {
         $minNumArgs = $maxNumArgs;
     }
     if ($numArgs < $minNumArgs) {
         $message = sprintf('Expect at least parameter %d given', $minNumArgs);
         $context = ['minNumArgs' => $minNumArgs, 'numArgs' => $numArgs];
         throw new Exception\IllegalArgumentException($message, -1, $context);
     }
     // ---------------------------------------------------------------------
     $result = [];
     $loopEnd = min($numArgs, $maxNumArgs);
     for ($index = 0; $index < $loopEnd; $index++) {
         $value = $args[$index];
         if (is_null($value)) {
             $result[$index] = $value;
             continue;
         }
         $ztype = $typeArgs[$index];
         switch ($ztype) {
             case IS_RESOURCE:
             case IS_CALLBACK:
                 if ($ztype !== ZendValUtils::getType($value)) {
                     if ($strict) {
                         $message = 'Given parameters is not matched specified type';
                         $context = ['ztype' => $ztype, 'value' => StringUtils::export($value)];
                         throw new Exception\IllegalArgumentException($message, -1, $context);
                     }
                     $value = null;
                 }
                 $result[$index] = $value;
                 break;
             default:
                 ZendValUtils::convertToExplicitType($value, $ztype, $strict);
                 $result[$index] = $value;
                 break;
         }
     }
     return array_pad($result, $maxNumArgs, null);
 }
Ejemplo n.º 7
0
 /**
  * Returns true, if a given value meets the validation requirements.
  *
  * @param   mixed  The value to validate.
  * @return  bool
  */
 public function isValid($value)
 {
     $isEmpty = false;
     $bitMask = $this->getOption('type');
     switch (Util\ZendValUtils::getType($value)) {
         case IS_NULL:
             $isEmpty = $bitMask & self::BIT_NULL;
             break;
         case IS_LONG:
             $isEmpty = $bitMask & self::BIT_LONG && $value == 0;
             break;
         case IS_DOUBLE:
             $isEmpty = $bitMask & self::BIT_DOUBLE && $value == 0.0;
             break;
         case IS_BOOL:
             $isEmpty = $bitMask & self::BIT_BOOL && $value == false;
             break;
         case IS_ARRAY:
             $isEmpty = $bitMask & self::BIT_ARRAY && $value == [];
             break;
         case IS_STRING:
             $isEmpty = $bitMask & self::BIT_STRING && $value == '' || $bitMask & self::BIT_STRING_ZERO && $value == '0' || $bitMask & self::BIT_STRING_SPACE && preg_match('/^\\s+$/s', $value);
             break;
         case IS_OBJECT:
             $isEmpty = $bitMask & self::BIT_OBJECT_COUNT && ($value instanceof \Countable && count($value) == 0) || $bitMask & self::BIT_OBJECT_STRING && (!method_exists($value, '__toString') || method_exists($value, '__toString') && (string) $value == '');
             break;
         default:
     }
     if ($isEmpty) {
         $this->reportError(self::IS_ERR_IS_EMPTY);
         break;
     }
     return !$isEmpty;
 }
Ejemplo n.º 8
0
 /**
  * Returns a parsable string representation of a variable
  *
  * @param   mixed   The variable you want to export.
  * @param   int     How deep to browse.
  * @return  string  Return the variable representation.
  */
 public static function export($expression, $maxDepth = 5)
 {
     // Declare internal closure to export given parameter.
     $currentDepth = 0;
     $featureFunc = function ($expression) use($maxDepth, $currentDepth, &$featureFunc) {
         if ($maxDepth < ++$currentDepth) {
             return '...';
         }
         switch (ZendValUtils::getType($expression)) {
             case IS_OBJECT:
                 $str = method_exists($expression, '__toString') ? (string) $expression : (method_exists($expression, 'getMessage') ? $expression->getMessage() : sprintf('object(%s)', get_class($expression)));
                 break;
             case IS_ARRAY:
                 $str = '';
                 foreach ($expression as $key => $val) {
                     if (isset($str[0])) {
                         $str .= ', ';
                     }
                     $str .= is_array($val) ? sprintf("'%s' => [%s]", $key, $featureFunc($val, $maxDepth)) : sprintf("'%s' => '%s'", $key, $featureFunc($val, $maxDepth));
                 }
                 $str = "[{$str}]";
                 break;
             case IS_NULL:
                 $str = 'null';
                 break;
             default:
                 $str = $expression;
                 ErrorHandlerUtils::start();
                 ZendValUtils::convertToString($str);
                 ErrorHandlerUtils::stop();
                 break;
         }
         return $str;
     };
     // Invoke the internal closure.
     return $featureFunc($expression, $maxDepth);
 }