1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace drupol\phpvfs\Utils; |
6
|
|
|
|
7
|
|
|
class Path implements \IteratorAggregate |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var bool |
11
|
|
|
*/ |
12
|
|
|
private $absolute; |
13
|
|
|
/** |
14
|
|
|
* @var string[] |
15
|
|
|
*/ |
16
|
|
|
private $fragments; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
|
|
public function __toString() |
22
|
|
|
{ |
23
|
|
|
return ($this->isAbsolute() ? '/' : '') . \implode('/', $this->getFragments()); |
24
|
|
|
} |
25
|
|
|
|
26
|
2 |
|
public function basename() |
27
|
|
|
{ |
28
|
2 |
|
return $this->getLastPart(); |
29
|
|
|
} |
30
|
|
|
|
31
|
2 |
|
public function dirname() |
32
|
|
|
{ |
33
|
2 |
|
return ($this->isAbsolute() ? '/' : '') . \implode('/', \array_slice($this->getFragments(), 0, -1)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
2 |
|
public static function fromString(string $id) |
40
|
|
|
{ |
41
|
2 |
|
$instance = new self(); |
42
|
2 |
|
$instance->fragments = \explode( |
43
|
2 |
|
\DIRECTORY_SEPARATOR, |
44
|
2 |
|
\ltrim($id, \DIRECTORY_SEPARATOR) |
45
|
|
|
); |
46
|
2 |
|
$instance->absolute = 0 === \strpos($id, '/'); |
47
|
|
|
|
48
|
2 |
|
return $instance; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function getFirstPart() |
55
|
|
|
{ |
56
|
|
|
$first = \reset($this->fragments); |
57
|
|
|
|
58
|
|
|
return empty($first) ? |
59
|
|
|
\DIRECTORY_SEPARATOR : |
60
|
|
|
$first; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
2 |
|
public function getIterator() |
67
|
|
|
{ |
68
|
2 |
|
yield from $this->getFragments(); |
69
|
2 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
2 |
|
public function getLastPart() |
75
|
|
|
{ |
76
|
2 |
|
return \end($this->fragments); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
2 |
|
public function isAbsolute() |
83
|
|
|
{ |
84
|
2 |
|
return $this->absolute; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function isRoot() |
88
|
|
|
{ |
89
|
|
|
return '' === $this->basename(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
|
|
public function isValid() |
96
|
|
|
{ |
97
|
|
|
return \preg_match('/^[^*?"<>|:]*$/', \trim($this->__toString(), ' /')); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
1 |
|
public function shift() |
104
|
|
|
{ |
105
|
1 |
|
return \array_shift($this->fragments); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
2 |
|
protected function getFragments() |
112
|
|
|
{ |
113
|
2 |
|
$fragments = $this->fragments; |
114
|
|
|
|
115
|
2 |
|
if (empty($fragments[0])) { |
116
|
|
|
$fragments[0] = \DIRECTORY_SEPARATOR; |
117
|
|
|
} |
118
|
|
|
|
119
|
2 |
|
return $fragments; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|