Passed
Push — master ( 40ca60...430c98 )
by Sebastian
02:56
created

Mailcode_Parser_Safeguard_Formatter_HTMLHighlighting   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 66
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A initFormatting() 0 3 1
A excludeTag() 0 10 2
A getReplaceNeedle() 0 3 1
A excludeTags() 0 8 2
A isTagExcluded() 0 5 1
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Parser_Safeguard_Formatter_HTMLHighlighting} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Parser
7
 * @see Mailcode_Parser_Safeguard_Formatter_HTMLHighlighting
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * HTML highlighting formatter: Ensures that commands that are highlighted
16
 * only in locations where this is possible. Commands nested in tag attributes
17
 * for example, will be ignored.
18
 *
19
 * @package Mailcode
20
 * @subpackage Parser
21
 * @author Sebastian Mordziol <[email protected]>
22
 */
23
class Mailcode_Parser_Safeguard_Formatter_HTMLHighlighting extends Mailcode_Parser_Safeguard_Formatter
24
{
25
   /**
26
    * @var string[]
27
    */
28
    private $excludeTags = array(
29
        'style', // NOTE: style tags are excluded natively on the parser level.
30
        'script'
31
    );
32
    
33
    protected function initFormatting(string $subject) : string
34
    {
35
        return $subject;
36
    }
37
    
38
   /**
39
    * Adds an HTML tag name to the list of tags within which
40
    * commands may not be highlighted.
41
    * 
42
    * @param string $tagName Case insensitive.
43
    * @return Mailcode_Parser_Safeguard_Formatter_HTMLHighlighting
44
    */
45
    public function excludeTag(string $tagName) : Mailcode_Parser_Safeguard_Formatter_HTMLHighlighting
46
    {
47
        $tagName = strtolower($tagName);
48
        
49
        if(!in_array($tagName, $this->excludeTags))
50
        {
51
            $this->excludeTags[] = $tagName;
52
        }
53
        
54
        return $this;
55
    }
56
    
57
   /**
58
    * Adds several exluded tag names at once.
59
    * 
60
    * @param string[] $tagNames
61
    * @return Mailcode_Parser_Safeguard_Formatter_HTMLHighlighting
62
    */
63
    public function excludeTags(array $tagNames) : Mailcode_Parser_Safeguard_Formatter_HTMLHighlighting
64
    {
65
        foreach($tagNames as $tagName)
66
        {
67
            $this->excludeTag((string)$tagName);
68
        }
69
        
70
        return $this;
71
    }
72
    
73
   /**
74
    * Whether the specified tag name is in the exlusion list.
75
    * 
76
    * @param string $tagName
77
    * @return bool
78
    */
79
    public function isTagExcluded(string $tagName) : bool
80
    {
81
        $tagName = strtolower($tagName);
82
        
83
        return in_array($tagName, $this->excludeTags);
84
    }
85
    
86
    public function getReplaceNeedle(Mailcode_Parser_Safeguard_Placeholder $placeholder) : string
87
    {
88
        return '<mailcode:highlight>'.$placeholder->getReplacementText().'</mailcode:highlight>';
89
    }
90
}
91