1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@see \Mailcode\Mailcode_Commands_Command_Mono} class. |
4
|
|
|
* |
5
|
|
|
* @package Mailcode |
6
|
|
|
* @subpackage Commands |
7
|
|
|
* @see \Mailcode\Mailcode_Commands_Command_Mono |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Mailcode; |
13
|
|
|
|
14
|
|
|
use AppUtils\ConvertHelper; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Mailcode command: opening CODE statement. |
18
|
|
|
* |
19
|
|
|
* @package Mailcode |
20
|
|
|
* @subpackage Commands |
21
|
|
|
* @author Sebastian Mordziol <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class Mailcode_Commands_Command_Mono |
24
|
|
|
extends Mailcode_Commands_Command |
25
|
|
|
implements |
26
|
|
|
Mailcode_Commands_Command_Type_Opening, |
27
|
|
|
Mailcode_Interfaces_Commands_Multiline, |
28
|
|
|
Mailcode_Interfaces_Commands_PreProcessing |
29
|
|
|
{ |
30
|
|
|
use Mailcode_Traits_Commands_Validation_Multiline; |
31
|
|
|
use Mailcode_Traits_Commands_Type_Opening; |
32
|
|
|
|
33
|
|
|
const ERROR_INVALID_CSS_CLASS_NAME = 82201; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string[] |
37
|
|
|
*/ |
38
|
|
|
protected $classes = array(); |
39
|
|
|
|
40
|
|
|
public function getName() : string |
41
|
|
|
{ |
42
|
|
|
return 'mono'; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getLabel() : string |
46
|
|
|
{ |
47
|
|
|
return t('Format text as monospaced'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function supportsType(): bool |
51
|
|
|
{ |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function supportsURLEncoding() : bool |
56
|
|
|
{ |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getDefaultType() : string |
61
|
|
|
{ |
62
|
|
|
return ''; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function requiresParameters(): bool |
66
|
|
|
{ |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function supportsLogicKeywords() : bool |
71
|
|
|
{ |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function getValidations() : array |
76
|
|
|
{ |
77
|
|
|
return array( |
78
|
|
|
'multiline', |
79
|
|
|
'class' |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
protected function validateSyntax_class(): void |
84
|
|
|
{ |
85
|
|
|
$literals = $this->params->getInfo()->getStringLiterals(); |
86
|
|
|
|
87
|
|
|
if(empty($literals)) { |
88
|
|
|
return; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
foreach($literals as $literal) |
92
|
|
|
{ |
93
|
|
|
$parts = ConvertHelper::explodeTrim(' ', $literal->getText()); |
94
|
|
|
|
95
|
|
|
foreach($parts as $part) { |
96
|
|
|
$this->addClass($part); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $className |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
|
|
public function addClass(string $className) : bool |
106
|
|
|
{ |
107
|
|
|
$className = trim($className); |
108
|
|
|
|
109
|
|
|
if($this->isClassNameValid($className)) |
110
|
|
|
{ |
111
|
|
|
if(!in_array($className, $this->classes)) { |
112
|
|
|
$this->classes[] = $className; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return true; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$this->validationResult->makeError( |
119
|
|
|
t('%1$s is not a valid CSS class name.', $className).' '. |
120
|
|
|
t('Allowed characters:').' '. |
121
|
|
|
t('Upper and lowercase letters, digits, underscores (_) and hyphens (-).').' '. |
122
|
|
|
t('May not start with a digit, two hyphens, or a hyphen followed by a digit.'), |
123
|
|
|
self::ERROR_INVALID_CSS_CLASS_NAME |
124
|
|
|
); |
125
|
|
|
|
126
|
|
|
return false; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function isClassNameValid(string $className) : bool |
130
|
|
|
{ |
131
|
|
|
$result = preg_match('/\A[a-z_][a-z0-9_][-_a-z0-9]+\Z/si', $className); |
132
|
|
|
|
133
|
|
|
return $result !== false && $result > 0; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function generatesContent() : bool |
137
|
|
|
{ |
138
|
|
|
return true; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function preProcessOpening(): string |
142
|
|
|
{ |
143
|
|
|
if($this->isMultiline()) { |
144
|
|
|
return sprintf('<pre%s>', $this->renderAttributes()); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return sprintf('<code%s>', $this->renderAttributes()); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function preProcessClosing(): string |
151
|
|
|
{ |
152
|
|
|
if($this->isMultiline()) { |
153
|
|
|
return '</pre>'; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return '</code>'; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
private function renderAttributes() : string |
160
|
|
|
{ |
161
|
|
|
if(empty($this->classes)) { |
162
|
|
|
return ''; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return sprintf(' class="%s"', implode(' ', $this->classes)); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|