Repository navigation
Expand file tree
/
Copy pathTextDocumentUri.php
More file actions
87 lines (71 loc) · 2.45 KB
/
Copy pathTextDocumentUri.php
File metadata and controls
87 lines (71 loc) · 2.45 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
<?php
namespace Phpactor\TextDocument;
use Phpactor\TextDocument\Exception\InvalidUriException;
use Symfony\Component\Filesystem\Path;
class TextDocumentUri
{
public const SCHEME_FILE = 'file';
public const SCHEME_UNTITLED = 'untitled';
public const SCHEME_PHAR = 'phar';
public const SCHEMES = [self::SCHEME_FILE, self::SCHEME_UNTITLED, self::SCHEME_PHAR];
final private function __construct(private string $scheme, private string $path)
{
}
public function __toString(): string
{
if ($this->scheme === self::SCHEME_UNTITLED) {
return sprintf('%s:%s', $this->scheme, $this->path);
}
return sprintf('%s://%s', $this->scheme, $this->path);
}
public static function fromString(?string $uri): self
{
if ($uri === null || $uri === '') {
throw new InvalidUriException(sprintf(
'Could not parse_url "%s"',
$uri
));
}
if (str_starts_with($uri, 'untitled:')) {
return new self(self::SCHEME_UNTITLED, substr($uri, 9));
}
$match = preg_match('{^(?<scheme>[a-z]+://){0,1}(?<path>.+)?}', $uri, $components);
if (!isset($components['scheme']) || $components['scheme'] == '') {
$components['scheme'] = self::SCHEME_FILE;
} else {
$components['scheme'] = substr($components['scheme'], 0, -3);
}
if (!isset($components['path'])) {
throw new InvalidUriException(sprintf(
'URI "%s" has no path component',
$uri
));
}
if (!in_array($components['scheme'], self::SCHEMES)) {
throw new InvalidUriException(sprintf(
'Only "%s" schemes are supported, got "%s"',
implode('", "', self::SCHEMES),
$components['scheme']
));
}
if ($components['scheme'] === self::SCHEME_FILE && false === Path::isAbsolute($uri)) {
throw new InvalidUriException(sprintf(
'URI for file:// must be absolute, got "%s"',
$uri
));
}
$components['path'] = Path::canonicalize($components['path']);
return new self(
$components['scheme'],
$components['path']
);
}
public function path(): string
{
return $this->path;
}
public function scheme(): string
{
return $this->scheme;
}
}