Passed
Push — master ( ad7522...d3eb9e )
by Sebastian
05:45
created

compileNumberParams()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 8
nop 2
dl 0
loc 23
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Factory_CommandSets_Set_Show} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Utilities
7
 * @see Mailcode_Factory_CommandSets_Set_Show
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Command set used to create showxxx commands.
16
 *
17
 * @package Mailcode
18
 * @subpackage Utilities
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class Mailcode_Factory_CommandSets_Set_Show extends Mailcode_Factory_CommandSets_Set
22
{
23
    public function var(string $variableName) : Mailcode_Commands_Command_ShowVariable
24
    {
25
        $variableName = $this->instantiator->filterVariableName($variableName);
26
        
27
        $cmd = $this->commands->createCommand(
28
            'ShowVariable',
29
            '',
30
            $variableName,
31
            '{showvar:'.$variableName.'}'
32
        );
33
        
34
        $this->instantiator->checkCommand($cmd);
35
        
36
        if($cmd instanceof Mailcode_Commands_Command_ShowVariable)
37
        {
38
            return $cmd;
39
        }
40
        
41
        throw $this->instantiator->exceptionUnexpectedType('ShowVariable', $cmd);
42
    }
43
    
44
    public function date(string $variableName, string $formatString="") : Mailcode_Commands_Command_ShowDate
45
    {
46
        $variableName = $this->instantiator->filterVariableName($variableName);
47
        
48
        $format = '';
49
        if(!empty($formatString))
50
        {
51
            $format = sprintf(
52
                ' "%s"',
53
                $formatString
54
            );
55
        }
56
        
57
        $cmd = $this->commands->createCommand(
58
            'ShowDate',
59
            '',
60
            $variableName.$format,
61
            sprintf(
62
                '{showdate: %s%s}',
63
                $variableName,
64
                $format
65
            )
66
        );
67
        
68
        $this->instantiator->checkCommand($cmd);
69
        
70
        if($cmd instanceof Mailcode_Commands_Command_ShowDate)
71
        {
72
            return $cmd;
73
        }
74
        
75
        throw $this->instantiator->exceptionUnexpectedType('ShowDate', $cmd);
76
    }
77
78
    public function number(string $variableName, string $formatString="", bool $absolute=false) : Mailcode_Commands_Command_ShowNumber
79
    {
80
        $variableName = $this->instantiator->filterVariableName($variableName);
81
        $paramsString = $this->compileNumberParams($formatString, $absolute);
82
83
        $cmd = $this->commands->createCommand(
84
            'ShowNumber',
85
            '',
86
            $variableName.$paramsString,
87
            sprintf(
88
                '{shownumber: %s%s}',
89
                $variableName,
90
                $paramsString
91
            )
92
        );
93
94
        $this->instantiator->checkCommand($cmd);
95
96
        if($cmd instanceof Mailcode_Commands_Command_ShowNumber)
97
        {
98
            return $cmd;
99
        }
100
101
        throw $this->instantiator->exceptionUnexpectedType('ShowNumber', $cmd);
102
    }
103
104
    private function compileNumberParams(string $formatString="", bool $absolute=false) : string
105
    {
106
        $params = array();
107
108
        if(!empty($formatString))
109
        {
110
            $params[] = sprintf(
111
                ' "%s"',
112
                $formatString
113
            );
114
        }
115
116
        if($absolute)
117
        {
118
            $params[] = ' absolute:';
119
        }
120
121
        if(!empty($params))
122
        {
123
            return ' '.implode(' ', $params);
124
        }
125
126
        return '';
127
    }
128
129
    /**
130
     * Creates a `showphone` command.
131
     *
132
     * @param string $variableName The name of the variable, with or without $ sign.
133
     * @param string $sourceFormat Two-letter country code, case insensitive.
134
     * @param string $urlEncoding The URL encoding mode, if any.
135
     * @return Mailcode_Commands_Command_ShowPhone
136
     * @throws Mailcode_Exception
137
     * @throws Mailcode_Factory_Exception
138
     */
139
    public function phone(string $variableName, string $sourceFormat, string $urlEncoding=Mailcode_Factory::URL_ENCODING_NONE) : Mailcode_Commands_Command_ShowPhone
140
    {
141
        $variableName = $this->instantiator->filterVariableName($variableName);
142
143
        $params = sprintf(
144
            '%s "%s"',
145
            $variableName,
146
            strtoupper($sourceFormat)
147
        );
148
149
        $cmd = $this->commands->createCommand(
150
            'ShowPhone',
151
            '',
152
            $params,
153
            sprintf(
154
                '{showphone: %s}',
155
                $params
156
            )
157
        );
158
159
        $this->instantiator->checkCommand($cmd);
160
        $this->instantiator->setEncoding($cmd, $urlEncoding);
161
162
        if($cmd instanceof Mailcode_Commands_Command_ShowPhone)
163
        {
164
            return $cmd;
165
        }
166
167
        throw $this->instantiator->exceptionUnexpectedType('ShowPhone', $cmd);
168
    }
169
170
    public function snippet(string $snippetName) : Mailcode_Commands_Command_ShowSnippet
171
    {
172
        $snippetName = $this->instantiator->filterVariableName($snippetName);
173
        
174
        $cmd = $this->commands->createCommand(
175
            'ShowSnippet',
176
            '',
177
            $snippetName,
178
            '{showsnippet:'.$snippetName.'}'
179
        );
180
        
181
        $this->instantiator->checkCommand($cmd);
182
        
183
        if($cmd instanceof Mailcode_Commands_Command_ShowSnippet)
184
        {
185
            return $cmd;
186
        }
187
        
188
        throw $this->instantiator->exceptionUnexpectedType('ShowSnippet', $cmd);
189
    }
190
}
191