Passed
Push — master ( d8dc61...dfdb64 )
by Sebastian
09:05
created

ApacheVelocity::renderQuotedValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
rs 10
cc 1
nc 1
nop 1
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\Translator\Syntax;
13
14
use Mailcode\Interfaces\Commands\EncodableInterface;
15
use Mailcode\Mailcode_Commands_Command;
16
use Mailcode\Mailcode_Commands_Keywords;
17
use Mailcode\Mailcode_Number_Info;
18
use Mailcode\Translator\BaseCommandTranslation;
19
use function Mailcode\dollarize;
20
21
/**
22
 * Abstract base class for apache velocity command translation classes.
23
 *
24
 * @package Mailcode
25
 * @subpackage Translator
26
 * @author Sebastian Mordziol <[email protected]>
27
 */
28
abstract class ApacheVelocity extends BaseCommandTranslation
29
{
30
    /**
31
     * @var string[]
32
     */
33
    private array $regexSpecialChars = array(
34
        '?',
35
        '.',
36
        '[',
37
        ']',
38
        '|',
39
        '{',
40
        '}',
41
        '$',
42
        '*',
43
        '^',
44
        '+',
45
        '<',
46
        '>',
47
        '(',
48
        ')'
49
    );
50
51
    public function getLabel(): string
52
    {
53
        return 'Apache Velocity';
54
    }
55
56
    /**
57
     * Filters the string for use in an Apache Velocity (Java)
58
     * regex string: escapes all special characters.
59
     *
60
     * Velocity does its own escaping, so no need to escape special
61
     * characters as if they were a javascript string.
62
     *
63
     * @param string $string
64
     * @return string
65
     */
66
    public function filterRegexString(string $string): string
67
    {
68
        // Special case: previously escaped quotes.
69
        // To avoid modifying them, we strip them out.
70
        $string = str_replace('\\"', 'ESCQUOTE', $string);
71
72
        // Any other existing backslashes in the string
73
        // have to be double-escaped, giving four
74
        // backslashes in the java regex.
75
        $string = str_replace('\\', '\\\\', $string);
76
77
        // All other special characters have to be escaped
78
        // with two backslashes.
79
        foreach ($this->regexSpecialChars as $char) {
80
            $string = str_replace($char, '\\' . $char, $string);
81
        }
82
83
        // Restore the escaped quotes, which stay escaped
84
        // with a single backslash.
85
        $string = str_replace('ESCQUOTE', '\\"', $string);
86
87
        return $string;
88
    }
89
90
    protected function renderVariableEncodings(Mailcode_Commands_Command $command, string $varName): string
91
    {
92
        if (!$command instanceof EncodableInterface || !$command->hasActiveEncodings()) {
93
            return sprintf(
94
                '${%s}',
95
                $varName
96
            );
97
        }
98
99
        return $this->renderEncodings($command, dollarize($varName));
100
    }
101
102
    public function renderNumberFormat(string $varName, Mailcode_Number_Info $numberInfo, bool $absolute): string
103
    {
104
        $varName = dollarize($varName);
105
106
        if ($absolute) {
107
            $varName = sprintf('${numeric.abs(%s)}', $varName);
108
        }
109
110
        return sprintf(
111
            "numeric.format(%s, %s, '%s', '%s')",
112
            $varName,
113
            $numberInfo->getDecimals(),
114
            $numberInfo->getDecimalsSeparator(),
115
            $numberInfo->getThousandsSeparator()
116
        );
117
    }
118
119
    public function renderStringToNumber(string $varName): string
120
    {
121
        return sprintf(
122
            '$numeric.toNumber(%s)',
123
            dollarize($varName)
124
        );
125
    }
126
127
    public function renderQuotedValue(string $value): string
128
    {
129
        return sprintf(
130
            '"%s"',
131
            str_replace('"', '\"', $value)
132
        );
133
    }
134
135
    public function getSyntaxName(): string
136
    {
137
        return 'ApacheVelocity';
138
    }
139
140
    /**
141
     * @var array<string,string>
142
     */
143
    private array $encodingTemplates = array(
144
        Mailcode_Commands_Keywords::TYPE_URLENCODE => '${esc.url(%s)}',
145
        Mailcode_Commands_Keywords::TYPE_URLDECODE => '${esc.unurl(%s)}',
146
        Mailcode_Commands_Keywords::TYPE_IDN_ENCODE => '${text.idn(%s)}',
147
        Mailcode_Commands_Keywords::TYPE_IDN_DECODE => '${text.unidn(%s)}'
148
    );
149
150
    protected function renderEncodings(EncodableInterface $command, string $statement): string
151
    {
152
        $encodings = $command->getActiveEncodings();
153
        $result = $statement;
154
155
        foreach ($encodings as $encoding) {
156
            $result = $this->renderEncoding($encoding, $result, $command);
157
        }
158
159
        return $result;
160
    }
161
162
    protected function renderEncoding(string $keyword, string $result, EncodableInterface $command): string
0 ignored issues
show
Unused Code introduced by
The parameter $command is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

162
    protected function renderEncoding(string $keyword, string $result, /** @scrutinizer ignore-unused */ EncodableInterface $command): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
163
    {
164
        $template = $this->encodingTemplates[$keyword] ?? '%s';
165
166
        return sprintf($template, $result);
167
    }
168
}
169