Passed
Pull Request — master (#13)
by Sebastian
04:02
created

hasVariableEncodings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

163
    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...
164
    {
165
        $template = $this->encodingTemplates[$keyword] ?? '%s';
166
167
        return sprintf($template, $result);
168
    }
169
}
170