|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_paths\Extras; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use kalanis\kw_paths\PathsException; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* trait TPathTransform |
|
11
|
|
|
* @package kalanis\kw_paths\Extras |
|
12
|
|
|
* Transform path from string to array and back |
|
13
|
|
|
*/ |
|
14
|
|
|
trait TPathTransform |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @param array<string> $path |
|
18
|
|
|
* @param string $pathDelimiter |
|
19
|
|
|
* @throws PathsException |
|
20
|
|
|
* @return string |
|
21
|
|
|
*/ |
|
22
|
12 |
|
public function compactName(array $path, string $pathDelimiter = DIRECTORY_SEPARATOR): string |
|
23
|
|
|
{ |
|
24
|
12 |
|
if (empty($pathDelimiter)) { |
|
25
|
1 |
|
throw new PathsException($this->noDirectoryDelimiterSet()); |
|
26
|
|
|
} |
|
27
|
11 |
|
return implode( |
|
28
|
11 |
|
$pathDelimiter, |
|
29
|
11 |
|
str_replace( |
|
30
|
11 |
|
$pathDelimiter, |
|
31
|
11 |
|
$this->getEscapeChar() . $pathDelimiter, |
|
32
|
11 |
|
str_replace( |
|
33
|
11 |
|
$this->getEscapeChar(), |
|
34
|
11 |
|
$this->getEscapeChar() . $this->getEscapeChar(), |
|
35
|
11 |
|
$path |
|
36
|
11 |
|
) |
|
37
|
11 |
|
) |
|
38
|
11 |
|
); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $path |
|
43
|
|
|
* @param string $pathDelimiter |
|
44
|
|
|
* @throws PathsException |
|
45
|
|
|
* @return array<string> |
|
46
|
|
|
*/ |
|
47
|
16 |
|
public function expandName(string $path, string $pathDelimiter = DIRECTORY_SEPARATOR): array |
|
48
|
|
|
{ |
|
49
|
16 |
|
$extraDelimiter = "--\e--"; |
|
50
|
16 |
|
if (empty($pathDelimiter)) { |
|
51
|
1 |
|
throw new PathsException($this->noDirectoryDelimiterSet()); |
|
52
|
|
|
} |
|
53
|
15 |
|
$path = str_replace($this->getEscapeChar() . $this->getEscapeChar(), $extraDelimiter . $extraDelimiter, $path); |
|
54
|
15 |
|
$path = str_replace($this->getEscapeChar() . $pathDelimiter, $this->getEscapeChar() . $extraDelimiter, $path); |
|
55
|
15 |
|
$arr = explode($pathDelimiter, $path); |
|
56
|
15 |
|
$arr = str_replace($this->getEscapeChar() . $extraDelimiter, $pathDelimiter, $arr); |
|
57
|
15 |
|
$arr = str_replace($extraDelimiter . $extraDelimiter, $this->getEscapeChar(), $arr); |
|
58
|
15 |
|
return $arr; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
16 |
|
protected function getEscapeChar(): string |
|
62
|
|
|
{ |
|
63
|
16 |
|
return '\\'; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
abstract protected function noDirectoryDelimiterSet(): string; |
|
67
|
|
|
} |
|
68
|
|
|
|