Google_Utils

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

Defined (2)

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

/includes/api-libs/Google/Utils.php  
  1. class Google_Utils 
  2. public static function urlSafeB64Encode($data) 
  3. $b64 = base64_encode($data); 
  4. $b64 = str_replace( 
  5. array('+', '/', '\r', '\n', '='),  
  6. array('-', '_'),  
  7. $b64 
  8. ); 
  9. return $b64; 
  10.  
  11. public static function urlSafeB64Decode($b64) 
  12. $b64 = str_replace( 
  13. array('-', '_'),  
  14. array('+', '/'),  
  15. $b64 
  16. ); 
  17. return base64_decode($b64); 
  18.  
  19. /** 
  20. * Misc function used to count the number of bytes in a post body, in the 
  21. * world of multi-byte chars and the unpredictability of 
  22. * strlen/mb_strlen/sizeof, this is the only way to do that in a sane 
  23. * manner at the moment. 
  24. * This algorithm was originally developed for the 
  25. * Solar Framework by Paul M. Jones 
  26. * @link http://solarphp.com/ 
  27. * @link http://svn.solarphp.com/core/trunk/Solar/Json.php 
  28. * @link http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Json/Decoder.php 
  29. * @param string $str 
  30. * @return int The number of bytes in a string. 
  31. */ 
  32. public static function getStrLen($str) 
  33. $strlenVar = strlen($str); 
  34. $d = $ret = 0; 
  35. for ($count = 0; $count < $strlenVar; ++ $count) { 
  36. $ordinalValue = ord($str{$ret}); 
  37. switch (true) { 
  38. case (($ordinalValue >= 0x20) && ($ordinalValue <= 0x7F)): 
  39. // characters U-00000000 - U-0000007F (same as ASCII) 
  40. $ret ++; 
  41. break; 
  42. case (($ordinalValue & 0xE0) == 0xC0): 
  43. // characters U-00000080 - U-000007FF, mask 110XXXXX 
  44. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
  45. $ret += 2; 
  46. break; 
  47. case (($ordinalValue & 0xF0) == 0xE0): 
  48. // characters U-00000800 - U-0000FFFF, mask 1110XXXX 
  49. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
  50. $ret += 3; 
  51. break; 
  52. case (($ordinalValue & 0xF8) == 0xF0): 
  53. // characters U-00010000 - U-001FFFFF, mask 11110XXX 
  54. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
  55. $ret += 4; 
  56. break; 
  57. case (($ordinalValue & 0xFC) == 0xF8): 
  58. // characters U-00200000 - U-03FFFFFF, mask 111110XX 
  59. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
  60. $ret += 5; 
  61. break; 
  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; 
  84.  
  85. /** 
  86. * Convert a string to camelCase 
  87. * @param string $value 
  88. * @return string 
  89. */ 
  90. public static function camelCase($value) 
  91. $value = ucwords(str_replace(array('-', '_'), ' ', $value)); 
  92. $value = str_replace(' ', '', $value); 
  93. $value[0] = strtolower($value[0]); 
  94. return $value; 
  1. class Google_Utils 
  2. public static function urlSafeB64Encode($data) 
  3. $b64 = base64_encode($data); 
  4. $b64 = str_replace( 
  5. array('+', '/', '\r', '\n', '='),  
  6. array('-', '_'),  
  7. $b64 
  8. ); 
  9. return $b64; 
  10.  
  11. public static function urlSafeB64Decode($b64) 
  12. $b64 = str_replace( 
  13. array('-', '_'),  
  14. array('+', '/'),  
  15. $b64 
  16. ); 
  17. return base64_decode($b64); 
  18.  
  19. /** 
  20. * Misc function used to count the number of bytes in a post body, in the 
  21. * world of multi-byte chars and the unpredictability of 
  22. * strlen/mb_strlen/sizeof, this is the only way to do that in a sane 
  23. * manner at the moment. 
  24. * This algorithm was originally developed for the 
  25. * Solar Framework by Paul M. Jones 
  26. * @link http://solarphp.com/ 
  27. * @link http://svn.solarphp.com/core/trunk/Solar/Json.php 
  28. * @link http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Json/Decoder.php 
  29. * @param string $str 
  30. * @return int The number of bytes in a string. 
  31. */ 
  32. public static function getStrLen($str) 
  33. $strlenVar = strlen($str); 
  34. $d = $ret = 0; 
  35. for ($count = 0; $count < $strlenVar; ++ $count) { 
  36. $ordinalValue = ord($str{$ret}); 
  37. switch (true) { 
  38. case (($ordinalValue >= 0x20) && ($ordinalValue <= 0x7F)): 
  39. // characters U-00000000 - U-0000007F (same as ASCII) 
  40. $ret ++; 
  41. break; 
  42. case (($ordinalValue & 0xE0) == 0xC0): 
  43. // characters U-00000080 - U-000007FF, mask 110XXXXX 
  44. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
  45. $ret += 2; 
  46. break; 
  47. case (($ordinalValue & 0xF0) == 0xE0): 
  48. // characters U-00000800 - U-0000FFFF, mask 1110XXXX 
  49. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
  50. $ret += 3; 
  51. break; 
  52. case (($ordinalValue & 0xF8) == 0xF0): 
  53. // characters U-00010000 - U-001FFFFF, mask 11110XXX 
  54. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
  55. $ret += 4; 
  56. break; 
  57. case (($ordinalValue & 0xFC) == 0xF8): 
  58. // characters U-00200000 - U-03FFFFFF, mask 111110XX 
  59. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
  60. $ret += 5; 
  61. break; 
  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; 
  84.  
  85. /** 
  86. * Convert a string to camelCase 
  87. * @param string $value 
  88. * @return string 
  89. */ 
  90. public static function camelCase($value) 
  91. $value = ucwords(str_replace(array('-', '_'), ' ', $value)); 
  92. $value = str_replace(' ', '', $value); 
  93. $value[0] = strtolower($value[0]); 
  94. return $value;