Passed
Push — master ( 2e2cc3...b09f9f )
by Sebastian
04:26
created

addURLEncoding()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 21
rs 9.9
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Translator_Syntax_ApacheVelocity} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Translator
7
 * @see Mailcode_Translator_Syntax_ApacheVelocity
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Abstract base class for apache velocity command translation classes. 
16
 *
17
 * @package Mailcode
18
 * @subpackage Translator
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
abstract class Mailcode_Translator_Syntax_ApacheVelocity extends Mailcode_Translator_Command
22
{
23
   /**
24
    * @var string[]
25
    */
26
    private $regexSpecialChars = array(
27
        '?',
28
        '.',
29
        '[',
30
        ']',
31
        '|',
32
        '{',
33
        '}',
34
        '$',
35
        '*',
36
        '^',
37
        '+',
38
        '<',
39
        '>',
40
        '(',
41
        ')'
42
    );
43
44
    public function getLabel(): string
45
    {
46
        return 'Apache Velocity';
47
    }
48
49
    /**
50
    * Filters the string for use in an Apache Velocity (Java)
51
    * regex string: escapes all special characters.
52
    *
53
    * Velocity does its own escaping, so no need to escape special
54
    * characters as if they were a javascript string.
55
    * 
56
    * @param string $string
57
    * @return string
58
    */
59
    public function filterRegexString(string $string) : string
60
    {
61
        // Special case: previously escaped quotes. 
62
        // To avoid modifying them, we strip them out.
63
        $string = str_replace('\\"', 'ESCQUOTE', $string);
64
        
65
        // Any other existing backslashes in the string
66
        // have to be double-escaped, giving four 
67
        // backslashes in the java regex.
68
        $string = str_replace('\\', '\\\\', $string);
69
        
70
        // All other special characters have to be escaped
71
        // with two backslashes. 
72
        foreach($this->regexSpecialChars as $char)
73
        {
74
            $string = str_replace($char, '\\'.$char, $string);
75
        }
76
        
77
        // Restore the escaped quotes, which stay escaped 
78
        // with a single backslash.
79
        $string = str_replace('ESCQUOTE', '\\"', $string);
80
81
        return $string;
82
    }
83
84
    protected function addURLEncoding(Mailcode_Commands_Command $command, string $statement) : string
85
    {
86
        if($command->isURLEncoded())
87
        {
88
            return sprintf(
89
                '${esc.url($%s)}',
90
                $statement
91
            );
92
        }
93
94
        if($command->isURLDecoded())
95
        {
96
            return sprintf(
97
                '${esc.unurl($%s)}',
98
                $statement
99
            );
100
        }
101
102
        return sprintf(
103
            '${%s}',
104
            $statement
105
        );
106
    }
107
}
108