|
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; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* a class to encode or decode Query and Fragment components |
|
16
|
|
|
* |
|
17
|
|
|
* @package League.uri |
|
18
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
|
19
|
|
|
* @since 4.2.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
class Transcoder |
|
22
|
|
|
{ |
|
23
|
|
|
const REGEXP_CHARACTERS_RESERVED = "/(?:[^\!\$&'\(\)\*\+,;\=\:\?\/@%]+|%(?![A-Fa-f0-9]{2}))/S"; |
|
24
|
|
|
|
|
25
|
|
|
const REGEXP_CHARACTERS_ENCODED = ',%(?<encode>[0-9a-fA-F]{2}),'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Encode a string according to RFC3986 Rules |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $subject |
|
31
|
|
|
* |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
699 |
|
public function encode($subject) |
|
35
|
|
|
{ |
|
36
|
|
|
$encoder = function (array $matches) { |
|
37
|
696 |
|
return rawurlencode($matches[0]); |
|
38
|
699 |
|
}; |
|
39
|
|
|
|
|
40
|
|
|
$formatter = function (array $matches) { |
|
41
|
111 |
|
return strtoupper($matches[0]); |
|
42
|
699 |
|
}; |
|
43
|
|
|
|
|
44
|
699 |
|
$subject = preg_replace_callback(self::REGEXP_CHARACTERS_RESERVED, $encoder, $subject); |
|
45
|
|
|
|
|
46
|
699 |
|
return preg_replace_callback(self::REGEXP_CHARACTERS_ENCODED, $formatter, $subject); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Decode a string according to RFC3986 Rules |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $subject |
|
53
|
|
|
* |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
public function decode($subject) |
|
57
|
|
|
{ |
|
58
|
696 |
|
return preg_replace_callback(self::REGEXP_CHARACTERS_ENCODED, function ($matches) { |
|
59
|
|
|
|
|
60
|
108 |
|
$decode = chr(hexdec($matches['encode'])); |
|
61
|
108 |
|
if (preg_match('/[\w|\._~]+/', $decode)) { |
|
62
|
6 |
|
return strtoupper($matches[0]); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
102 |
|
if (preg_match('/[\[\]\+\?:]+/', $decode)) { |
|
66
|
15 |
|
return $decode; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
96 |
|
return rawurldecode($matches[0]); |
|
70
|
|
|
|
|
71
|
696 |
|
}, $this->encode($subject)); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|