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