|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Efabrica\TranslationsAutomatization\Bridge\KdybyTranslation\TokenModifier; |
|
4
|
|
|
|
|
5
|
|
|
use Efabrica\TranslationsAutomatization\Tokenizer\Token; |
|
6
|
|
|
use Efabrica\TranslationsAutomatization\TokenModifier\TokenModifier; |
|
7
|
|
|
|
|
8
|
|
|
class ParamsExtractorTokenModifier extends TokenModifier |
|
9
|
|
|
{ |
|
10
|
|
|
private $staticParamsNameMap = []; |
|
11
|
|
|
|
|
12
|
|
|
private $staticParamsValueMap = []; |
|
13
|
|
|
|
|
14
|
2 |
|
public function __construct(array $staticParamsNameMap = [], array $staticParamsValueMap = []) |
|
15
|
|
|
{ |
|
16
|
2 |
|
$this->staticParamsNameMap = $staticParamsNameMap; |
|
17
|
2 |
|
$this->staticParamsValueMap = $staticParamsValueMap; |
|
18
|
2 |
|
} |
|
19
|
|
|
|
|
20
|
2 |
|
protected function modify(Token $token): Token |
|
21
|
|
|
{ |
|
22
|
2 |
|
$targetText = $token->getTargetText(); |
|
23
|
2 |
|
preg_match_all('/{(.*?)}/', $targetText, $matches); |
|
24
|
2 |
|
|
|
25
|
|
|
$textParameters = []; |
|
26
|
2 |
|
$matchesCount = count($matches[0]); |
|
27
|
2 |
|
for ($i = 0; $i < $matchesCount; ++$i) { |
|
28
|
2 |
|
$paramName = $this->createParamName($matches[1][$i]); |
|
29
|
2 |
|
$paramValue = $this->createParamValue($matches[1][$i]); |
|
30
|
2 |
|
$targetText = str_replace($matches[0][$i], '%' . $paramName . '%', $targetText); |
|
31
|
|
|
$textParameters[$paramName] = $paramValue; |
|
32
|
2 |
|
} |
|
33
|
2 |
|
|
|
34
|
2 |
|
$translationKey = $token->getTargetText() === $token->getTranslationKey() ? $targetText : $token->getTranslationKey(); |
|
35
|
|
|
$token->setTextParameters($textParameters); |
|
36
|
2 |
|
$token->changeTargetText($targetText); |
|
37
|
|
|
$token->changeTranslationKey($translationKey); |
|
38
|
|
|
return $token; |
|
39
|
2 |
|
} |
|
40
|
2 |
|
|
|
41
|
2 |
|
private function createParamName(string $paramName): string |
|
42
|
|
|
{ |
|
43
|
2 |
|
if (isset($this->staticParamsNameMap[$paramName])) { |
|
44
|
|
|
return $this->staticParamsNameMap[$paramName]; |
|
45
|
|
|
} |
|
46
|
2 |
|
|
|
47
|
|
|
if (strpos($paramName, '$') !== 0) { |
|
48
|
2 |
|
return $paramName; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$paramName = str_replace('$', '', $paramName); |
|
52
|
2 |
|
if (strpos($paramName, '->') > 0) { |
|
53
|
2 |
|
$paramName = lcfirst(implode('', array_map('ucfirst', explode('->', $paramName)))); |
|
54
|
2 |
|
} |
|
55
|
|
|
return $paramName; |
|
56
|
|
|
} |
|
57
|
2 |
|
|
|
58
|
|
|
private function createParamValue(string $paramName): string |
|
59
|
|
|
{ |
|
60
|
|
|
if (isset($this->staticParamsValueMap[$paramName])) { |
|
61
|
|
|
return $this->staticParamsValueMap[$paramName]; |
|
62
|
|
|
} |
|
63
|
2 |
|
return $paramName; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|