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