Passed
Push — master ( 8482a1...034aeb )
by Sebastian
15:02
created

BaseHubLCommandTranslation::renderEncodings()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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

82
    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...
83
    {
84
        $template = $this->encodingTemplates[$keyword] ?? '%s';
85
86
        return sprintf($template, $result);
87
    }
88
89
    public function renderStringToNumber(string $varName): string
90
    {
91
        return sprintf(
92
            '%s|float',
93
            $this->formatVariableName($varName)
94
        );
95
    }
96
}
97