Passed
Push — master ( 6c661c...e8275e )
by Sebastian
04:14
created

Mailcode_Factory_CommandSets_Set_Misc::mono()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 19
c 1
b 0
f 0
nc 8
nop 2
dl 0
loc 33
rs 9.6333
1
<?php
2
/**
3
 * File containing the {@see Mailcode_Factory_CommandSets_Set_Misc} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Utilities
7
 * @see Mailcode_Factory_CommandSets_Set_Misc
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
/**
15
 * Factory utility used to create commands.
16
 *
17
 * @package Mailcode
18
 * @subpackage Utilities
19
 * @author Sebastian Mordziol <[email protected]>
20
 */
21
class Mailcode_Factory_CommandSets_Set_Misc extends Mailcode_Factory_CommandSets_Set
22
{
23
    /**
24
     * @param string $comments Quoted or unquoted string.
25
     * @return Mailcode_Commands_Command_Comment
26
     * @throws Mailcode_Exception
27
     * @throws Mailcode_Factory_Exception
28
     */
29
    public function comment(string $comments) : Mailcode_Commands_Command_Comment
30
    {
31
        $cmd = Mailcode::create()->getCommands()->createCommand(
32
            'Comment',
33
            '', // type
34
            $this->instantiator->quoteString($comments),
35
            sprintf(
36
                '{comment: "%s"}',
37
                $this->instantiator->quoteString($comments)
38
            )
39
        );
40
    
41
        $this->instantiator->checkCommand($cmd);
42
        
43
        if($cmd instanceof Mailcode_Commands_Command_Comment)
44
        {
45
            return $cmd;
46
        }
47
        
48
        throw $this->instantiator->exceptionUnexpectedType('Comment', $cmd);
49
    }
50
51
    /**
52
     * Creates a for loop command.
53
     *
54
     * @param string $sourceVariable
55
     * @param string $loopVariable
56
     * @return Mailcode_Commands_Command_For
57
     * @throws Mailcode_Exception
58
     * @throws Mailcode_Factory_Exception
59
     */
60
    public function for(string $sourceVariable, string $loopVariable) : Mailcode_Commands_Command_For
61
    {
62
        $sourceVariable = '$'.ltrim($sourceVariable, '$');
63
        $loopVariable = '$'.ltrim($loopVariable, '$');
64
        
65
        $cmd = Mailcode::create()->getCommands()->createCommand(
66
            'For', 
67
            '', 
68
            sprintf(
69
                '%s in: %s',
70
                $loopVariable,
71
                $sourceVariable
72
            ), 
73
            sprintf(
74
                '{for: %s in: %s}',
75
                $loopVariable,
76
                $sourceVariable
77
            )
78
        );
79
        
80
        $this->instantiator->checkCommand($cmd);
81
        
82
        if($cmd instanceof Mailcode_Commands_Command_For)
83
        {
84
            return $cmd;
85
        }
86
        
87
        throw $this->instantiator->exceptionUnexpectedType('For', $cmd);
88
    }
89
90
    /**
91
     * Creates a break command, which can be used to break out of
92
     * a loop command. Using this outside of a loop will trigger
93
     * a validation error.
94
     *
95
     * @return Mailcode_Commands_Command_Break
96
     * @throws Mailcode_Exception
97
     * @throws Mailcode_Factory_Exception
98
     */
99
    public function break() : Mailcode_Commands_Command_Break
100
    {
101
        $cmd = Mailcode::create()->getCommands()->createCommand(
102
            'Break',
103
            '',
104
            '',
105
            '{break}'
106
        );
107
108
        $this->instantiator->checkCommand($cmd);
109
110
        if($cmd instanceof Mailcode_Commands_Command_Break)
111
        {
112
            return $cmd;
113
        }
114
115
        throw $this->instantiator->exceptionUnexpectedType('Break', $cmd);
116
    }
117
118
    /**
119
     * Creates a preprocessor command to format text as preformatted.
120
     *
121
     * NOTE: Requires the Mailcode text to be preprocessed using the
122
     * preprocessor. See the documentation on how to use it.
123
     *
124
     * @param bool $multiline
125
     * @param string[] $classes
126
     * @return Mailcode_Commands_Command_Mono
127
     * @throws Mailcode_Exception
128
     * @throws Mailcode_Factory_Exception
129
     */
130
    public function mono(bool $multiline=false, array $classes=array()) : Mailcode_Commands_Command_Mono
131
    {
132
        $params = '';
133
        $source = '{code';
134
135
        if($multiline) {
136
            $params = 'multiline:';
137
            $source = '{code: multiline:';
138
        }
139
140
        if(!empty($classes)) {
141
            $classString = sprintf('"%s"', implode(' ', $classes));
142
            $params .= $classString;
143
            $source .= $classString;
144
        }
145
146
        $source .= '}';
147
148
        $cmd = Mailcode::create()->getCommands()->createCommand(
149
            'Mono',
150
            '',
151
            $params,
152
            $source
153
        );
154
155
        $this->instantiator->checkCommand($cmd);
156
157
        if($cmd instanceof Mailcode_Commands_Command_Mono)
158
        {
159
            return $cmd;
160
        }
161
162
        throw $this->instantiator->exceptionUnexpectedType('Mono', $cmd);
163
    }
164
165
    public function code(string $language) : Mailcode_Commands_Command_Code
166
    {
167
        $cmd = Mailcode::create()->getCommands()->createCommand(
168
            'Code',
169
            '',
170
            sprintf(
171
                '"%s"',
172
                $language
173
            ),
174
            sprintf(
175
                '{code: "%s"}',
176
                $language
177
            )
178
        );
179
180
        $this->instantiator->checkCommand($cmd);
181
182
        if($cmd instanceof Mailcode_Commands_Command_Code)
183
        {
184
            return $cmd;
185
        }
186
187
        throw $this->instantiator->exceptionUnexpectedType('Code', $cmd);
188
    }
189
}
190