* @copyright Copyright (c) 2010 United Prototype GmbH (http://unitedprototype.com) */ namespace UnitedPrototype\GoogleAnalytics\Internals; /** * @link http://code.google.com/p/gaforflash/source/browse/trunk/src/com/google/analytics/core/Utils.as */ class Util { /** * This class does only have public static methods, no instantiation necessary */ private function __construct() { } /** * Mimics Javascript's encodeURIComponent() function for consistency with the GA Javascript client. * * @param mixed $value * @return string */ public static function encodeUriComponent($value) { return static::convertToUriComponentEncoding(rawurlencode($value)); } /** * Here as a separate method so it can also be applied to e.g. a http_build_query() result. * * @link http://stackoverflow.com/questions/1734250/what-is-the-equivalent-of-javascripts-encodeuricomponent-in-php/1734255#1734255 * @link http://devpro.it/examples/php_js_escaping.php * * @param string $encodedValue * @return string */ public static function convertToUriComponentEncoding($encodedValue) { return str_replace(array('%21', '%2A', '%27', '%28', '%29'), array('!', '*', "'", '(', ')'), $encodedValue); } /** * Generates a 32bit random number. * * @link http://code.google.com/p/gaforflash/source/browse/trunk/src/com/google/analytics/core/Utils.as#33 * @return int */ public static function generate32bitRandom() { return round((rand() / getrandmax()) * 0x7fffffff); } /** * Generates a hash for input string. * * @link http://code.google.com/p/gaforflash/source/browse/trunk/src/com/google/analytics/core/Utils.as#44 * @param string $string * @return int */ public static function generateHash($string) { $string = (string)$string; $hash = 1; if($string !== null && $string !== '') { $hash = 0; $length = strlen($string); for($pos = $length - 1; $pos >= 0; $pos--) { $current = ord($string[$pos]); $hash = (($hash << 6) & 0xfffffff) + $current + ($current << 14); $leftMost7 = $hash & 0xfe00000; if($leftMost7 != 0) { $hash ^= $leftMost7 >> 21; } } } return $hash; } } ?>