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