|
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\Modifiers; |
|
13
|
|
|
|
|
14
|
|
|
use League\Uri\Interfaces\Uri; |
|
15
|
|
|
use League\Uri\Types\TranscoderTrait; |
|
16
|
|
|
use Psr\Http\Message\UriInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* A class to normalize URI objects |
|
20
|
|
|
* |
|
21
|
|
|
* @package League.uri |
|
22
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
|
23
|
|
|
* @since 4.0.0 |
|
24
|
|
|
*/ |
|
25
|
|
|
class Normalize extends AbstractUriModifier |
|
26
|
|
|
{ |
|
27
|
|
|
use TranscoderTrait; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Return a Uri object modified according to the modifier |
|
31
|
|
|
* |
|
32
|
|
|
* @param Uri|UriInterface $uri |
|
33
|
|
|
* |
|
34
|
|
|
* @return Uri|UriInterface |
|
35
|
|
|
*/ |
|
36
|
36 |
|
public function __invoke($uri) |
|
37
|
|
|
{ |
|
38
|
36 |
|
$modifier = new Pipeline([new HostToAscii(), new KsortQuery()]); |
|
39
|
|
|
|
|
40
|
36 |
|
$path = $uri->getPath(); |
|
41
|
36 |
|
if ('' !== $uri->getScheme().$uri->getAuthority() || (isset($path[0]) && '/' === $path[0])) { |
|
42
|
36 |
|
$modifier = $modifier->pipe(new RemoveDotSegments()); |
|
43
|
24 |
|
} |
|
44
|
|
|
|
|
45
|
36 |
|
return $this->decodeUri($modifier($uri)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
36 |
|
protected function decodeUri($uri) |
|
49
|
|
|
{ |
|
50
|
36 |
|
foreach (['Path', 'Query', 'Fragment'] as $part) { |
|
51
|
36 |
|
$uri = $this->decodeUriPart($uri, $part); |
|
52
|
24 |
|
} |
|
53
|
|
|
|
|
54
|
36 |
|
return $uri; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
36 |
|
protected function decodeUriPart($uri, $property) |
|
58
|
|
|
{ |
|
59
|
36 |
|
$value = preg_replace_callback( |
|
60
|
36 |
|
',%('.self::$unreservedCharsEncoded.'),i', |
|
61
|
36 |
|
function (array $matches) { |
|
62
|
3 |
|
return rawurldecode($matches[0]); |
|
63
|
36 |
|
}, |
|
64
|
36 |
|
call_user_func([$uri, 'get'.$property]) |
|
65
|
24 |
|
); |
|
66
|
|
|
|
|
67
|
36 |
|
return call_user_func([$uri, 'with'.$property], $value); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|