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

/lib/google/src/Google/Utils.php

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