Passed
Push — master ( 1b5df6...213867 )
by Sebastian
13:22
created

translate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 7
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 18
rs 10
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Translator_Syntax_ApacheVelocity_ElseIf} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Translator
7
 * @see Mailcode_Translator_Syntax_ApacheVelocity_ElseIf
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Translates the "ElseIf" command to Apache Velocity.
16
 *
17
 * @package Mailcode
18
 * @subpackage Translator
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class Mailcode_Translator_Syntax_ApacheVelocity_ElseIf extends Mailcode_Translator_Syntax_ApacheVelocity implements Mailcode_Translator_Command_ElseIf
22
{
23
    public function translate(Mailcode_Commands_Command_ElseIf $command): string
24
    {
25
        if($command instanceof Mailcode_Commands_Command_ElseIf_Command)
26
        {
27
            return $this->translateCommand($command);
28
        }
29
        
30
        if($command instanceof Mailcode_Commands_Command_ElseIf_Variable)
31
        {
32
            return $this->translateVariable($command);
33
        }
34
        
35
        if($command instanceof Mailcode_Commands_Command_ElseIf_Contains)
36
        {
37
            return $this->translateContains($command);
38
        }
39
        
40
        return '';
41
    }
42
    
43
    protected function translateCommand(Mailcode_Commands_Command_ElseIf_Command $command) : string
44
    {
45
        $params = $command->getParams();
46
        
47
        if(!$params)
0 ignored issues
show
introduced by
$params is of type Mailcode\Mailcode_Parser_Statement, thus it always evaluated to true.
Loading history...
48
        {
49
            return '';
50
        }
51
        
52
        return sprintf(
53
            '#elseif(%s)',
54
            $params->getNormalized()
55
        );
56
    }
57
    
58
    protected function translateVariable(Mailcode_Commands_Command_ElseIf_Variable $command) : string
59
    {
60
        return sprintf(
61
            '#elseif(%s %s %s)',
62
            $command->getVariable()->getFullName(),
63
            $command->getComparator(),
64
            $command->getValue()
65
        );
66
    }
67
    
68
    protected function translateContains(Mailcode_Commands_Command_ElseIf_Contains $command) : string
69
    {
70
        $opts = 's';
71
        if($command->isCaseInsensitive())
72
        {
73
            $opts = 'is';
74
        }
75
        
76
        return sprintf(
77
            '#elseif(%s.matches("(?%s)%s"))',
78
            $command->getVariable()->getFullName(),
79
            $opts,
80
            $this->filterRegexString(trim($command->getSearchTerm(), '"'))
81
        );
82
    }
83
}
84