Пример #1
0
	/**
	* @method OC_Shorty_Type::normalize
	* @brief Cleanup and formal normalization of a given value   according to its type
	* Normalizes a given value according to its claimed type.
	* This typically means trimming of string values, but somet  imes also more specific actions.
	* @param mixed value: Value to be normalized
	* @param OC_Shorty_Type::type type: Supposed type of the va  lue
	* @param bool strict: Flag indicating if the normalization   should be done in a strict way
	* @return mixed: The normalized value
	* @throws error Indicating a parameter violation
	* @access public
	* @author Christian Reiner
	*/
	static function normalize ( $value, $type, $strict=FALSE )
	{
		if (NULL===(self::validate($value,$type,$strict)))
		{
			if ( ! $strict)
				return NULL;
			else
				throw new OC_Shorty_Exception ( "invalid value '%1\$s' for type '%2\$s'", array($value,$type) );
		} // if
		switch ( $type )
		{
			case self::ID:
				return trim ( $value );

			case self::STATUS:
				return trim ( $value );

			case self::SORTKEY:
				return trim ( $value );

			case self::SORTVAL:
				return trim ( $value );

			case self::JSON:
				return trim ( $value );

			case self::STRING:
				return trim ( $value );

			case self::URL:
				return trim ( $value );

			case self::PATH:
				return trim ( $value );

			case self::INTEGER:
				return sprintf ( '%d', $value );

			case self::FLOAT:
				return sprintf ( '%f', $value );

			case self::TIMESTAMP:
				return trim ( $value );

			case self::DATE:
				return date ( 'Y-m-d', self::validate($value,OC_Shorty_Type::DATE) );

			case self::BOOLEAN:
				return OC_Shorty_Tools::toBoolean(trim($value)) ? TRUE : FALSE;

		} // switch $type
		throw new OC_Shorty_Exception ( "unknown request argument type '%s'", array($type) );
	} // function normalize