/google_api/Utils.php

  1. <?php 
  2. /** 
  3. * Copyright 2011 Google Inc. 
  4. * 
  5. * Licensed under the Apache License, Version 2.0 (the "License"); 
  6. * you may not use this file except in compliance with the License. 
  7. * You may obtain a copy of the License at 
  8. * 
  9. * http://www.apache.org/licenses/LICENSE-2.0 
  10. * 
  11. * Unless required by applicable law or agreed to in writing, software 
  12. * distributed under the License is distributed on an "AS IS" BASIS,  
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  14. * See the License for the specific language governing permissions and 
  15. * limitations under the License. 
  16. */ 
  17.  
  18. /** 
  19. * Collection of static utility methods used for convenience across 
  20. * the client library. 
  21. */ 
  22. class Google_Utils 
  23. public static function urlSafeB64Encode($data) 
  24. $b64 = base64_encode($data); 
  25. $b64 = str_replace( 
  26. array('+', '/', '\r', '\n', '='),  
  27. array('-', '_'),  
  28. $b64 
  29. ); 
  30. return $b64; 
  31.  
  32. public static function urlSafeB64Decode($b64) 
  33. $b64 = str_replace( 
  34. array('-', '_'),  
  35. array('+', '/'),  
  36. $b64 
  37. ); 
  38. return base64_decode($b64); 
  39.  
  40. /** 
  41. * Misc function used to count the number of bytes in a post body, in the 
  42. * world of multi-byte chars and the unpredictability of 
  43. * strlen/mb_strlen/sizeof, this is the only way to do that in a sane 
  44. * manner at the moment. 
  45. * 
  46. * This algorithm was originally developed for the 
  47. * Solar Framework by Paul M. Jones 
  48. * 
  49. * @link http://solarphp.com/ 
  50. * @link http://svn.solarphp.com/core/trunk/Solar/Json.php 
  51. * @link http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Json/Decoder.php 
  52. * @param string $str 
  53. * @return int The number of bytes in a string. 
  54. */ 
  55. public static function getStrLen($str) 
  56. $strlenVar = strlen($str); 
  57. $d = $ret = 0; 
  58. for ($count = 0; $count < $strlenVar; ++ $count) { 
  59. $ordinalValue = ord($str{$ret}); 
  60. switch (true) { 
  61. case (($ordinalValue >= 0x20) && ($ordinalValue <= 0x7F)): 
  62. // characters U-00000000 - U-0000007F (same as ASCII) 
  63. $ret ++; 
  64. break; 
  65. case (($ordinalValue & 0xE0) == 0xC0): 
  66. // characters U-00000080 - U-000007FF, mask 110XXXXX 
  67. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
  68. $ret += 2; 
  69. break; 
  70. case (($ordinalValue & 0xF0) == 0xE0): 
  71. // characters U-00000800 - U-0000FFFF, mask 1110XXXX 
  72. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
  73. $ret += 3; 
  74. break; 
  75. case (($ordinalValue & 0xF8) == 0xF0): 
  76. // characters U-00010000 - U-001FFFFF, mask 11110XXX 
  77. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
  78. $ret += 4; 
  79. break; 
  80. case (($ordinalValue & 0xFC) == 0xF8): 
  81. // characters U-00200000 - U-03FFFFFF, mask 111110XX 
  82. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
  83. $ret += 5; 
  84. break; 
  85. case (($ordinalValue & 0xFE) == 0xFC): 
  86. // characters U-04000000 - U-7FFFFFFF, mask 1111110X 
  87. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 
  88. $ret += 6; 
  89. break; 
  90. default: 
  91. $ret ++; 
  92. return $ret; 
  93.  
  94. /** 
  95. * Normalize all keys in an array to lower-case. 
  96. * @param array $arr 
  97. * @return array Normalized array. 
  98. */ 
  99. public static function normalize($arr) 
  100. if (!is_array($arr)) { 
  101. return array(); 
  102.  
  103. $normalized = array(); 
  104. foreach ($arr as $key => $val) { 
  105. $normalized[strtolower($key)] = $val; 
  106. return $normalized; 
  107.  
  108. /** 
  109. * Convert a string to camelCase 
  110. * @param string $value 
  111. * @return string 
  112. */ 
  113. public static function camelCase($value) 
  114. $value = ucwords(str_replace(array('-', '_'), ' ', $value)); 
  115. $value = str_replace(' ', '', $value); 
  116. $value[0] = strtolower($value[0]); 
  117. return $value; 
.