-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.php
88 lines (76 loc) · 2.45 KB
/
Util.php
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
<?php
/**
* SocialConnect project
* @author: Patsura Dmitry https://github.com/ovr <[email protected]>
*/
declare(strict_types=1);
namespace SocialConnect\OAuth1;
class Util
{
/**
* @param mixed $input
* @return array|string
*/
public static function urlencodeRFC3986($input)
{
if (is_int($input)) {
return (string) $input;
}
if (is_array($input)) {
return array_map(array(
__NAMESPACE__ . '\Util',
'urlencodeRFC3986'
), $input);
}
if (is_scalar($input)) {
return rawurlencode($input);
}
$type = gettype($input);
throw new \InvalidArgumentException("Unsupported type: {$type}");
}
/**
* This decode function isn't taking into consideration the above
* modifications to the encoding process. However, this method doesn't
* seem to be used anywhere so leaving it as is.
*
* @param string $string
* @return string
*/
public static function urldecodeRFC3986($string)
{
return urldecode($string);
}
/**
* @param array $params
* @return string
*/
public static function buildHttpQuery(array $params)
{
if (!$params) {
return '';
}
$keys = self::urlencodeRFC3986(array_keys($params));
$values = self::urlencodeRFC3986(array_values($params));
$params = array_combine($keys, $values);
// Parameters are sorted by name, using lexicographical byte value ordering.
// Ref: Spec: 9.1.1 (1)
uksort($params, 'strcmp');
$pairs = [];
foreach ($params as $parameter => $value) {
if (is_array($value)) {
// If two or more parameters share the same name, they are sorted by their value
// Ref: Spec: 9.1.1 (1)
// June 12th, 2010 - changed to sort because of issue 164 by hidetaka
sort($value, SORT_STRING);
foreach ($value as $duplicate_value) {
$pairs[] = $parameter . '=' . $duplicate_value;
}
} else {
$pairs[] = $parameter . '=' . $value;
}
}
// For each parameter, the name is separated from the corresponding value by an '=' character (ASCII code 61)
// Each name-value pair is separated by an '&' character (ASCII code 38)
return implode('&', $pairs);
}
}