1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_user_paths; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_paths\ArrayPath; |
7
|
|
|
use kalanis\kw_paths\Interfaces\IPaths; |
8
|
|
|
use kalanis\kw_paths\PathsException; |
9
|
|
|
use kalanis\kw_routed_paths\RoutedPath; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class InnerLinks |
14
|
|
|
* Extend known path to user file with user's props |
15
|
|
|
*/ |
16
|
|
|
class InnerLinks |
17
|
|
|
{ |
18
|
|
|
/** @var ArrayPath */ |
19
|
|
|
protected $arrPath = null; |
20
|
|
|
/** @var RoutedPath */ |
21
|
|
|
protected $routedPath = null; |
22
|
|
|
/** @var bool */ |
23
|
|
|
protected $moreUsers = false; |
24
|
|
|
/** @var bool */ |
25
|
|
|
protected $moreLangs = false; |
26
|
|
|
/** @var string[] */ |
27
|
|
|
protected $prefixPath = []; |
28
|
|
|
/** @var bool */ |
29
|
|
|
protected $useSystemPrefix = true; |
30
|
|
|
/** @var bool */ |
31
|
|
|
protected $useDataSeparator = true; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param RoutedPath $routedPath |
35
|
|
|
* @param bool $moreUsers |
36
|
|
|
* @param bool $moreLangs |
37
|
|
|
* @param string[] $prefixPath path to all user data, depends on storage |
38
|
|
|
* @param bool $useSystemPrefix add system dir as prefix |
39
|
|
|
* @param bool $useDataSeparator add system dir as separator of part to user's data itself |
40
|
|
|
*/ |
41
|
172 |
|
public function __construct( |
42
|
|
|
RoutedPath $routedPath, |
43
|
|
|
bool $moreUsers = false, |
44
|
|
|
bool $moreLangs = false, |
45
|
|
|
array $prefixPath = [], |
46
|
|
|
bool $useSystemPrefix = true, |
47
|
|
|
bool $useDataSeparator = true |
48
|
|
|
) { |
49
|
172 |
|
$this->arrPath = new ArrayPath(); |
50
|
172 |
|
$this->routedPath = $routedPath; |
51
|
172 |
|
$this->moreUsers = $moreUsers; |
52
|
172 |
|
$this->moreLangs = $moreLangs; |
53
|
172 |
|
$this->prefixPath = $prefixPath; |
54
|
172 |
|
$this->useSystemPrefix = $useSystemPrefix; |
55
|
172 |
|
$this->useDataSeparator = $useDataSeparator; |
56
|
172 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Path to system things |
60
|
|
|
* @param string $module |
61
|
|
|
* @param string[] $current |
62
|
|
|
* @return string[] |
63
|
|
|
*/ |
64
|
43 |
|
public function toModulePath(string $module, array $current): array |
65
|
|
|
{ |
66
|
43 |
|
return array_merge( |
67
|
43 |
|
$this->prefixPath, |
68
|
43 |
|
$this->useSystemPrefix ? $this->addModuleSeparator() : [], |
69
|
43 |
|
[$module], |
70
|
|
|
$current |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Path to user system things |
76
|
|
|
* @param string[] $current |
77
|
|
|
* @throws PathsException |
78
|
|
|
* @return string[] |
79
|
|
|
*/ |
80
|
43 |
|
public function toUserPath(array $current): array |
81
|
|
|
{ |
82
|
43 |
|
return array_merge( |
83
|
43 |
|
$this->prefixPath, |
84
|
43 |
|
$this->useSystemPrefix ? $this->addPrefixSeparator() : [], |
85
|
43 |
|
$this->moreUsers ? $this->addUser($this->routedPath->getUser()) : [], |
86
|
|
|
$current |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Path to user system things |
92
|
|
|
* @param string $module |
93
|
|
|
* @param string[] $current |
94
|
|
|
* @throws PathsException |
95
|
|
|
* @return string[] |
96
|
|
|
*/ |
97
|
43 |
|
public function toUserModulePath(string $module, array $current): array |
98
|
|
|
{ |
99
|
43 |
|
return array_merge( |
100
|
43 |
|
$this->prefixPath, |
101
|
43 |
|
$this->useSystemPrefix ? $this->addPrefixSeparator() : [], |
102
|
43 |
|
$this->moreUsers ? $this->addUser($this->routedPath->getUser()) : [], |
103
|
43 |
|
$this->useDataSeparator ? $this->addModuleSeparator() : [], |
104
|
43 |
|
[$module], |
105
|
|
|
$current |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Path to user data |
111
|
|
|
* @param string[] $current |
112
|
|
|
* @throws PathsException |
113
|
|
|
* @return string[] |
114
|
|
|
*/ |
115
|
43 |
|
public function toFullPath(array $current): array |
116
|
|
|
{ |
117
|
43 |
|
return array_merge( |
118
|
43 |
|
$this->prefixPath, |
119
|
43 |
|
$this->useSystemPrefix ? $this->addPrefixSeparator() : [], |
120
|
43 |
|
$this->moreUsers ? $this->addUser($this->routedPath->getUser()) : [], |
121
|
43 |
|
$this->useDataSeparator ? $this->addDataSeparator() : [], |
122
|
43 |
|
$this->moreLangs ? $this->addLang($this->routedPath->getLang()) : [], |
123
|
|
|
$current |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return string[] |
129
|
|
|
*/ |
130
|
44 |
|
protected function addModuleSeparator(): array |
131
|
|
|
{ |
132
|
44 |
|
return [IPaths::DIR_MODULE]; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return string[] |
137
|
|
|
*/ |
138
|
66 |
|
protected function addPrefixSeparator(): array |
139
|
|
|
{ |
140
|
66 |
|
return [IPaths::DIR_USER]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param string $user |
145
|
|
|
* @throws PathsException |
146
|
|
|
* @return string[] |
147
|
|
|
*/ |
148
|
60 |
|
protected function addUser(string $user): array |
149
|
|
|
{ |
150
|
60 |
|
return $this->arrPath->setString($user)->getArray(); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return string[] |
155
|
|
|
*/ |
156
|
22 |
|
protected function addDataSeparator(): array |
157
|
|
|
{ |
158
|
22 |
|
return [IPaths::DIR_DATA]; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param string $lang |
163
|
|
|
* @return string[] |
164
|
|
|
* @throws PathsException |
165
|
|
|
*/ |
166
|
20 |
|
protected function addLang(string $lang): array |
167
|
|
|
{ |
168
|
20 |
|
return $this->arrPath->setString($lang)->getArray(); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|