Test Failed
Push — develop ( 338959...4d106e )
by Paul
15:14
created

Module::colorStyleDeclaration()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 14
ccs 0
cts 11
cp 0
rs 9.9666
cc 3
nc 1
nop 1
crap 12
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Integrations\Divi\Modules\SiteReviewsForm;
4
5
use ET\Builder\FrontEnd\Module\Style;
6
use ET\Builder\Packages\Module\Options\Font\FontStyle;
7
use ET\Builder\Packages\StyleLibrary\Utils\StyleDeclarations;
8
use GeminiLabs\SiteReviews\Contracts\ShortcodeContract;
9
use GeminiLabs\SiteReviews\Integrations\Divi\Defaults\ModuleClassnamesDefaults;
10
use GeminiLabs\SiteReviews\Integrations\Divi\Defaults\ModuleStylesDefaults;
11
use GeminiLabs\SiteReviews\Integrations\Divi\Modules\DiviModule;
12
use GeminiLabs\SiteReviews\Shortcodes\SiteReviewsFormShortcode;
13
14
class Module extends DiviModule
15
{
16
    public static function blockName(): string
17
    {
18
        return 'glsr-divi/form';
19
    }
20
21
    /**
22
     * This method is equivalent to "module-classnames.ts".
23
     */
24
    public static function module_classnames(array $args): void
25
    {
26
        $args = glsr(ModuleClassnamesDefaults::class)->merge($args);
27
        $ratingColor = $args['attrs']['design']['decoration']['ratingColor']['desktop']['value'] ?? '';
28
        $args['classnamesInstance']->add('has-custom-color', !empty($ratingColor));
29
        parent::module_classnames($args);
30
    }
31
32
    /**
33
     * This method is equivalent to "module-styles.tsx".
34
     */
35
    public static function module_styles(array $args): void
36
    {
37
        $args = glsr(ModuleStylesDefaults::class)->merge($args);
38
        $attrs = $args['attrs'];
39
        $elements = $args['elements'];
40
        $orderClass = $args['orderClass'];
41
        $baseSelector = '.et-db #page-container .et_pb_section';
42
        Style::add([
43
            'id' => $args['id'],
44
            'name' => $args['name'],
45
            'orderIndex' => $args['orderIndex'],
46
            'storeInstance' => $args['storeInstance'],
47
            'styles' => [
48
                $elements->style([
49
                    'attrName' => 'module',
50
                    'styleProps' => [
51
                        'border' => [
52
                            'propertySelectors' => [
53
                                'desktop' => [
54
                                    'value' => [
55
                                        'border-radius' => implode(',', [
56
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-dropzone",
57
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-input",
58
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-input-checkbox",
59
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-select",
60
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-textarea",
61
                                        ]),
62
                                        'border-style' => implode(',', [
63
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-dropzone",
64
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-input",
65
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-input-checkbox",
66
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-input-radio",
67
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-input-range",
68
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-select",
69
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-textarea",
70
                                            "{$baseSelector} {$orderClass} .glsr-form .glsr-toggle-track::before",
71
                                        ]),
72
                                    ],
73
                                ],
74
                            ],
75
                        ],
76
                        'boxShadow' => [
77
                            'selector' => implode(',', [
78
                                "{$baseSelector} {$orderClass} .glsr-form .glsr-dropzone",
79
                                "{$baseSelector} {$orderClass} .glsr-form .glsr-input",
80
                                "{$baseSelector} {$orderClass} .glsr-form .glsr-input-checkbox",
81
                                "{$baseSelector} {$orderClass} .glsr-form .glsr-input-radio",
82
                                "{$baseSelector} {$orderClass} .glsr-form .glsr-input-range",
83
                                "{$baseSelector} {$orderClass} .glsr-form .glsr-select",
84
                                "{$baseSelector} {$orderClass} .glsr-form .glsr-textarea",
85
                                "{$baseSelector} {$orderClass} .glsr-form .glsr-toggle-track::before",
86
                            ]),
87
                        ],
88
                    ],
89
                ]),
90
                $elements->style([
91
                    'styleProps' => [
92
                        'advancedStyles' => [
93
                            [
94
                                'componentName' => 'divi/common',
95
                                'props' => [
96
                                    'attr' => $attrs['design']['decoration']['ratingColor'] ?? [],
97
                                    'property' => '--glsr-form-star-bg',
98
                                    'selector' => "{$orderClass} .glsr-form",
99
                                ],
100
                            ],
101
                            [
102
                                'componentName' => 'divi/common',
103
                                'props' => [
104
                                    'attr' => $attrs['design']['decoration']['ratingSize'] ?? [],
105
                                    'property' => '--glsr-form-star',
106
                                    'selector' => "{$orderClass} .glsr-form",
107
                                ],
108
                            ],
109
                            [
110
                                'componentName' => 'divi/common',
111
                                'props' => [
112
                                    'attr' => $attrs['design']['decoration']['columnGap'] ?? [],
113
                                    'property' => '--glsr-form-col-gap',
114
                                    'selector' => "{$orderClass} .glsr-form",
115
                                ],
116
                            ],
117
                            [
118
                                'componentName' => 'divi/common',
119
                                'props' => [
120
                                    'attr' => $attrs['design']['decoration']['rowGap'] ?? [],
121
                                    'property' => '--glsr-form-row-gap',
122
                                    'selector' => "{$orderClass} .glsr-form",
123
                                ],
124
                            ],
125
                            [
126
                                'componentName' => 'divi/common',
127
                                'props' => [
128
                                    'attr' => $attrs['design']['decoration']['spacing'] ?? [],
129
                                    'declarationFunction' => static::spacingStyleDeclaration(),
130
                                    'selector' => "{$orderClass} .glsr-form",
131
                                ],
132
                            ],
133
                            [
134
                                'componentName' => 'divi/common',
135
                                'props' => [
136
                                    'attr' => $attrs['design']['decoration']['background'] ?? [],
137
                                    'declarationFunction' => static::colorStyleDeclaration(['--glsr-input-bg', '--glsr-toggle-bg-0']),
138
                                    'selector' => "{$orderClass} .glsr-form",
139
                                ],
140
                            ],
141
                            [
142
                                'componentName' => 'divi/common',
143
                                'props' => [
144
                                    'attr' => $attrs['design']['decoration']['choiceColor'] ?? [],
145
                                    'declarationFunction' => static::colorStyleDeclaration(['--glsr-choice-color']),
146
                                    'selector' => implode(',', [
147
                                        "{$orderClass} .glsr-input-checkbox:checked",
148
                                        "{$orderClass} .glsr-input-radio:checked",
149
                                        "{$orderClass} .glsr-input-range:checked",
150
                                        "{$orderClass} .glsr-toggle-switch:has(:checked)",
151
                                    ]),
152
                                ],
153
                            ],
154
                            [
155
                                'componentName' => 'divi/common',
156
                                'props' => [
157
                                    'attr' => $attrs['design']['advanced']['focus']['background'] ?? [],
158
                                    'declarationFunction' => static::colorStyleDeclaration(['--glsr-input-bg', '--glsr-toggle-bg-0']),
159
                                    'selector' => implode(',', [
160
                                        "{$orderClass} .glsr-input:focus",
161
                                        "{$orderClass} .glsr-select:focus",
162
                                        "{$orderClass} .glsr-textarea:focus",
163
                                    ]),
164
                                ],
165
                            ],
166
                            [
167
                                'componentName' => 'divi/common',
168
                                'props' => [
169
                                    'attr' => $attrs['design']['advanced']['focus']['textColor'] ?? [],
170
                                    'declarationFunction' => static::colorStyleDeclaration(['--glsr-input-color']),
171
                                    'selector' => implode(',', [
172
                                        "{$orderClass} .glsr-input:focus",
173
                                        "{$orderClass} .glsr-select:focus",
174
                                        "{$orderClass} .glsr-textarea:focus",
175
                                    ]),
176
                                ],
177
                            ],
178
                        ],
179
                    ],
180
                ]),
181
                FontStyle::style([
182
                    'attr' => $attrs['design']['decoration']['textFont'] ?? [],
183
                    'orderClass' => $orderClass,
184
                    'selector' => "{$baseSelector} {$orderClass} .glsr-field",
185
                    'propertySelectors' => [
186
                        'font' => [
187
                            'desktop' => [
188
                                'value' => [
189
                                    'color' => implode(', ', [
190
                                        "{$baseSelector} {$orderClass} .glsr-field .glsr-dropzone",
191
                                        "{$baseSelector} {$orderClass} .glsr-field .glsr-input",
192
                                        "{$baseSelector} {$orderClass} .glsr-field .glsr-select",
193
                                        "{$baseSelector} {$orderClass} .glsr-field .glsr-textarea",
194
                                        "{$baseSelector} {$orderClass} .glsr-field .glsr-input-checkbox",
195
                                        "{$baseSelector} {$orderClass} .glsr-field .glsr-input-radio",
196
                                        "{$baseSelector} {$orderClass} .glsr-field .glsr-input-range",
197
                                    ]),
198
                                ],
199
                            ],
200
                        ],
201
                    ],
202
                ]),
203
                FontStyle::style([
204
                    'attr' => $attrs['design']['decoration']['labelFont'] ?? [],
205
                    'orderClass' => $orderClass,
206
                    'selector' => "{$baseSelector} {$orderClass} .glsr-field .glsr-label",
207
                    'propertySelectors' => [
208
                        'font' => [
209
                            'desktop' => [
210
                                'value' => [
211
                                    'color' => "{$baseSelector} {$orderClass} .glsr-field .glsr-label",
212
                                ],
213
                            ],
214
                        ],
215
                    ],
216
                ]),
217
            ],
218
        ]);
219
        parent::module_styles($args);
220
    }
221
222
    public static function shortcodeInstance(): ShortcodeContract
223
    {
224
        static $shortcode;
225
        if (empty($shortcode)) {
226
            $shortcode = glsr(SiteReviewsFormShortcode::class);
227
        }
228
        return $shortcode;
229
    }
230
231
    protected static function colorStyleDeclaration(array $cssProperties): callable
232
    {
233
        return static function (array $args) use ($cssProperties): string {
234
            $color = $args['attrValue']['color'] ?? null;
235
            $declarations = new StyleDeclarations([
236
                'returnType' => 'string',
237
                'important' => false,
238
            ]);
239
            if ($color) {
240
                foreach ($cssProperties as $prop) {
241
                    $declarations->add($prop, $color);
242
                }
243
            }
244
            return $declarations->value();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $declarations->value() could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
245
        };
246
    }
247
248
    protected static function spacingStyleDeclaration(): callable
249
    {
250
        return static function (array $args): string {
251
            $padding = $args['attrValue']['padding'] ?? null;
252
            $declarations = new StyleDeclarations([
253
                'returnType' => 'string',
254
                'important' => false,
255
            ]);
256
            if (isset($padding['top'])) {
257
                $declarations->add('--glsr-input-pt', $padding['top']);
258
            }
259
            if (isset($padding['right'])) {
260
                $declarations->add('--glsr-input-pr', $padding['right']);
261
            }
262
            if (isset($padding['bottom'])) {
263
                $declarations->add('--glsr-input-pb', $padding['bottom']);
264
            }
265
            if (isset($padding['left'])) {
266
                $declarations->add('--glsr-input-pl', $padding['left']);
267
            }
268
            return $declarations->value();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $declarations->value() could return the type array which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
269
        };
270
    }
271
}
272