Passed
Push — master ( 2851e6...6e90f8 )
by Sebastian
05:27
created

renderNumberFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 10
rs 10
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
    public function renderNumberFormat(string $varName, Mailcode_Number_Info $numberInfo) : string
109
    {
110
        $varName = ltrim($varName, '$');
111
112
        return sprintf(
113
            "price.format(%s, %s, '%s', '%s')",
114
            '$'.$varName,
115
            $numberInfo->getDecimals(),
116
            $numberInfo->getDecimalsSeparator(),
117
            $numberInfo->getThousandsSeparator()
118
        );
119
    }
120
121
    public function renderStringToNumber(string $varName) : string
122
    {
123
        $varName = ltrim($varName, '$');
124
125
        return sprintf(
126
            '$price.toNumber(%s)',
127
            '$'.$varName
128
        );
129
    }
130
}
131