Passed
Push — master ( af11e6...9ff364 )
by Sebastian
03:46
created

Mailcode_Factory_CommandSets_Set_Show::number()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 3
dl 0
loc 24
rs 9.7998
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 Factory
7
 * @see Mailcode_Factory_CommandSets_Set_Show
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
use Mailcode\Parser\PreParser;
15
16
/**
17
 * Command set used to create showxxx commands.
18
 *
19
 * @package Mailcode
20
 * @subpackage Factory
21
 * @author Sebastian Mordziol <[email protected]>
22
 */
23
class Mailcode_Factory_CommandSets_Set_Show extends Mailcode_Factory_CommandSets_Set
24
{
25
    public function var(string $variableName) : Mailcode_Commands_Command_ShowVariable
26
    {
27
        $variableName = $this->instantiator->filterVariableName($variableName);
28
        
29
        $cmd = $this->commands->createCommand(
30
            'ShowVariable',
31
            '',
32
            $variableName,
33
            '{showvar:'.$variableName.'}'
34
        );
35
        
36
        $this->instantiator->checkCommand($cmd);
37
        
38
        if($cmd instanceof Mailcode_Commands_Command_ShowVariable)
39
        {
40
            return $cmd;
41
        }
42
        
43
        throw $this->instantiator->exceptionUnexpectedType('ShowVariable', $cmd);
44
    }
45
    
46
    public function date(string $variableName, string $formatString="") : Mailcode_Commands_Command_ShowDate
47
    {
48
        $variableName = $this->instantiator->filterVariableName($variableName);
49
        
50
        $format = '';
51
        if(!empty($formatString))
52
        {
53
            $format = sprintf(
54
                ' "%s"',
55
                $formatString
56
            );
57
        }
58
        
59
        $cmd = $this->commands->createCommand(
60
            'ShowDate',
61
            '',
62
            $variableName.$format,
63
            sprintf(
64
                '{showdate: %s%s}',
65
                $variableName,
66
                $format
67
            )
68
        );
69
        
70
        $this->instantiator->checkCommand($cmd);
71
        
72
        if($cmd instanceof Mailcode_Commands_Command_ShowDate)
73
        {
74
            return $cmd;
75
        }
76
        
77
        throw $this->instantiator->exceptionUnexpectedType('ShowDate', $cmd);
78
    }
79
80
    public function number(string $variableName, string $formatString="", bool $absolute=false) : Mailcode_Commands_Command_ShowNumber
81
    {
82
        $variableName = $this->instantiator->filterVariableName($variableName);
83
        $paramsString = $this->compileNumberParams($formatString, $absolute);
84
85
        $cmd = $this->commands->createCommand(
86
            'ShowNumber',
87
            '',
88
            $variableName.$paramsString,
89
            sprintf(
90
                '{shownumber: %s%s}',
91
                $variableName,
92
                $paramsString
93
            )
94
        );
95
96
        $this->instantiator->checkCommand($cmd);
97
98
        if($cmd instanceof Mailcode_Commands_Command_ShowNumber)
99
        {
100
            return $cmd;
101
        }
102
103
        throw $this->instantiator->exceptionUnexpectedType('ShowNumber', $cmd);
104
    }
105
106
    private function compileNumberParams(string $formatString="", bool $absolute=false) : string
107
    {
108
        $params = array();
109
110
        if(!empty($formatString))
111
        {
112
            $params[] = sprintf(
113
                ' "%s"',
114
                $formatString
115
            );
116
        }
117
118
        if($absolute)
119
        {
120
            $params[] = ' absolute:';
121
        }
122
123
        if(!empty($params))
124
        {
125
            return ' '.implode(' ', $params);
126
        }
127
128
        return '';
129
    }
130
131
    /**
132
     * Creates a `showphone` command.
133
     *
134
     * @param string $variableName The name of the variable, with or without $ sign.
135
     * @param string $sourceFormat Two-letter country code, case insensitive.
136
     * @param string $urlEncoding The URL encoding mode, if any.
137
     * @return Mailcode_Commands_Command_ShowPhone
138
     * @throws Mailcode_Exception
139
     * @throws Mailcode_Factory_Exception
140
     */
141
    public function phone(string $variableName, string $sourceFormat, string $urlEncoding=Mailcode_Factory::URL_ENCODING_NONE) : Mailcode_Commands_Command_ShowPhone
142
    {
143
        $variableName = $this->instantiator->filterVariableName($variableName);
144
145
        $params = sprintf(
146
            '%s "%s"',
147
            $variableName,
148
            strtoupper($sourceFormat)
149
        );
150
151
        $cmd = $this->commands->createCommand(
152
            'ShowPhone',
153
            '',
154
            $params,
155
            sprintf(
156
                '{showphone: %s}',
157
                $params
158
            )
159
        );
160
161
        $this->instantiator->checkCommand($cmd);
162
        $this->instantiator->setEncoding($cmd, $urlEncoding);
163
164
        if($cmd instanceof Mailcode_Commands_Command_ShowPhone)
165
        {
166
            return $cmd;
167
        }
168
169
        throw $this->instantiator->exceptionUnexpectedType('ShowPhone', $cmd);
170
    }
171
172
    public function snippet(string $snippetName) : Mailcode_Commands_Command_ShowSnippet
173
    {
174
        $snippetName = $this->instantiator->filterVariableName($snippetName);
175
        
176
        $cmd = $this->commands->createCommand(
177
            'ShowSnippet',
178
            '',
179
            $snippetName,
180
            '{showsnippet:'.$snippetName.'}'
181
        );
182
        
183
        $this->instantiator->checkCommand($cmd);
184
        
185
        if($cmd instanceof Mailcode_Commands_Command_ShowSnippet)
186
        {
187
            return $cmd;
188
        }
189
        
190
        throw $this->instantiator->exceptionUnexpectedType('ShowSnippet', $cmd);
191
    }
192
193
    /**
194
     * @param string $url The target URL. Can contain Mailcode.
195
     * @param string|null $trackingID If not set, an auto-generated tracking ID will be used.
196
     * @param array<string,string> $queryParams
197
     * @return Mailcode_Commands_Command_ShowURL
198
     * @throws Mailcode_Exception
199
     * @throws Mailcode_Factory_Exception
200
     */
201
    public function url(string $url, ?string $trackingID=null, array $queryParams=array()) : Mailcode_Commands_Command_ShowURL
202
    {
203
        $contentID = PreParser::storeContent($url);
204
205
        $params = array();
206
        $params[] = (string)$contentID;
207
208
        if($trackingID !== null)
209
        {
210
            $params[] = sprintf('"%s"', $trackingID);
211
        }
212
213
        if(!empty($queryParams))
214
        {
215
            foreach($queryParams as $name => $value)
216
            {
217
                $params[] = sprintf(
218
                    '"%s=%s"',
219
                    $name,
220
                    $value
221
                );
222
            }
223
        }
224
225
        $paramsString = implode(' ', $params);
226
227
        $cmd = $this->commands->createCommand(
228
            'ShowURL',
229
            '',
230
            $paramsString,
231
            '{showurl: '.$paramsString.'}'
232
        );
233
234
        $this->instantiator->checkCommand($cmd);
235
236
        if($cmd instanceof Mailcode_Commands_Command_ShowURL)
237
        {
238
            return $cmd;
239
        }
240
241
        throw $this->instantiator->exceptionUnexpectedType('ShowURL', $cmd);
242
    }
243
}
244