Google_Utils

Collection of static utility methods used for convenience across the client library.

Defined (1)

The class is defined in the following location(s).

/google-api-php-client/api-code/service/Google_Utils.php  
  1. class Google_Utils { 
  2. public static function urlSafeB64Encode($data) { 
  3. $b64 = base64_encode($data); 
  4. $b64 = str_replace(array('+', '/', '\r', '\n', '='),  
  5. array('-', '_'),  
  6. $b64); 
  7. return $b64; 
  8.  
  9. public static function urlSafeB64Decode($b64) { 
  10. $b64 = str_replace(array('-', '_'),  
  11. array('+', '/'),  
  12. $b64); 
  13. return base64_decode($b64); 
  14.  
  15. /** 
  16. * Misc function used to count the number of bytes in a post body, in the world of multi-byte chars 
  17. * and the unpredictability of strlen/mb_strlen/sizeof, this is the only way to do that in a sane 
  18. * manner at the moment. 
  19. * This algorithm was originally developed for the 
  20. * Solar Framework by Paul M. Jones 
  21. * @link http://solarphp.com/ 
  22. * @link http://svn.solarphp.com/core/trunk/Solar/Json.php 
  23. * @link http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Json/Decoder.php 
  24. * @param string $str 
  25. * @return int The number of bytes in a string. 
  26. */ 
  27. static public function getStrLen($str) { 
  28. $strlenVar = strlen($str); 
  29. $d = $ret = 0; 
  30. for ($count = 0; $count < $strlenVar; ++ $count) { 
  31. $ordinalValue = ord($str{$ret}); 
  32. switch (true) { 
  33. case (($ordinalValue >= 0x20) && ($ordinalValue <= 0x7F)): 
  34. // characters U-00000000 - U-0000007F (same as ASCII) 
  35. $ret ++; 
  36. break; 
  37.  
  38. case (($ordinalValue & 0xE0) == 0xC0): 
  39. // characters U-00000080 - U-000007FF, mask 110XXXXX 
  40. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
  41. $ret += 2; 
  42. break; 
  43.  
  44. case (($ordinalValue & 0xF0) == 0xE0): 
  45. // characters U-00000800 - U-0000FFFF, mask 1110XXXX 
  46. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
  47. $ret += 3; 
  48. break; 
  49.  
  50. case (($ordinalValue & 0xF8) == 0xF0): 
  51. // characters U-00010000 - U-001FFFFF, mask 11110XXX 
  52. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
  53. $ret += 4; 
  54. break; 
  55.  
  56. case (($ordinalValue & 0xFC) == 0xF8): 
  57. // characters U-00200000 - U-03FFFFFF, mask 111110XX 
  58. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
  59. $ret += 5; 
  60. break; 
  61.  
  62. case (($ordinalValue & 0xFE) == 0xFC): 
  63. // characters U-04000000 - U-7FFFFFFF, mask 1111110X 
  64. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
  65. $ret += 6; 
  66. break; 
  67. default: 
  68. $ret ++; 
  69. return $ret; 
  70.  
  71. /** 
  72. * Normalize all keys in an array to lower-case. 
  73. * @param array $arr 
  74. * @return array Normalized array. 
  75. */ 
  76. public static function normalize($arr) { 
  77. if (!is_array($arr)) { 
  78. return array(); 
  79.  
  80. $normalized = array(); 
  81. foreach ($arr as $key => $val) { 
  82. $normalized[strtolower($key)] = $val; 
  83. return $normalized;