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

HubL::formatVariableName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * @package Mailcode
4
 * @subpackage Translator
5
 */
6
7
declare(strict_types=1);
8
9
namespace Mailcode\Translator\Syntax;
10
11
use Mailcode\Interfaces\Commands\EncodableInterface;
12
use Mailcode\Mailcode;
13
use Mailcode\Mailcode_Commands_Keywords;
14
use Mailcode\Translator\BaseCommandTranslation;
15
use function Mailcode\undollarize;
16
17
/**
18
 * Abstract base class for Hubspot "HubL" command translation classes.
19
 *
20
 * @package Mailcode
21
 * @subpackage Translator
22
 * @author Sebastian Mordziol <[email protected]>
23
 */
24
abstract class HubL extends BaseCommandTranslation
25
{
26
    public function getLabel(): string
27
    {
28
        return 'Hubspot HubL';
29
    }
30
31
    public function getSyntaxName(): string
32
    {
33
        return 'HubL';
34
    }
35
36
    public static function areVariableNamesLowercase() : bool
37
    {
38
        return true;
39
    }
40
41
    protected function formatVariableName(string $name) : string
42
    {
43
        $varName = undollarize($name);
44
45
        if(self::areVariableNamesLowercase()) {
46
            $varName = strtolower($varName);
47
        }
48
49
        return $varName;
50
    }
51
52
    protected function formatVariablesInString(string $subject) : string
53
    {
54
        $variables = Mailcode::create()->createVariables()->parseString($subject)->getAll();
55
56
        foreach($variables as $variable) {
57
            $subject = str_replace($variable->getMatchedText(), $this->formatVariableName($variable->getFullName()), $subject);
58
        }
59
60
        return $subject;
61
    }
62
63
    public function renderQuotedValue(string $value): string
64
    {
65
        return sprintf(
66
            '"%s"',
67
            str_replace('"', '\"', $value)
68
        );
69
    }
70
71
    /**
72
     * @var array<string,string>
73
     */
74
    private array $encodingTemplates = array(
75
        Mailcode_Commands_Keywords::TYPE_URLENCODE => '%s|urlencode',
76
        Mailcode_Commands_Keywords::TYPE_URLDECODE => '%s|urldecode',
77
    );
78
79
    protected function renderEncodings(EncodableInterface $command, string $statement): string
80
    {
81
        $encodings = $command->getActiveEncodings();
82
        $result = $statement;
83
84
        foreach ($encodings as $encoding) {
85
            $result = $this->renderEncoding($encoding, $result, $command);
86
        }
87
88
        return $result;
89
    }
90
91
    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

91
    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...
92
    {
93
        $template = $this->encodingTemplates[$keyword] ?? '%s';
94
95
        return sprintf($template, $result);
96
    }
97
}
98