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