1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* League.Uri (http://uri.thephpleague.com) |
4
|
|
|
* |
5
|
|
|
* @package League.uri |
6
|
|
|
* @author Ignace Nyamagana Butera <[email protected]> |
7
|
|
|
* @copyright 2016 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 InvalidArgumentException; |
15
|
|
|
use League\Uri\Interfaces\Uri; |
16
|
|
|
use Psr\Http\Message\UriInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* A function to give information about URI Reference |
20
|
|
|
* |
21
|
|
|
* This function returns an associative array representing the URI Reference information: |
22
|
|
|
* each key represents a given state and each value is a boolean to indicate the current URI |
23
|
|
|
* status against the declared state. |
24
|
|
|
* |
25
|
|
|
* <ul> |
26
|
|
|
* <li>absolute_uri: Tell whether the URI is absolute |
27
|
|
|
* <li>network_path: Tell whether the URI is a network-path relative reference |
28
|
|
|
* <li>absolute_path: Tell whether the URI is a absolute-path relative reference |
29
|
|
|
* <li>relative_path: Tell whether the URI is a relative-path relative reference |
30
|
|
|
* <li>same_document: Tell whether the URI is a same-document relative reference |
31
|
|
|
* </ul> |
32
|
|
|
* |
33
|
|
|
* @link https://tools.ietf.org/html/rfc3986#section-4.2 |
34
|
|
|
* @link https://tools.ietf.org/html/rfc3986#section-4.3 |
35
|
|
|
* @link https://tools.ietf.org/html/rfc3986#section-4.4 |
36
|
|
|
* |
37
|
|
|
* @since 4.2.0 |
38
|
|
|
* |
39
|
|
|
* @param Uri|UriInterface $uri The uri to get reference info from |
40
|
|
|
* @param Uri|UriInterface|null $base_uri The base uri to use to get same document reference info |
41
|
|
|
* |
42
|
|
|
* @throws InvalidArgumentException if the submitted Uri is invalid |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
function uri_reference($uri, $base_uri = null) |
47
|
|
|
{ |
48
|
258 |
|
if (!$uri instanceof Uri && !$uri instanceof UriInterface) { |
49
|
3 |
|
throw new InvalidArgumentException( |
50
|
1 |
|
'URI passed must implement PSR-7 UriInterface or League\Uri Uri interface' |
51
|
2 |
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
255 |
|
if (null !== $base_uri && (!$base_uri instanceof Uri && !$base_uri instanceof UriInterface)) { |
55
|
3 |
|
throw new InvalidArgumentException( |
56
|
1 |
|
'The base URI passed must implement PSR-7 UriInterface or League\Uri Uri interface' |
57
|
2 |
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$infos = [ |
61
|
252 |
|
'absolute_uri' => false, |
62
|
168 |
|
'network_path' => false, |
63
|
168 |
|
'absolute_path' => false, |
64
|
168 |
|
'relative_path' => false, |
65
|
168 |
|
'same_document' => false, |
66
|
168 |
|
]; |
67
|
|
|
|
68
|
252 |
|
static $normalizer; |
69
|
252 |
|
if (null === $normalizer) { |
70
|
3 |
|
$normalizer = new Normalize(); |
71
|
2 |
|
} |
72
|
|
|
|
73
|
252 |
|
if (null !== $base_uri) { |
74
|
6 |
|
$uri_string = (string) $normalizer($uri)->withFragment(''); |
75
|
6 |
|
$base_uri_string = (string) $normalizer($base_uri)->withFragment(''); |
76
|
6 |
|
$infos['same_document'] = $uri_string === $base_uri_string; |
77
|
4 |
|
} |
78
|
|
|
|
79
|
252 |
|
if ('' !== $uri->getScheme()) { |
80
|
111 |
|
$infos['absolute_uri'] = true; |
81
|
|
|
|
82
|
111 |
|
return $infos; |
83
|
|
|
} |
84
|
|
|
|
85
|
159 |
|
if ('' !== $uri->getAuthority()) { |
86
|
9 |
|
$infos['network_path'] = true; |
87
|
|
|
|
88
|
9 |
|
return $infos; |
89
|
|
|
} |
90
|
|
|
|
91
|
150 |
|
$path = $uri->getPath(); |
92
|
150 |
|
if (isset($path[0]) && '/' === $path[0]) { |
93
|
12 |
|
$infos['absolute_path'] = true; |
94
|
|
|
|
95
|
12 |
|
return $infos; |
96
|
|
|
} |
97
|
|
|
|
98
|
138 |
|
$infos['relative_path'] = true; |
99
|
|
|
|
100
|
138 |
|
return $infos; |
101
|
|
|
} |
102
|
|
|
|