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\Components\Path; |
15
|
|
|
use League\Uri\Interfaces\Uri; |
16
|
|
|
use League\Uri\Modifiers\Filters\Uri as UriFilter; |
17
|
|
|
use Psr\Http\Message\UriInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Resolve an URI according to a base URI using |
21
|
|
|
* RFC3986 rules |
22
|
|
|
* |
23
|
|
|
* @package League.uri |
24
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
25
|
|
|
* @since 4.0.0 |
26
|
|
|
*/ |
27
|
|
|
class Resolve extends AbstractUriModifier |
28
|
|
|
{ |
29
|
|
|
use UriFilter; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* New instance |
33
|
|
|
* |
34
|
|
|
* @param Uri|UriInterface $uri |
35
|
|
|
*/ |
36
|
132 |
|
public function __construct($uri) |
37
|
|
|
{ |
38
|
132 |
|
$this->uri = $this->filterUri($uri); |
|
|
|
|
39
|
132 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Return a Uri object modified according to the modifier |
43
|
|
|
* |
44
|
|
|
* @param Uri|UriInterface $payload |
45
|
|
|
* |
46
|
|
|
* @return Uri|UriInterface |
47
|
|
|
*/ |
48
|
132 |
|
public function __invoke($payload) |
49
|
|
|
{ |
50
|
132 |
|
$this->assertUriObject($payload); |
51
|
132 |
|
$uri = $this->resolveUri($payload); |
52
|
|
|
|
53
|
132 |
|
$path = (new Path($uri->getPath()))->withoutDotSegments(); |
54
|
132 |
|
if ('' !== $uri->getAuthority() && '' !== $path->__toString()) { |
55
|
111 |
|
$path = $path->withLeadingSlash(); |
56
|
74 |
|
} |
57
|
|
|
|
58
|
132 |
|
return $uri->withPath((string) $path); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Resolve the payload URI |
63
|
|
|
* |
64
|
|
|
* @param Uri|UriInterface $payload |
65
|
|
|
* |
66
|
|
|
* @return Uri|UriInterface |
67
|
|
|
*/ |
68
|
132 |
|
protected function resolveUri($payload) |
69
|
|
|
{ |
70
|
132 |
|
if ('' === (string) $payload) { |
71
|
3 |
|
return $this->uri; |
72
|
|
|
} |
73
|
|
|
|
74
|
129 |
|
if ('' !== $payload->getScheme()) { |
75
|
6 |
|
return $payload; |
76
|
|
|
} |
77
|
|
|
|
78
|
123 |
|
if ('' !== $payload->getAuthority()) { |
79
|
3 |
|
return $payload->withScheme($this->uri->getScheme()); |
80
|
|
|
} |
81
|
|
|
|
82
|
80 |
|
return $this |
83
|
120 |
|
->resolveDomain($payload->getPath(), $payload->getQuery()) |
84
|
120 |
|
->withFragment($payload->getFragment()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Resolve the URI for a Authority-less payload URI |
89
|
|
|
* |
90
|
|
|
* @param string $path |
91
|
|
|
* @param string $query |
92
|
|
|
* |
93
|
|
|
* @return Uri|UriInterface |
94
|
|
|
*/ |
95
|
120 |
|
protected function resolveDomain($path, $query) |
96
|
|
|
{ |
97
|
120 |
|
if ('' === $path) { |
98
|
6 |
|
if ('' === $query) { |
99
|
3 |
|
$query = $this->uri->getQuery(); |
100
|
2 |
|
} |
101
|
|
|
|
102
|
6 |
|
return $this->uri->withQuery($query); |
103
|
|
|
} |
104
|
|
|
|
105
|
114 |
|
if (0 === strpos($path, '/')) { |
106
|
9 |
|
return $this->uri->withPath($path)->withQuery($query); |
107
|
|
|
} |
108
|
|
|
|
109
|
105 |
|
return $this->uri |
110
|
105 |
|
->withPath($this->mergePath($path)->__toString()) |
111
|
105 |
|
->withQuery($query); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Merging Relative URI path with Base URI path |
116
|
|
|
* |
117
|
|
|
* @param string $path |
118
|
|
|
* |
119
|
|
|
* @return PathInterface |
120
|
|
|
*/ |
121
|
105 |
|
protected function mergePath($path) |
122
|
|
|
{ |
123
|
105 |
|
$basePath = $this->uri->getPath(); |
124
|
105 |
|
if ('' !== $this->uri->getAuthority() && '' === $basePath) { |
125
|
3 |
|
return (new Path($path))->withLeadingSlash(); |
126
|
|
|
} |
127
|
|
|
|
128
|
102 |
|
if ('' !== $basePath) { |
129
|
102 |
|
$segments = explode('/', $basePath); |
130
|
102 |
|
array_pop($segments); |
131
|
102 |
|
$path = implode('/', $segments).'/'.$path; |
132
|
68 |
|
} |
133
|
|
|
|
134
|
102 |
|
return new Path($path); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.