forked from dunglas/solid-client-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIriHelper.php
More file actions
150 lines (130 loc) · 4.33 KB
/
IriHelper.php
File metadata and controls
150 lines (130 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
/*
* This file is part of the Solid Client PHP project.
* (c) Kévin Dunglas <kevin@dunglas.fr>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Dunglas\PhpSolidClient;
/**
* RFC 3986 §5 relative IRI resolution.
*/
final class IriHelper
{
/**
* Resolves a relative reference against a base URI per RFC 3986 §5.3.
*/
public static function resolve(string $base, string $reference): string
{
// If the reference is already absolute, return it
$r = self::parse($reference);
if (null !== $r['scheme']) {
return self::recompose(
$r['scheme'],
$r['authority'],
self::removeDotSegments($r['path']),
$r['query'],
$r['fragment'],
);
}
$b = self::parse($base);
if (null !== $r['authority']) {
return self::recompose(
$b['scheme'],
$r['authority'],
self::removeDotSegments($r['path']),
$r['query'],
$r['fragment'],
);
}
if ('' === $r['path']) {
$path = $b['path'];
$query = $r['query'] ?? $b['query'];
} else {
if (str_starts_with($r['path'], '/')) {
$path = self::removeDotSegments($r['path']);
} else {
$path = self::merge($b, $r['path']);
$path = self::removeDotSegments($path);
}
$query = $r['query'];
}
return self::recompose($b['scheme'], $b['authority'], $path, $query, $r['fragment']);
}
/**
* @return array{scheme: ?string, authority: ?string, path: string, query: ?string, fragment: ?string}
*/
private static function parse(string $uri): array
{
// RFC 3986 Appendix B regex
preg_match('~^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?~', $uri, $m);
return [
'scheme' => isset($m[2]) && '' !== $m[2] ? $m[2] : null,
'authority' => isset($m[3]) && '' !== $m[3] ? $m[4] : null,
'path' => $m[5] ?? '',
'query' => isset($m[6]) && '' !== $m[6] ? $m[7] : null,
'fragment' => isset($m[8]) && '' !== $m[8] ? $m[9] : null,
];
}
/**
* @param array{scheme: ?string, authority: ?string, path: string, query: ?string, fragment: ?string} $base
*/
private static function merge(array $base, string $referencePath): string
{
if (null !== $base['authority'] && '' === $base['path']) {
return '/'.$referencePath;
}
$lastSlash = strrpos($base['path'], '/');
return false !== $lastSlash
? substr($base['path'], 0, $lastSlash + 1).$referencePath
: $referencePath;
}
private static function removeDotSegments(string $path): string
{
$output = [];
$segments = explode('/', $path);
$absolute = str_starts_with($path, '/');
$trailingSlash = false;
foreach ($segments as $segment) {
if ('.' === $segment) {
$trailingSlash = true;
continue;
}
if ('..' === $segment) {
array_pop($output);
$trailingSlash = true;
continue;
}
$trailingSlash = false;
$output[] = $segment;
}
$result = implode('/', $output);
if ($trailingSlash && !str_ends_with($result, '/')) {
$result .= '/';
}
// Preserve leading slash for absolute paths
if ($absolute && !str_starts_with($result, '/')) {
$result = '/'.$result;
}
return $result;
}
private static function recompose(?string $scheme, ?string $authority, string $path, ?string $query, ?string $fragment): string
{
$result = '';
if (null !== $scheme) {
$result .= $scheme.':';
}
if (null !== $authority) {
$result .= '//'.$authority;
}
$result .= $path;
if (null !== $query) {
$result .= '?'.$query;
}
if (null !== $fragment) {
$result .= '#'.$fragment;
}
return $result;
}
}