Passed
Push — master ( d06d8f...13736a )
by Sebastian
02:30
created

Mailcode_Translator_Syntax::translateSafeguard()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 19
rs 10
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Translator_Syntax} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Translator
7
 * @see Mailcode_Translator_Syntax
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Abstract base class for a translator syntax, allowing the translation
16
 * of mailcode commands into this syntax.
17
 *
18
 * @package Mailcode
19
 * @subpackage Translator
20
 * @author Sebastian Mordziol <[email protected]>
21
 */
22
abstract class Mailcode_Translator_Syntax
23
{
24
    const ERROR_UNKNOWN_COMMAND_TYPE = 50401;
25
    
26
   /**
27
    * Retrieves the syntax' type ID, e.g. "ApacheVelocity".
28
    * @return string
29
    */
30
    public function getTypeID() : string
31
    {
32
        $parts = explode('_', get_class($this));
33
        
34
        return array_pop($parts);
35
    }
36
    
37
   /**
38
    * Translates a single command to the target syntax.
39
    * 
40
    * @param Mailcode_Commands_Command $command
41
    * @throws Mailcode_Translator_Exception
42
    * @return string
43
    */
44
    public function translateCommand(Mailcode_Commands_Command $command) : string
45
    {
46
        $id = $command->getID();
47
        
48
        $method = '_translate'.$id;
49
        
50
        if(!method_exists($this, $method))
51
        {
52
            throw new Mailcode_Translator_Exception(
53
                'Unknown command type in translator',
54
                sprintf(
55
                    'The method [%s] does not exist in [%s].',
56
                    $method,
57
                    get_class($this)
58
                ),
59
                self::ERROR_UNKNOWN_COMMAND_TYPE
60
            );
61
        }
62
        
63
        return $this->$method($command);
64
    }
65
    
66
   /**
67
    * Translates all safeguarded commands in the subject string to the 
68
    * target syntax in one go.
69
    * 
70
    * @param Mailcode_Parser_Safeguard $safeguard
71
    * @return string
72
    */
73
    public function translateSafeguard(Mailcode_Parser_Safeguard $safeguard) : string
74
    {
75
        $subject = $safeguard->makeSafe();
76
        
77
        if(!$safeguard->hasPlaceholders())
78
        {
79
            return $subject;
80
        }
81
        
82
        $placeholders = $safeguard->getPlaceholders();
83
        
84
        $replaces = array();
85
        
86
        foreach($placeholders as $placeholder)
87
        {
88
            $replaces[$placeholder->getReplacementText()] = $this->translateCommand($placeholder->getCommand());
89
        }
90
            
91
        return str_replace(array_keys($replaces), array_values($replaces), $subject);
92
    }
93
    
94
    abstract protected function _translateShowVariable(Mailcode_Commands_Command_ShowVariable $command) : string;
95
        
96
    abstract protected function _translateSetVariable(Mailcode_Commands_Command_SetVariable $command) : string;
97
    
98
    abstract protected function _translateIf(Mailcode_Commands_Command_If $command) : string;
99
    
100
    abstract protected function _translateEnd(Mailcode_Commands_Command_End $command) : string;
101
        
102
    abstract protected function _translateElse(Mailcode_Commands_Command_Else $command) : string;
103
        
104
    abstract protected function _translateElseIf(Mailcode_Commands_Command_ElseIf $command) : string;
105
    
106
    abstract protected function _translateFor(Mailcode_Commands_Command_For $command) : string;
107
}
108