This repository has been archived by the owner on Jul 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPath.php
83 lines (68 loc) · 1.5 KB
/
Path.php
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
<?php declare (strict_types = 1);
namespace Wavevision\Utils;
use Nette\InvalidStateException;
use Nette\SmartObject;
use function array_map;
use function array_merge;
use function implode;
use function realpath;
use function rtrim;
use function sprintf;
class Path
{
use SmartObject;
public const DELIMITER = '/';
/**
* @var array<string|null>
*/
private array $path;
private function __construct(?string ...$path)
{
$this->path = $path;
}
public static function create(?string ...$path): self
{
return new self(...$path);
}
public static function join(?string ...$parts): string
{
return Strings::replace(
Strings::replace(
implode(self::DELIMITER, array_map([self::class, 'trim'], $parts)),
['#\\\#', '#//+#'],
self::DELIMITER
),
'#:/#',
'://'
);
}
public static function trim(?string $path): ?string
{
if ($path === null) {
return null;
}
return rtrim($path, self::DELIMITER);
}
public static function realpath(string $path): string
{
$realpath = realpath($path);
if ($realpath === false) {
throw new InvalidStateException(
sprintf("Unable to get real path for '%s'. Check if directory exists.", $path)
);
}
return $realpath;
}
public function path(?string ...$path): self
{
return self::create(self::join(...array_merge($this->path, $path)));
}
public function string(?string ...$path): string
{
return (string)$this->path(...$path);
}
public function __toString(): string
{
return self::join(...$this->path);
}
}