Total Complexity | 4 |
Total Lines | 29 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
8 | class FilePathToKeyTokenModifier implements TokenModifierInterface |
||
9 | { |
||
10 | private $basePath; |
||
11 | |||
12 | private $excludeDirectoryNames; |
||
13 | 6 | ||
14 | public function __construct(string $basePath, array $excludeDirectoryNames = []) |
||
15 | 6 | { |
|
16 | 6 | $this->basePath = rtrim($basePath, '/'); |
|
17 | 6 | $this->excludeDirectoryNames = array_map('strtolower', $excludeDirectoryNames); |
|
18 | } |
||
19 | 6 | ||
20 | public function modifyAll(TokenCollection $tokenCollection): TokenCollection |
||
21 | 6 | { |
|
22 | 6 | $pathParts = array_unique(explode('/', str_replace($this->basePath . '/', '', pathinfo($tokenCollection->getFilePath(), PATHINFO_DIRNAME) . '/' . pathinfo($tokenCollection->getFilePath(), PATHINFO_FILENAME)))); |
|
23 | foreach ($tokenCollection->getTokens() as $token) { |
||
24 | 6 | $newKeyParts = array_filter(array_map([$this, 'toUnderscore'], $pathParts), function ($pathPart) { |
|
25 | 6 | return !in_array($pathPart, $this->excludeDirectoryNames); |
|
26 | 6 | }); |
|
27 | 6 | $newKeyParts[] = $token->getTranslationKey(); |
|
28 | $token->changeTranslationKey(implode('.', $newKeyParts)); |
||
29 | 6 | } |
|
30 | return $tokenCollection; |
||
31 | } |
||
32 | |||
33 | private function toUnderscore(string $string): string |
||
37 | } |
||
38 | } |
||
39 |