Mailcode_Commands_Command_Mono   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 144
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 24
eloc 52
dl 0
loc 144
rs 10
c 2
b 0
f 0

15 Methods

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