Passed
Push — master ( 138b71...c5c69e )
by Sebastian
04:58
created

translateNotEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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
        if($command instanceof Mailcode_Commands_Command_ElseIf_Empty)
41
        {
42
            return $this->translateEmpty($command);
43
        }
44
        
45
        if($command instanceof Mailcode_Commands_Command_ElseIf_NotEmpty)
46
        {
47
            return $this->translateNotEmpty($command);
48
        }
49
        
50
        return '';
51
    }
52
    
53
    protected function translateCommand(Mailcode_Commands_Command_ElseIf_Command $command) : string
54
    {
55
        $params = $command->getParams();
56
        
57
        if(!$params)
0 ignored issues
show
introduced by
$params is of type Mailcode\Mailcode_Parser_Statement, thus it always evaluated to true.
Loading history...
58
        {
59
            return '';
60
        }
61
        
62
        return sprintf(
63
            '#elseif(%s)',
64
            $params->getNormalized()
65
        );
66
    }
67
    
68
    protected function translateVariable(Mailcode_Commands_Command_ElseIf_Variable $command) : string
69
    {
70
        return sprintf(
71
            '#elseif(%s %s %s)',
72
            $command->getVariable()->getFullName(),
73
            $command->getComparator(),
74
            $command->getValue()
75
        );
76
    }
77
    
78
    protected function translateContains(Mailcode_Commands_Command_ElseIf_Contains $command) : string
79
    {
80
        $opts = 's';
81
        if($command->isCaseInsensitive())
82
        {
83
            $opts = 'is';
84
        }
85
        
86
        return sprintf(
87
            '#elseif(%s.matches("(?%s)%s"))',
88
            $command->getVariable()->getFullName(),
89
            $opts,
90
            $this->filterRegexString(trim($command->getSearchTerm(), '"'))
91
        );
92
    }
93
    
94
    protected function translateEmpty(Mailcode_Commands_Command_ElseIf_Empty $command) : string
95
    {
96
        return $this->_translateEmpty($command->getVariable(), false);
97
    }
98
99
    protected function translateNotEmpty(Mailcode_Commands_Command_ElseIf_NotEmpty $command) : string
100
    {
101
        return $this->_translateEmpty($command->getVariable(), true);
102
    }
103
104
    protected function _translateEmpty(Mailcode_Variables_Variable $variable, bool $notEmpty) : string
105
    {
106
        $sign = '';
107
        
108
        if($notEmpty)
109
        {
110
            $sign = '!';
111
        }
112
        
113
        return sprintf(
114
            '#elseif(%s$StringUtils.isEmpty(%s))',
115
            $sign,
116
            $variable->getFullName()
117
        );
118
    }
119
}
120