|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* League.Uri (http://uri.thephpleague.com) |
|
4
|
|
|
* |
|
5
|
|
|
* @package League.uri |
|
6
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
|
7
|
|
|
* @copyright 2013-2015 Ignace Nyamagana Butera |
|
8
|
|
|
* @license https://github.com/thephpleague/uri/blob/master/LICENSE (MIT License) |
|
9
|
|
|
* @version 4.2.0 |
|
10
|
|
|
* @link https://github.com/thephpleague/uri/ |
|
11
|
|
|
*/ |
|
12
|
|
|
namespace League\Uri\Types; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Uri Parameter validation |
|
16
|
|
|
* |
|
17
|
|
|
* @package League.uri |
|
18
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
|
19
|
|
|
* @since 4.2.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
trait TranscoderTrait |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Encoded Characters regular expression pattern |
|
25
|
|
|
* |
|
26
|
|
|
* @see http://tools.ietf.org/html/rfc3986#section-2.1 |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected static $encodedChars = '[A-Fa-f0-9]{2}'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* RFC3986 Sub delimiter characters regular expression pattern |
|
34
|
|
|
* |
|
35
|
|
|
* @see http://tools.ietf.org/html/rfc3986#section-2.2 |
|
36
|
|
|
* |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected static $subdelimChars = "\!\$&'\(\)\*\+,;\=%"; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* RFC3986 unreserved characters regular expression pattern |
|
43
|
|
|
* |
|
44
|
|
|
* @see http://tools.ietf.org/html/rfc3986#section-2.3 |
|
45
|
|
|
* |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
protected static $unreservedChars = 'A-Za-z0-9_\-\.~'; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* RFC3986 unreserved characters encoded regular expression pattern |
|
52
|
|
|
* |
|
53
|
|
|
* @see http://tools.ietf.org/html/rfc3986#section-2.3 |
|
54
|
|
|
* |
|
55
|
|
|
* @var string |
|
56
|
|
|
*/ |
|
57
|
|
|
protected static $unreservedCharsEncoded = '2[D|E]|3[0-9]|4[1-9|A-F]|5[0-9|A|F]|6[1-9|A-F]|7[0-9|E]'; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Encode a component string |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $str The string to encode |
|
63
|
|
|
* @param string $regexp a regular expression |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
1530 |
|
protected static function encode($str, $regexp) |
|
68
|
|
|
{ |
|
69
|
|
|
$encoder = function (array $matches) { |
|
70
|
231 |
|
return rawurlencode($matches[0]); |
|
71
|
1530 |
|
}; |
|
72
|
|
|
|
|
73
|
1530 |
|
$str = preg_replace_callback($regexp, $encoder, $str); |
|
74
|
|
|
$formatter = function (array $matches) { |
|
75
|
297 |
|
return strtoupper($matches[0]); |
|
76
|
1530 |
|
}; |
|
77
|
|
|
|
|
78
|
1530 |
|
return preg_replace_callback(',%'.self::$encodedChars.',', $formatter, $str); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Encode a path string according to RFC3986 |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $str can be a string or an array |
|
85
|
|
|
* |
|
86
|
|
|
* @return string The same type as the input parameter |
|
87
|
|
|
*/ |
|
88
|
1238 |
|
protected static function encodePath($str) |
|
89
|
|
|
{ |
|
90
|
1238 |
|
$regexp = '/(?:[^'.self::$unreservedChars.self::$subdelimChars.'\:\/@]+| |
|
91
|
1238 |
|
%(?!'.self::$encodedChars.'))/x'; |
|
92
|
|
|
|
|
93
|
1238 |
|
return self::encode($str, $regexp); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Decode a component string |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $str The string to decode |
|
100
|
|
|
* @param string $pattern a regular expression pattern |
|
101
|
|
|
* |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
1687 |
|
protected static function decode($str, $pattern) |
|
105
|
|
|
{ |
|
106
|
1687 |
|
$regexp = ',%'.$pattern.',i'; |
|
107
|
1687 |
|
$decoder = function (array $matches) use ($regexp) { |
|
108
|
|
|
|
|
109
|
132 |
|
if (preg_match($regexp, $matches[0])) { |
|
110
|
36 |
|
return strtoupper($matches[0]); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
99 |
|
return rawurldecode($matches[0]); |
|
114
|
1687 |
|
}; |
|
115
|
|
|
|
|
116
|
1687 |
|
return preg_replace_callback(',%'.self::$encodedChars.',', $decoder, $str); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Decode a component according to RFC3986 |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $str |
|
123
|
|
|
* |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
1056 |
|
protected static function decodeComponent($str) |
|
127
|
|
|
{ |
|
128
|
1056 |
|
return self::decode($str, self::$unreservedCharsEncoded); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Decode a path component according to RFC3986 |
|
133
|
|
|
* |
|
134
|
|
|
* @param string $str |
|
135
|
|
|
* |
|
136
|
|
|
* @return string |
|
137
|
|
|
*/ |
|
138
|
1218 |
|
protected static function decodePath($str) |
|
139
|
|
|
{ |
|
140
|
1218 |
|
return self::decode($str, self::$unreservedCharsEncoded.'|2F'); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|