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 $pattern a regular expression pattern |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
1512 |
|
protected static function encode($str, $pattern) |
68
|
|
|
{ |
69
|
1512 |
|
$regexp = '/(?:[^'.self::$unreservedChars.$pattern.']+|%(?!'.self::$encodedChars.'))/'; |
70
|
|
|
$encoder = function (array $matches) { |
71
|
228 |
|
return rawurlencode($matches[0]); |
72
|
1512 |
|
}; |
73
|
|
|
|
74
|
1512 |
|
$str = preg_replace_callback($regexp, $encoder, $str); |
75
|
|
|
$formatter = function (array $matches) { |
76
|
294 |
|
return strtoupper($matches[0]); |
77
|
1512 |
|
}; |
78
|
|
|
|
79
|
1512 |
|
return preg_replace_callback(',%'.self::$encodedChars.',', $formatter, $str); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Encode a path string according to RFC3986 |
84
|
|
|
* |
85
|
|
|
* @param string $str can be a string or an array |
86
|
|
|
* |
87
|
|
|
* @return string The same type as the input parameter |
88
|
|
|
*/ |
89
|
1238 |
|
protected static function encodePath($str) |
90
|
|
|
{ |
91
|
1238 |
|
return self::encode($str, self::$subdelimChars.'\:\/@'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Encode a string according to RFC3986 Rules |
96
|
|
|
* |
97
|
|
|
* @param string $str |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
696 |
|
protected static function encodeQueryFragment($str) |
102
|
|
|
{ |
103
|
696 |
|
return self::encode($str, self::$subdelimChars.'\:\/@\?'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Decode a component string |
108
|
|
|
* |
109
|
|
|
* @param string $str The string to decode |
110
|
|
|
* @param string $pattern a regular expression pattern |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
1669 |
|
protected static function decode($str, $pattern) |
115
|
|
|
{ |
116
|
1669 |
|
$regexp = ',%'.$pattern.',i'; |
117
|
1669 |
|
$decoder = function (array $matches) use ($regexp) { |
118
|
|
|
|
119
|
129 |
|
if (preg_match($regexp, $matches[0])) { |
120
|
36 |
|
return strtoupper($matches[0]); |
121
|
|
|
} |
122
|
|
|
|
123
|
96 |
|
return rawurldecode($matches[0]); |
124
|
1669 |
|
}; |
125
|
|
|
|
126
|
1669 |
|
return preg_replace_callback(',%'.self::$encodedChars.',', $decoder, $str); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Decode a component according to RFC3986 |
131
|
|
|
* |
132
|
|
|
* @param string $str |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
1038 |
|
protected static function decodeComponent($str) |
137
|
|
|
{ |
138
|
1038 |
|
return self::decode($str, self::$unreservedCharsEncoded); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Decode a path component according to RFC3986 |
143
|
|
|
* |
144
|
|
|
* @param string $str |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
1218 |
|
protected static function decodePath($str) |
149
|
|
|
{ |
150
|
1218 |
|
return self::decode($str, self::$unreservedCharsEncoded.'|2F'); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|