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
|
|
|
use InvalidArgumentException; |
15
|
|
|
use League\Uri\Interfaces\UriPart; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Common methods for Component Value Object |
19
|
|
|
* |
20
|
|
|
* @package League.uri |
21
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
22
|
|
|
* @since 4.0.0 |
23
|
|
|
*/ |
24
|
|
|
trait ImmutableComponentTrait |
25
|
|
|
{ |
26
|
|
|
use ValidatorTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Reserved characters list |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected static $reservedCharactersRegex = "\!\$&'\(\)\*\+,;\=\:"; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Invalid characters list |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected static $invalidCharactersRegex; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Asserts the string against RFC3986 rules |
44
|
|
|
* |
45
|
|
|
* @param string $str |
46
|
|
|
* |
47
|
|
|
* @throws InvalidArgumentException If the string is invalid |
48
|
|
|
*/ |
49
|
1337 |
|
protected function assertValidComponent($str) |
50
|
|
|
{ |
51
|
1337 |
|
if (isset(static::$invalidCharactersRegex) && preg_match(static::$invalidCharactersRegex, $str)) { |
52
|
39 |
|
throw new InvalidArgumentException('The component contains invalid characters'); |
53
|
|
|
} |
54
|
1298 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns whether two UriPart objects represent the same value |
58
|
|
|
* The comparison is based on the getUriComponent method |
59
|
|
|
* |
60
|
|
|
* @param UriPart $component |
61
|
|
|
* |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
36 |
|
public function sameValueAs(UriPart $component) |
65
|
|
|
{ |
66
|
36 |
|
return $component->getUriComponent() === $this->getUriComponent(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns the instance string representation |
71
|
|
|
* with its optional URI delimiters |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
abstract public function getUriComponent(); |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Returns the instance string representation; If the |
79
|
|
|
* instance is not defined an empty string is returned |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
abstract public function __toString(); |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Encoding string according to RFC3986 |
87
|
|
|
* |
88
|
|
|
* @param string $str |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
693 |
|
protected static function encode($str) |
93
|
|
|
{ |
94
|
|
|
$encoder = function (array $matches) { |
95
|
675 |
|
return rawurlencode($matches[0]); |
96
|
693 |
|
}; |
97
|
|
|
|
98
|
693 |
|
$formatter = function (array $matches) { |
99
|
24 |
|
return strtoupper($matches['encode']); |
100
|
693 |
|
}; |
101
|
|
|
|
102
|
693 |
|
$str = preg_replace_callback( |
103
|
693 |
|
'/(?:[^'.static::$reservedCharactersRegex.']+|%(?![A-Fa-f0-9]{2}))/S', |
104
|
693 |
|
$encoder, |
105
|
|
|
$str |
106
|
693 |
|
); |
107
|
|
|
|
108
|
693 |
|
return preg_replace_callback(',(?<encode>%[0-9a-f]{2}),', $formatter, $str); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|