Passed
Push — master ( 067f9c...72cbdc )
by Michal
03:15
created

TrimTokenModifier   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
eloc 17
dl 0
loc 37
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A modify() 0 12 4
A changeText() 0 5 1
1
<?php
2
3
namespace Efabrica\TranslationsAutomatization\TokenModifier;
4
5
use Efabrica\TranslationsAutomatization\Tokenizer\Token;
6
use Efabrica\TranslationsAutomatization\TokenModifier\TokenModifier;
7
8
class TrimTokenModifier extends TokenModifier
9
{
10
    private $prefixCharacterMask;
11
12
    private $suffixCharacterMask;
13
14
    private $affectedTexts;
15
16
    public function __construct(
17
        ?string $prefixCharacterMask = null,
18
        ?string $suffixCharacterMask = null,
19
        int $affectedTexts = Token::TOKEN_ALL
20
    ) {
21
        $this->prefixCharacterMask = $prefixCharacterMask;
22
        $this->suffixCharacterMask = $suffixCharacterMask;
23
        $this->affectedTexts = $affectedTexts;
24
    }
25
26
    protected function modify(Token $token): Token
27
    {
28
        if ($this->affectedTexts & Token::TOKEN_TRANSLATION_KEY) {
29
            $token->changeTranslationKey($this->changeText($token->getTranslationKey()));
30
        }
31
        if ($this->affectedTexts & Token::TOKEN_TRANSLATION_CODE) {
32
            $token->changeTranslationCode($this->changeText($token->getTranslationCode()));
33
        }
34
        if ($this->affectedTexts & Token::TOKEN_TARGET_TEXT) {
35
            $token->changeTargetText($this->changeText($token->getTargetText()));
36
        }
37
        return $token;
38
    }
39
40
    private function changeText(string $text): string
41
    {
42
        $text = ltrim($text, $this->prefixCharacterMask);
43
        $text = rtrim($text, $this->suffixCharacterMask);
44
        return $text;
45
    }
46
}
47