Test Failed
Push — master ( c5c65d...95a748 )
by Sebastian
02:26
created

appendLogicKeywords()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 12
rs 10
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Commands_Highlighter} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Commands
7
 * @see Mailcode_Commands_Highlighter
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Base command class with the common functionality for all commands.
16
 *
17
 * @package Mailcode
18
 * @subpackage Commands
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class Mailcode_Commands_Highlighter
22
{
23
   /**
24
    * @var Mailcode_Commands_Command
25
    */
26
    protected $command;
27
    
28
   /**
29
    * @var string[]
30
    */
31
    protected $parts = array();
32
    
33
    public function __construct(Mailcode_Commands_Command $command)
34
    {
35
       $this->command = $command;
36
    }
37
    
38
    public function highlight() : string
39
    {
40
        $this->parts = array();
41
        
42
        $this->appendBracket('{');
43
        $this->appendCommand();
44
        $this->appendParams($this->command);
45
        $this->appendLogicKeywords();
46
        $this->appendBracket('}');
47
        
48
        return implode('', $this->parts);
49
    }
50
    
51
    protected function appendCommand() : void
52
    {
53
        $this->parts[] = $this->renderTag(array('command-name'), $this->command->getName());
54
        
55
        if($this->command->hasType())
56
        {
57
            $this->parts[] = ' '.$this->renderTag(array('command-type'), $this->command->getType());
58
        }
59
        
60
        if($this->command->requiresParameters())
61
        {
62
            $this->parts[] = $this->renderTag(array('hyphen'), ':');
63
            $this->parts[] = '<wbr>';
64
        }
65
    }
66
    
67
    protected function appendParams(Mailcode_Commands_Command $command) : void
68
    {
69
        $params = $command->getParams();
70
        
71
        if($params === null)
72
        {
73
            return;
74
        }
75
        
76
        $tokens = $params->getInfo()->getTokens();
77
        
78
        $this->parts[] = '<span class="mailcode-params">';
79
        
80
        foreach($tokens as $token)
81
        {
82
            $this->appendParamToken($token);
83
        }
84
        
85
        $this->parts[] = '</span>';
86
    }
87
    
88
    protected function appendParamToken(Mailcode_Parser_Statement_Tokenizer_Token $token) : void
89
    {
90
        $this->parts[] = ' '.$this->renderTag(array('token-'.strtolower($token->getTypeID())), $token->getNormalized());
91
    }
92
    
93
    protected function appendLogicKeywords() : void
94
    {
95
        if(!$this->command->supportsLogicKeywords())
96
        {
97
            return;
98
        }
99
        
100
        $keywords = $this->command->getLogicKeywords()->getKeywords();
101
        
102
        foreach($keywords as $keyword)
103
        {
104
            $this->appendLogicKeyword($keyword);
105
        }
106
    }
107
    
108
    protected function appendLogicKeyword(Mailcode_Commands_LogicKeywords_Keyword $keyword) : void
109
    {
110
        $this->parts[] = ' '.$this->renderTag(array('logic-'.$keyword->getName()), $keyword->getName());
111
        
112
        $type = $keyword->getType();
113
        
114
        if(!empty($type))
115
        {
116
            $this->parts[] = ' '.$this->renderTag(array('command-type'), $type);
117
        }
118
        
119
        $this->parts[] = $this->renderTag(array('hyphen'), ':');
120
        
121
        $this->appendParams($keyword->getCommand());
122
    }
123
    
124
   /**
125
    * @param string[] $classes
126
    * @param string $content
127
    * @return string
128
    */
129
    protected function renderTag(array $classes, string $content) : string
130
    {
131
        $parts = array();
132
        
133
        foreach($classes as $class)
134
        {
135
            $parts[] = 'mailcode-'.$class;
136
        }
137
        
138
        return sprintf(
139
            '<span class="%s">%s</span>',
140
            implode(' ', $parts),
141
            $content
142
        );
143
    }
144
    
145
    protected function appendBracket(string $bracket) : void
146
    {
147
        $this->parts[] = $this->renderTag(
148
            array('bracket'),
149
            $bracket
150
        );
151
    }
152
}
153