|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tleckie\FileResolver; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Resolver |
|
7
|
|
|
* |
|
8
|
|
|
* @package Tleckie\FileResolver |
|
9
|
|
|
* @author Teodoro Leckie Westberg <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class Resolver implements ResolverInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var int */ |
|
14
|
|
|
private const DEFAULT_DEEP = 2; |
|
15
|
|
|
|
|
16
|
|
|
/** @var int */ |
|
17
|
|
|
private const DEFAULT_CHARS = 3; |
|
18
|
|
|
|
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
private string $basePath; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string */ |
|
23
|
|
|
private string $hash; |
|
24
|
|
|
|
|
25
|
|
|
/** @var string */ |
|
26
|
|
|
private string $extension; |
|
27
|
|
|
|
|
28
|
|
|
/** @var int */ |
|
29
|
|
|
private int $deep; |
|
30
|
|
|
|
|
31
|
|
|
/** @var int */ |
|
32
|
|
|
private int $chars; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Resolver constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param string $basePath |
|
38
|
|
|
* @param string $hash |
|
39
|
|
|
* @param string $extension |
|
40
|
|
|
* @param int $deep |
|
41
|
|
|
* @param int $chars |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct( |
|
44
|
|
|
string $basePath, |
|
45
|
|
|
string $hash, |
|
46
|
|
|
string $extension = '', |
|
47
|
|
|
int $deep = self::DEFAULT_DEEP, |
|
48
|
|
|
int $chars = self::DEFAULT_CHARS |
|
49
|
|
|
) { |
|
50
|
|
|
$this->changeBasePath($basePath); |
|
51
|
|
|
$this->changeHash($hash); |
|
52
|
|
|
$this->changeExtension($extension) ; |
|
53
|
|
|
$this->changeDeep($deep); |
|
54
|
|
|
$this->changeChars($chars); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return int |
|
59
|
|
|
*/ |
|
60
|
|
|
public function chars(): int |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->chars; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param int $chars |
|
67
|
|
|
* @return $this |
|
68
|
|
|
*/ |
|
69
|
|
|
public function changeChars(int $chars): self |
|
70
|
|
|
{ |
|
71
|
|
|
$this->chars = $chars; |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
public function hash(): string |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->hash; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $hash |
|
86
|
|
|
* @return $this |
|
87
|
|
|
*/ |
|
88
|
|
|
public function changeHash(string $hash): self |
|
89
|
|
|
{ |
|
90
|
|
|
$this->hash = $hash; |
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
public function extension(): string |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->extension; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $extension |
|
105
|
|
|
* @return $this |
|
106
|
|
|
*/ |
|
107
|
|
|
public function changeExtension(string $extension): self |
|
108
|
|
|
{ |
|
109
|
|
|
$this->extension = $extension; |
|
110
|
|
|
|
|
111
|
|
|
return $this; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return int |
|
116
|
|
|
*/ |
|
117
|
|
|
public function deep(): int |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->deep; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param int $deep |
|
124
|
|
|
* @return $this |
|
125
|
|
|
*/ |
|
126
|
|
|
public function changeDeep(int $deep): self |
|
127
|
|
|
{ |
|
128
|
|
|
$this->deep = $deep; |
|
129
|
|
|
|
|
130
|
|
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
|
|
public function basePath(): string |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->basePath; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param string $basePath |
|
143
|
|
|
* @return $this |
|
144
|
|
|
*/ |
|
145
|
|
|
public function changeBasePath(string $basePath): self |
|
146
|
|
|
{ |
|
147
|
|
|
$this->basePath = rtrim($basePath, DIRECTORY_SEPARATOR); |
|
148
|
|
|
|
|
149
|
|
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
|
|
public function fullName(): string |
|
156
|
|
|
{ |
|
157
|
|
|
return sprintf( |
|
158
|
|
|
'%s%s', |
|
159
|
|
|
$this->fullPath(), |
|
160
|
|
|
$this->file() |
|
161
|
|
|
); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return string |
|
166
|
|
|
*/ |
|
167
|
|
|
public function fullPath(): string |
|
168
|
|
|
{ |
|
169
|
|
|
$path = sprintf( |
|
170
|
|
|
'%s%s%s%s', |
|
171
|
|
|
$this->basePath, |
|
172
|
|
|
DIRECTORY_SEPARATOR, |
|
173
|
|
|
$this->split(), |
|
174
|
|
|
DIRECTORY_SEPARATOR |
|
175
|
|
|
); |
|
176
|
|
|
|
|
177
|
|
|
return $this->normalize($path); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @return string |
|
182
|
|
|
*/ |
|
183
|
|
|
private function split(): string |
|
184
|
|
|
{ |
|
185
|
|
|
$parts = str_split($this->hash, $this->chars); |
|
186
|
|
|
|
|
187
|
|
|
return implode( |
|
188
|
|
|
DIRECTORY_SEPARATOR, |
|
189
|
|
|
array_slice($parts, 0, $this->deep) |
|
190
|
|
|
); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @return string |
|
195
|
|
|
*/ |
|
196
|
|
|
public function file(): string |
|
197
|
|
|
{ |
|
198
|
|
|
if ($this->hasExtension()) { |
|
199
|
|
|
return sprintf('%s.%s', $this->hash, $this->extension); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return $this->hash; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @return bool |
|
207
|
|
|
*/ |
|
208
|
|
|
private function hasExtension(): bool |
|
209
|
|
|
{ |
|
210
|
|
|
return !empty($this->extension); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @param string $value |
|
215
|
|
|
* @return string |
|
216
|
|
|
*/ |
|
217
|
|
|
private function normalize(string $value): string |
|
218
|
|
|
{ |
|
219
|
|
|
return preg_replace( |
|
220
|
|
|
sprintf('#%s{2,}#', DIRECTORY_SEPARATOR), |
|
221
|
|
|
DIRECTORY_SEPARATOR, |
|
222
|
|
|
$value |
|
223
|
|
|
); |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|