/** * 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; }
/** * 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); }