PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Lampcms/Modules/Google/src/service/apiUtils.php

http://github.com/snytkine/LampCMS
PHP | 117 lines | 56 code | 10 blank | 51 comment | 9 complexity | 3fa3e81cba70de08a571d0b189d59377 MD5 | raw file
Possible License(s): LGPL-3.0
  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. * Collection of static utility methods used for convenience across
  19. * the client library.
  20. *
  21. * @author Chirag Shah <chirags@google.com>
  22. */
  23. class apiUtils {
  24. public static function urlSafeB64Encode($data) {
  25. $b64 = base64_encode($data);
  26. $b64 = str_replace(array('+', '/', '\r', '\n', '='),
  27. array('-', '_'),
  28. $b64);
  29. return $b64;
  30. }
  31. public static function urlSafeB64Decode($b64) {
  32. $b64 = str_replace(array('-', '_'),
  33. array('+', '/'),
  34. $b64);
  35. return base64_decode($b64);
  36. }
  37. /**
  38. * Misc function used to count the number of bytes in a post body, in the world of multi-byte chars
  39. * and the unpredictability of strlen/mb_strlen/sizeof, this is the only way to do that in a sane
  40. * manner at the moment.
  41. *
  42. * This algorithm was originally developed for the
  43. * Solar Framework by Paul M. Jones
  44. *
  45. * @link http://solarphp.com/
  46. * @link http://svn.solarphp.com/core/trunk/Solar/Json.php
  47. * @link http://framework.zend.com/svn/framework/standard/trunk/library/Zend/Json/Decoder.php
  48. * @param string $str
  49. * @return int The number of bytes in a string.
  50. */
  51. static public function getStrLen($str) {
  52. $strlenVar = strlen($str);
  53. $d = $ret = 0;
  54. for ($count = 0; $count < $strlenVar; ++ $count) {
  55. $ordinalValue = ord($str{$ret});
  56. switch (true) {
  57. case (($ordinalValue >= 0x20) && ($ordinalValue <= 0x7F)):
  58. // characters U-00000000 - U-0000007F (same as ASCII)
  59. $ret ++;
  60. break;
  61. case (($ordinalValue & 0xE0) == 0xC0):
  62. // characters U-00000080 - U-000007FF, mask 110XXXXX
  63. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  64. $ret += 2;
  65. break;
  66. case (($ordinalValue & 0xF0) == 0xE0):
  67. // characters U-00000800 - U-0000FFFF, mask 1110XXXX
  68. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  69. $ret += 3;
  70. break;
  71. case (($ordinalValue & 0xF8) == 0xF0):
  72. // characters U-00010000 - U-001FFFFF, mask 11110XXX
  73. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  74. $ret += 4;
  75. break;
  76. case (($ordinalValue & 0xFC) == 0xF8):
  77. // characters U-00200000 - U-03FFFFFF, mask 111110XX
  78. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  79. $ret += 5;
  80. break;
  81. case (($ordinalValue & 0xFE) == 0xFC):
  82. // characters U-04000000 - U-7FFFFFFF, mask 1111110X
  83. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  84. $ret += 6;
  85. break;
  86. default:
  87. $ret ++;
  88. }
  89. }
  90. return $ret;
  91. }
  92. /**
  93. * Normalize all keys in an array to lower-case.
  94. * @param array $arr
  95. * @return array Normalized array.
  96. */
  97. public static function normalize($arr) {
  98. if (!is_array($arr)) {
  99. return array();
  100. }
  101. $normalized = array();
  102. foreach ($arr as $key => $val) {
  103. $normalized[strtolower($key)] = $val;
  104. }
  105. return $normalized;
  106. }
  107. }